point of UIViewController in IB - xcode

What exactly is the point of adding a UIViewController in IB? There is no way to add code like you can if you create a viewController in Xcode? And if you can what is the advantage of doing it in IB.
And isn't the whole point of a MVC to seperate code into "modular" parts so why would add a ViewController in IB

Sometimes all you need is a UIViewController. But most times you would create a UIViewController subclass of your own (in IB).

Related

How does a XIB file in a ViewController is called

I know how to create a XIB for UIView or UIViewController classes. What I want to know is the lifecycle of XIB?
How it is constructed. I think it comes before ViewDidLoad but I don't have any constructed call for me to know how the ViewController is connected to XIB.
Can you guys give me a simple answer on how it works? Thanks in advance.

Keep leftBarButtonItems on new UIViewcontroller when pushed

I have a UIViewController embedded in a UINavigationController. The rootViewController now contains already some buttons as leftBarButtonItems.
Now when I push a new UIViewController on top of the UINavigationController I want the new UIViewController to keep the existing leftBarButtonItems extended with the Back-Button.
Right now the situations is as follows: When I push the new UIViewController then the existing leftBarButtonItems disappear and only the Back-Button is visible.
Each UIViewController has it's own "navigationItem" property, which acts as the navigation bar representation for that viewcontroller. When you add buttons to the navigationItem of a particular UIViewController they are limited in scope to the viewcontroller to which they were added, and they don't persist into other viewcontrollers.
Basically, you'll have to add the buttons to the navigationItem of each viewcontroller as it loads. You can make this simpler by writing adding a method to do this work to a class other than your UIViewControllers. What happens when you touch each button might be viewcontroller specific though, so you'll have to think through how touch actions will be fed back to the relevant viewcontroller. Perhaps introduce some kind of NavigationBarDelegate protocol or something?
I found what seems like a hacky way to get around this when pushing multiple instances of the same view controller on to a detail view controller which I assume would work similarly. Before pushing the new view controller I used this: (browser is my new view controller)
self.browser.navigationItem setLeftBarButtonItem:self.detailViewController.navigationItem.leftBarButtonItem animated:YES]; // Sets popover view controller button.
[self.detailViewController.navigationController pushViewController:self.browser animated:YES];
This probably isn't a good way to do it but it seems to work in my situation.

Can I use NSViewController to provide a reusable NSTableView with defined columns and data?

I'm fairly new to Xcode and Cocoa development, so please excuse me if I use the wrong terminology.
I have a window which has an NSTableView bound to a NSArrayController. The NSTableView has some predefined columns and the NSArrayController is populated with data when my window is loaded.
This all works fine, but I now need to re-use the functionality in a number of other windows.
After much reading I think and NSViewController is what I need, to provide an object that I can re-use in different windows in multiple .xib.
So what I have done is created a new NSViewController sub class in Xcode which also created a new xib for that controller.
The xib contains a custom view, where I have placed my NSTableView.
#interface KeyViewController : NSViewController
#property (weak) IBOutlet NSTableView *keyTable;
#end
The files owner class is set to the KeyViewController, and the view outlet is set to the files owner.
I then placed a ViewController object into the original window and hooked up the view to a new custom view in the window
I then set the nib name for the ViewController in its properties.
The new view never gets displayed and the view controller initWithNibName never gets called.
Am I missing something vital or missing the plot completely. Should you be able to do this just with interface builder or do I need to alloc and init the view in code?
If I do have to do it in code, whats the purpose of the ViewController object in IB and the Nib Name property for it?
Thanks..

tableView property with UIViewController (or UITableViewController)

#import <UIKit/UIKit.h>
#interface FourthViewController : UIViewController{
//#interface FourthViewController : UITableViewController{
I want to update my tableview so I trying to use [self.tableView reloadData] but i cannot even type this line of code since I'm working in a UIViewController and the tableView property is for UITableViewController's only. So I tried to be smart and just change the UIViewController to UITableViewController but instead i got this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UITableViewController loadView] loaded the "b4k-XH-Uhj-view-V2h-dE-ixJ" nib but didn't get a UITableView.'
Imo this is strange since a UITableViewController would work for a tableView but not the other way around, but seams like i got it all wrong, or whats happening here?
more code if you ask for it =)
Thanks ahead.
A UITableViewController is really just a UIViewController that contains an outlet to a UITableView and implements the two tableview delegates: UITableViewDelegate and UITableViewDataSource.
You may want to consider replacing the viewcontroller instance in your nib file with a UITableViewController rather than a standard UIViewController.
Alternatively, you can manually adjust your ForthViewController instance by creating an outlet for the contained UITableView to expose the tableview as a property.
However, if you do it this way you'll also have to manually implement the two tableview delegates UITableViewDelegate and UITableViewDataSource somewhere (probably in ForthViewController) and manually associate the UITableView with these delegates.

Why is my UIView not the "parent" of my UIImageView?

I have an mainUIView within which there is a customised UIImageView (named myImageView).
In the myImageView.h file, I have declared:
#property (strong, nonatomic) mainViewController *viewController;
and in the myImageView.m file, I have the following codes:
self.viewController = self.parent;
This lines raises an error indication:
Property 'parent' not found on object of type myImageView.
I thought putting the myImageView inside the mainUIView would make the default parent-and-sub relationship, but obviously it's not so.
In the main storyboard, I tried to Ctrl-drag the myImageView to the mainViewController, but no options pops up for me to select delegate.
Can anyone give me explanation of these please? I'm feeling a bit confused... Many thanks!
As your myImageView is a UIView is has no parent. Is just has a superview which again is a UIView. Basically a view doesn't know its controller. Although the controller can implement a view's delegate to get informed about events. So your AppDelegate knows your ViewController and the controller knows your ImageView. The AppDelegates window knows the view of your controller which will most likely contain the whole view hierarchy (except AlertViews, HUDViews, modal views and simmilar...).
I think what you want to do works best implementing the delegate pattern. (see Apple Docs)

Resources