UIViewController not working for the first time - iphone

MyController *myViewController = [[MyController alloc] initWithNibName:#"myView" bundle:nil];
The nib file myView.nib has 2 uiimageviews and 2 uilabels. When I first init myViewController, all the 4 subviews are set as 0x0.
The second time I dont get such a behavior.

The view object itself does not get created until it is referenced via self.view and loadView is called. It could be that the first time you try to inspect the view or do something with it this hasn't happened yet, and the second time could be after the system creates the view if you are adding it to another view or a navigation controller or something.

You probably forgot to hook up the view in your Nib file to the view property of MyController, and/or hooked up the subviews to the various IBOutlets of MyController.

Kevlar is absolutely right. You can force loading view and setting up all references with the following statement:
if (myViewController.view);
It does nothing except you'll get all subviews bound to outlets.

Related

titleView only works in one view controller (self.navigationController is nil)

I have a few navigation controllers that are set up in a NIB under a tab controller. I'm trying to set up the same logo in the top view controller of each navigationcontroller.
In the first view controller that appears, I have this code in viewDidLoad:
self.navigationItem.titleView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"green-noback-logo-only.png"]] autorelease];
This works (well, almost, I'll have to resize the image) and replaces the text set up in the NIB with my logo image.
However, this exact same code doesn't work in either of the other two view controllers. Instead, any text I've set up for the title in the NIB shows. I've tried putting that code in initWithCoder, viewDidLoad, viewDidAppear and viewWillAppear and it does nothing. I'm explicitly setting leftBarButtonItem to nil, although I'm guessing it was nil to begin with. I have also already checked that self.navigationItem is not nil in any of the places where I'm trying to set the titleView.
Any idea what would be special about the other two controllers that would prevent them from having a titleView set? Otherwise, does someone have a more foolproof way to set titleView?
try setting the image view to self.navigationController.navigationItem instead of self.navigationItem.
tl;dr: I screwed up initWithCoder:.
A good wallbanger for a beginner. Following Paul N's answer, I discovered that self.navigationController == nil in the two broken view controllers. It took me another few hours of head-desking to figure out the rest.
All three top level view controllers were subclasses of UITableViewController. However, only two of them were using grouped style. I was overriding initWithCoder: to use initWithStyle: inside the two non-working table view controllers. This threw away the connection to the navigation controller stored in the NIB. I originally did this because I couldn't figure out how to set grouped style on those inside the NIB (suggested by another answer here).
Serves me right for subclassing in such a rotten fashion, I guess.
Anyway, the solution was to fix the initWithCoder: implementation to call [super initWithCoder:coder] as usual and set up the table view style in the NIB. I did this by dragging a table view under that view controller, setting the datasource, setting the delegate, and setting it to grouped style. (This is how table view controllers are set up in the NIB by default.)

problem with getting the nib file loaded in the view !

I have created 2 views named "FirstView" and "SecondView". Both views have nib files. Now I am getting the "FirstView" nib file perfectly and then on click, I am pushing my SecondView in the window. The SecondView gets loaded but the nib is not shown in that view. Its totally white!
code to change the view:
svc = [[signupViewController alloc] initWithNibName:#"signupViewController" bundle:nil];
[vc.view removeFromSuperview];
[window addSubview:svc.view];
Can anyone kindly help ?
Thanks.
-
ahsan
I suppose you have two controllers, one for FirstView and a second one for SecondView. So if you are allocating your controller programmatically make sure you are initializing the secondViewController with initWithNibName: at this point when you perform presentModalViewController or pushViewController you should be able to see your nib view.

UIViewController loaded from nib: -viewDidLoad not being called

Is there anything special I need to do when adding a UIViewController into a nib? My -viewDidLoad method is not being called, even though the nib is being loaded and its subclass is set in IB to my view controller class.
http://dl.dropbox.com/u/448021/Test.zip
There's my test case. I just can't figure out why FooViewController -viewDidLoad isn't being called.
Thanks for the help.
The FooViewController you created there serves no purpose, if I see things correctly. In MainWindow.xib, you have a navigation controller and your own RootViewController. So far so good. You define the view of that in RootViewController.xib. Also ok. But the View Controller inside that last xib will do nothing, until you do something like
[self.navigationController pushViewController:detailViewController animated:YES];
(which is in your didSelectRowAtIndexPath)
The commented out part in didSelectRowAtIndexPath basically invokes a new viewcontroller when a user selects a row, and does so while loading the associated xib file, which is loaded in this line:
DetailViewController *detailViewController = [[DetailViewController alloc]
initWithNibName:#"Nib name" bundle:nil];
You could also create the viewcontroller in a nib file, like you have now, but then you would need to define an
IBOutlet FooViewController *fooVC;
and link that up within IB, and then push this fooVC onto the view stack when the user selects something - in that case you would skip the alloc / init line above.
Add a view to FooViewController.
Just go to interface builder and drag a view to FooViewController.

superview and parentviewcontroller nil after adding a subview

I think I'm missing something fundamental and so I want to ask the community for some help. I'm building an app based around a basic iPhone Utility Application. My MainView and FlipsideView share some elements so I have created separate ViewControllers and nib files for those pieces. In order to do this I have done the following:
1. Created a viewcontroller called searchDateViewController which is the file's owner of searchDateView.xib
2. searchDateView.xib is basically a UIView with a UILabel inside, the view is wired up correctly
3. Inside both MainViewController.m and FlipsideViewController.m I add a subview as folllows:
- (void)loadView{
[super loadView];
searchDateViewController = [[SearchDateViewController alloc] initWithNibName:#"SearchDateView" bundle:nil];
[[searchDateViewController view] setFrame:[searchDateView frame]];
[[self view] addSubview:[searchDateViewController view]];
...
}
Everything displays and works just fine. Basically depending on actions that happen in each of the main and flipside views the UILabel of the nib is changed. However, I wanted to do something slightly different if the searchDateViewController is loaded from the MainView or the FlipsideView. However, I can't seem to figure out which ViewController is adding the searchDateViewController subview.
In searchDateViewController I tried:
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"superview %#", self.view.superview);
NSLog(#"parentviewcontroller %#", self.parentViewController);
}
In both cases I get nil.
So my question is - can I find out which ViewController is adding searchDateViewController a a subview? If so how? Or if my logic here is completely messed up, how should I be doing this?
Thanks!
viewDidLoad is invoked when the view controller has loaded its view. In your case, that happends in this line:
[[searchDateViewController view] setFrame:[searchDateView frame]];
At that moment, you haven't yet called addSubview: so it is no wonder the view's superview is nil.
To solve your problem, you should define a property inside SearchDateViewController to distinguish between the different cases. This property would then be set accordingly by the parent controller that creates the SearchDateViewController instance.
Generally, I do not think it is a good idea to use a UIViewController subclass as a controller for a view that is used as a subview of one or several fullscreen views rather than be used as a fullscreen view itself. Much of UIViewController's logic works on the assumption that it is used to manage a fullscreen view. For instance, with your design, I think it's possible that SearchDateViewController will modify the view's frame when the device orientation changes etc. Since you don't need all this functionality for a non-fullscreen subview, I suggest you subclass your SearchDateViewController directly from NSObject.
ViewController and views are completely separate.
In most cases, when you add a subview to a parent view you don't add its controller to the parent's viewController. The exception to this rule is the navigation controller which adds the controller instead of the view to maintain a hierarchy of view controllers.
Your SearchDate viewController can't find a parent controller because you never assigned one and the system does not do it automatically. You can just assign a parent controller when you evoke the view from another controller.
searchDateViewController.parentController=self;

adding multiple views to view controller inside a tab controller

I have a tabview controller to which I added a UIViewController to each tab. I want to have multiple UIViews inside the UIViewController.
So in the implementation of the UIViewController class I added [self.view addSubView:uiview1] and [self.view addSubView:uiview2]. The problem is that when I run the app, it crahes on load.
However, if I only used a single UIView and did: self.view = UIView1 that would work fine.
Does anyone know what is causing the problem? Or if I'm doing something fundamentally wrong?
Assuming you are doing this programmatically, you're supposed to create the view in the view controller's loadView method. So you must do this:
self.view = [[[UIView alloc] initWithFrame:someFrame] autorelease];
before you do this:
[self.view addSubview:uiview1];
[self.view addSubview:uiview2];
Otherwise, self.view would be nil.
There's no reason you can't have multiple views within your UIViewController's main view member variable. However, there are quite a few items left unanswered in your question:
How are you obtaining view1 and view2?
Are they outlets in your XIB file (are you using a XIB file, or creating everything in code), or are you creating them in code?
Where in your UIViewController subclass are you adding them to your view member variable?
What's the message printed to the console when it crashes?