ios UIview in views - objective-c

I have a uiview that has all my buttons, that has its controller. How can I include the button view in all my uiviews using the interface builder? The buttons must work when click. The purpose is to increase maintainability.

You can create a custom UIView in interface builder like this:
Loading custom UIView from nib, all subviews contained in nib are nil?
Then add your custom view to other views in IB by adding a normal UIView and changing the class to your subclass.

Try this. I'm not sure that adding UIView in Interface Builder will work, but adding subview programmatically always works.

Related

Accessing class functions

I have created a UIView class. In this class it has touchesbegan and touches moved functions etc. When I create an instance of this class in my UIView controller they don't work.
Is it not possible for the instance to access these functions? I know the class works because if I drag a UIView onto the View Controller and assign it with the class I can drag it and resize it etc.
I have created a UIView class. In this class it has touchesbegan and
touches moved functions etc. When I create an instance of this class
in my UIView controller they don't work.
Check the userInteractionEnabled property of the view. The default value for any UIView is YES, so your view should track touches, but you may have disabled the view in your storyboard or .xib file.
Is it not possible for the instance to access these functions?
It's possible, and the entire touch handling mechanism relies on it. If your view is enabled, and it's not covered up by some other view, and there aren't any gesture recognizers that are stealing its touch events, it should be receiving touch messages.

Can I load nib contents in to UIView using only Interface Builder?

Does anyone know a way to, in a storyboard, load a UIView's contents from another nib? I know I can do this easily with code, but I am trying to figure out how to do more in IB.
I have a storyboard with my main UI layout, I have a UIScrollView and I want to design its contents in IB. The only way I could figure out how to do this was to design the UIView in its own .nib, but then my issue is, how do I load the nib without coding it to do so? Is this even possible? It doesn't seem too far fetched to me.
I'm assuming you simply want to lay out your UIScrollView in IB, that a .nib is mentioned because that was an approach you were exploring, but if you could do this strictly in your storyboard that would be acceptable, if not preferable:
First, create a new file in Xcode that is a subclass of UIScrollView.
In your storyboard, drag a UIScrollView out to the scene (viewcontroller) where you want to display this scroll view.
In the Identity inspector, set the Custom Class of the UIScrollView to your subclass of UIScrollView.
Create an outlet for this UIScrollView by ctrl+dragging the UIScrollView into the .h file of the ViewController subclass it's displayed in. Name it something like myScrollView
In your ViewController's -viewDidLoad method, set the contentSize property of the UIScrollView to whatever size you want it to be. So it will look something like:
self.myScrollView.contentSize = CGSizeMake(800,800);
Now, drag out UI objects to your UIScrollView and design.
IMPORTANT: To create outlets to these objects is a little tricky. Let's say you've dragged out a UILabel. You need to manually go into your UIScrollView subclass and add to the .h
#property (nonatomic, weak) IBOutlet UILabel* myLabel;
and to the .m
#synthesize myLabel = _myLabel;
Now you need to get your outline view on screen along with your storyboard and ctrl+drag FROM YOUR SCROLL VIEW TO YOUR LABEL to create an outlet. This is kind of the reverse of what you're used to.
Now you can reference that outlet from within the viewcontroller or the scrollview subclass . For instance, in the viewcontroller -viewDidLoad you could say:
self.scrollView.myLabel.text = #"Hello World";
HTH!
If what you want is to edit inside a scrollview from IB, it's a pain, but doable.
Have a look at my answer on this question.
Add a generic UIView in the IB, setting its custom class to the name of your nib file.
Replace GradientControl with the name of your nib file (minus the '.xib').

Changing rootviews view type in an iOS app

While creating a navigation based application, it automatically creates a root view controller which subclasses UITableViewController, but in MainWindow.xib as you know I can't see a UITableView is placed under root view controller but we drag and drop a table view there. Can we simply drag and drop a UIView instead of a UITableView and change the root view controller class to sublass a uiview instead of UITableView and change its methods?
Or I must drag a UITableView in IB for the root view controller? I am a beginner and I do not want to make complicated things so what is the simplest way of using a UIView as a root. If thats not simple I will stick with the table view.
Yes you can change. You just need to change some bindings in inteface builder and change rootViewController's superclass to UIViewController instead of UITableViewController.
UPDATE
First open the rootViewController.xib and delete the tableiew from there and drag-n-drop a UIView. Bind that View to view property in file-owner. Change superclass of rootViewController from UITableViewController to UIViewController. That's it.
Here are the screenshots.

How to add a toolbar to a UIPickerView subclass?

I've created a UIPickerView subclass, I've completed the implementation of the picker view itself.
What I would like to do however is to add a toolbar above the picker view as part of the new class. How and where should I do this in the subclass implementation?
I would add both the picker and the toolbar to another view and use that as your "control"

Is it possible to initialize a UINavigationController subclass from a NIB?

I have a UITabBarController based application. The tabs will be created from database entries, so I don't know them in advance. I'd like to programmatically initialize a UINavigationController subclass (I have a few different kinds) for each tab.
Ideally, I would really like to draw the whole UINavigationController subclass + it's subview using Interface Builder, just like you can do when you add define the view for each tab in IB (that's when you know what the tabs will be in advance). I've tried setting the "Class identity" to my UINavigationController subclass name, then add a UINavigationController inside it, but obviously it didn't inject the instance at the "File's owner" level...
Thanks
I've ended up not subclassing UINavigationController, it appears it isn't what's recommended. I quickly realized that there wasn't a whole lot of benefit from designing the UINavigationController's from within IB. Instead, I subclassed UIViewController, wrote the few lines of init for theUINavigationBar (custom buttons, custom middle image) in my viewDidLoad. It's just better to simply wrap the subclass with [[UINavigationController alloc] initWithRootController: subclass]; when I programmatically fill my tabs. Subclassing UINavigationControllerdirectly prevented me from properly using the navigationItem property of my inject view.