Multiple UIViewControllers - Best way to implement this - iphone

I'm building an iOS application and I try to determine the best way to implement this :
I have a main UIViewController (MainViewController) that displays a simple view. This view contains a button that let the user add an object (let's say a Circle) to the main view. The user can add multiple circles by pressing the button and move each of them by dragging them. The circle objects should have their own color (randomly chosen).
The question is: what is the best way to implement this?
Should I create an other UIViewController subclass (CircleViewController) for the Circle object, whose view actually draws the circle?
And then, when the user presses the button, should I create a new instance of this CircleViewController and add its view to the MainViewController?
When the user double-tap a circle, it should disappear... How can I send a message to the mainViewController to tell it to remove the concerned CircleViewController's view?
Thank you very much for your help.

If your object is really as simple as a circle you should look at Quartz in Apple's Documentation and the method drawRect: in UIView. If you are doing something more like an image you could subclass UIView and put your code in there. Either way, you do not need to create new viewControllers.

Related

How do I change UILabels within my SKScene code?

I'm making an app where I have a tower in the background rendered with SpriteKit. The tower is made out of different blocks where I get the data from. I want to display this data in a UIView with different UILabels, etc...
Here's an image of the UIView and UILabels I try to link
https://imgur.com/vOw5QoW
The problem is that I can't figure out how to link my labels and view to my SKScene code (which is not possible I know). I'm struggling to find a way to link these UI objects to my SKScene code (Show and hide the view when a certain action happens etc...)
Is there a way to link these 2?
(I know the existence of creating UI elements with SK code, but that's not what i'm looking for.)
If you want to display both SKView and UIView on the same screen, you can use Container View Controllers.
You define "container views" in your storyboard and assign them to separate view controllers via "embedded" segue.
Here's a good article on doing it - https://useyourloaf.com/blog/container-view-controllers/

Updatable, custom view on a UIToolbar

I want to make a small area to present some information in the middle of a UIToolbar and was wondering what the best way to do this is.
I need to show some text and a graphic, both of which need to be updated (around every 3 seconds) as new information arrives. The graphic is similar to the iPhone signal strength indicator, so it can be either custom drawn or selected from one of 3 graphics (low, medium, high strength).
I'll probably use initWithCustomView: to create a UIBarButtonItem, although I would like the view to be clickable (to allow the user to change the information shown).
What's the best way to make that view? Can I use a NIB, or do I need to do custom drawing in the view? What's the best way to update the buttons? I'm assuming that I'll have to remake the toolbarItems array each time and set it when the information changes. Is there a cleaner way to do this? Thanks.
Using initWithCustomView: sounds like a good way to go. You can create your custom view any way you want: with a NIB, by drawing it, even using images. It can also have its own subviews. It can be any object that inherits from UIView. (So, if you wanted, you could even make it actionable by using a UIButton, a custom UIControl, or a custom UIView with a gesture recognizer attached.)
You shouldn't have to remake toolbarItems (or, for that matter, do anything with it after you've added all your button items) if you just keep a pointer to your custom view UIBarButtonItem. It could be an instance variable. Then, to update the custom view, you could access it as you would any other view. I've never actually tried this, but I can't see any problem with doing it.
You sound like you had it mostly figured out. Hope this is helpful.
I needed the same solution and was hoping for some code examples from you. So I ended up doing it all in IB and the steps are here as follows:
Create UItoolbar in IB with no Items. (A Bar Button Item will be added again once you add the UIView)
Add UIView as subview of UIToolbar
Add UILabels to subview of UIView that is already a subview of the UIToolbar.
Create IBOutlets from UIToolbar, UIView and each UILabel and then you can reference the labels in your app.
I did set the backgrounds to clearColor so the text appears on top of UIToolbar without any box or borders.
And the text updates dynamically which was the desired outcome.
Hope this helps someone as this has been eluding me for a while.

Adding an image to my UIViewController

just a quick question here.
How can I add an image to my UIViewController. Currently, my view controller has a couple labels and a couple buttons, but I have a file line.jpg that I'd like to add to this view controller as well. And I'd like to add it at a specific location (namely, at the center of the screen). How could I (meaning, what is the code?) initialize some object (that encapsulates my image) and then add it to the screen?
Thanks!
Use UIImageView for that.
http://developer.apple.com/iphone/library/documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

UIButton Game character selection to UIImageView animation

I am almost at the end of coding my kids educational application, woohoo!.
But... Im stuck on something.
When app loads i have my main view. It has 4 buttons for flipviews(each with ten views of content) and 4 buttons for character selection(an image that will follow you through every screen of content).
Problem is im unsure on how to link UIButton selection to UIImage display in multiple views. I want the user to choose a character button and then continue to the flipviews and in the views the image displayed should be the one that they have selected on the main view. So everytime they return to the main view they can change the character that will follow them around the app.
Any thoughts, help or code would be much appreciated!
Thank You
Alex
Make a new object, a subclass of UIImageView, which has a -setImage method. Once you set the image, then where ever you embed that object, it will display the same image. You could even have that subclass view have a score displayed next to it, or a name or other stats, so as you go from one screen to another, you have all that info follow you around with the image. No need to create labels in all the screens for global info like that.
In summary:
make a new subclass of UIView or UIImageView in Xcode using the New File... menu. You would do new UIView if you will have other items than just an image.
add methods that allow you to set the image, update text stats etc.
BONUS: you can make the class handle taps, so if a user taps the image, you could do something like provide help or run a cute little animation
embed that object in any screens you wish. Keep in mind that you can have that view be sized differently in each screen using transforms. Cool, no?
that's pretty much it!
Good luck!

iPhone: detecting double taps on uiscrollview

Besides subclassing, is there a simple means to detect double taps on a UIImageView within a UIScrollView?
Thanks
I have created ZoomScrollView class (a drop-in subclass of UIScrollView) that can help you intercept any touches from a scroll view, and also handles double-tap zooming out of the box if that's what you want to do.
Grab it at github.com/andreyvit/ScrollingMadness/ (the README contains a long description of two UIScrollView tricks and the reasoning behind them).
Of course, if you did not want to zoom, and just wanted to intercept a double-tap on some inner image view, then subclassing is your friend. (Another way would be to attach a view controller to that image view or one of its parent views inside UIScrollView, then the controller will be part of the responder chain and will be able to handle the touches.)
Looking at UIImageView.h (within the UIKit framework) there are no public delegate methods or other methods that let you know if the image view has been double-tapped. You'll probably have to subclass.
The answer is NO.
http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/Introduction/Intro.html
Download the sample code (download link on the top).
See how apple did it.
See you.