Other xib's auto-load fine, but not this one.
When I push this viewcontroller, initWithNibName, loadView, viewDidLoad, and viewWillAppear are called fine, but the view (and all self.xxx objects in #interface) are nil in all these methods, and I am left with an empty window under the navigationbar.
SettingsMain *newVC=[[SettingsMain alloc] initWithNibName:#"SettingsMain" bundle:nil];
[self.navigationController pushViewController:newVC animated:YES];
I was wondering if I can force setting self.view to what's in the .xib, in loadView or initWithNibName, so that all the outlets etc are initalized.
The viewcontroller has the standard code,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
The problem is that when viewDidLoad is fired, self.view is nil.
Related
I have created a navigationController. In the OnButtonTap action I am adding a tabBarController to navigationController. Now I want the title of every tab on navigationItemBar. But when I change the title in the viewDidLoad method of viewControllers of tab-bar it won't change.
I tried this in viewDidLoad method of view controller in tab bars.
self.title = #"Friends";
self.navigationItem.title=#"Friends";
[self.navigationItem setHidesBackButton:YES];
and the button is also not hiding...
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title=#"YourTitle";
// Custom initialization
[self.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:#"selectedImage"]withFinishedUnselectedImage:[UIImage imageNamed:#"unselectedImage"]];
}
return self;
}
I have addded UIScrollView to my xib but it was not showing on to my view controller.
Also in the view did load method, I have added text boxes to the view controller. This is displayed when I push the view, but not the scrollview which I added inside the xib.
I tried to push the view in two ways
Way 1
SubscribedDetailedViewController *aDetailView = [[[SubscribedDetailedViewController alloc]initWithNibName:#"SubscribedDetailedViewController" bundle:nil]autorelease];
[self.navigationController pushViewController:aPoemDetailView animated:YES];
Way 2
SubscribedDetailedViewController *aDetailView = [[[SubscribedDetailedViewController alloc]init]autorelease];
[self.navigationController pushViewController:aPoemDetailView animated:YES];
Also I placed the below code
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
The above function also gets called.
Why the scrollview which I added into the xib is not being displayed? But the controls added via code inside the viewdidload being shown.
I've done this many times with code that is exactly the same, but for some reason it isn't working today.
ExampleViewController1 *exampleView = [[ExampleViewController1 alloc] initWithNibName:#"ExampleViewController1" bundle:nil];
[exampleView setProjectName:[[self.projectListArray objectAtIndex:indexPath.row] objectForKey:#"name"]];
NSLog(#"%#", [[self.projectListArray objectAtIndex:indexPath.row] objectForKey:#"name"]);
XAppDelegate.stackController pushViewController:exampleView fromViewController:nil animated:YES]
My NSLog prints out appropriately.
My ExampleViewController1.h file declared like:
#property(nonatomic, strong) NSString *projectName;
I then do this code in ExampleViewController1.m's
-(void)viewDidLoad {
NSLog(#"%#", self.projectName);
self.projectNameLabel.text = self.projectName;
[super viewDidLoad];
}
The results of my NSLogs are curious. The NSLog from my viewDidLoad appears to be getting called before my other one:
2012-04-22 10:59:41.462 StackedViewKit[43799:f803] (null)
2012-04-22 10:59:41.463 StackedViewKit[43799:f803] NewTest
I have confirmed that the (null) value there is from NSLog(#"%#", self.projectName);, but that should be the second NSLog called...I can't figure out why it is coming through first.
Someone requested this code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// random color
self.view.backgroundColor = [UIColor colorWithRed:((float)rand())/RAND_MAX green:((float)rand())/RAND_MAX blue:((float)rand())/RAND_MAX alpha:1.0];
}
return self;
}
As I expected, the problem is that you are trying to access self.view inside the initialization method. So move the line self.view.backgroundColor = ... to the viewDidLoad method:
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#", self.projectName);
self.projectNameLabel.text = self.projectName;
self.view.backgroundColor = [UIColor colorWithRed:((float)rand())/RAND_MAX green:((float)rand())/RAND_MAX blue:((float)rand())/RAND_MAX alpha:1.0];
}
In fact, the documentation of the view property says:
If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view.
So when you call self.view in the initialization method, the view controller will have to load the view (from the nib or using the loadView method). And that's why viewDidLoad is called.
viewDidLoad is called before a view controller is displayed for the
first time, not immediately after initWithNibName.
> viewDidLoad method is called after the view controller has loaded its view
hierarchy into memory. This method is called regardless of whether the
view hierarchy was loaded from a nib file or created programmatically
in the loadView method.
> initWithNibName The nib file you specify is not loaded right away. It
is loaded the first time the view controller’s view is accessed. If
you want to perform additional initialization after the nib file is
loaded, override the viewDidLoad method and perform your tasks there.
You can use App delegate to pass the data from one to another, that is another alternate solution.
you do in initWithNibName method itself. or in viewDidAppear.
Your initWithNibName method should be like this as per as #sch comments;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil] //just set it here first and then check
if (self) {
// do something here;
}
return self;
}
We just need to be smart enough to think about what do we need to in constructor and what do we need to at viewDidLoad (once it had loaded into memory)
In my navigation based application, initWithNibName method does not called/fire
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
I'm defining rootViewController (calViewController) in appdelegate like this
calViewController *objCalViewController = (calViewController *) [navController topViewController];
objCalViewController.context = [self managedObjectContext];
[window addSubview:navController.view];
[window makeKeyAndVisible];
Is this is the issue? Please give me a help
When your UIViewController is defined in a nib file or Storyboard (usually as an IBOutlet), initWithNibName:bundle: is not called, rather initWithCoder: is. This is the case when you use Interface Builder to set your UIViewController as part of UITabBarController or UINavigationController, and almost always when using Storyboards.
in your appdelegate.m do like this
calViewController *objCalViewController = [[calViewController alloc] initWithNibName:#"WebViewController_iPhone" bundle:nil];
objCalViewController.managedObjectContext = self.managedObjectContext;
navController = [[UINavigationController alloc] initWithRootViewController:objCalViewController];
[self.window setRootViewController:navController];
[window makeKeyAndVisible];
Ok lets say i have a viewController named TCViewController with
TCViewController.h, TCViewController.m and TCViewController.xib
and in TCViewController.m
i am overriding the below method.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
In my RootViewController i want to initialize TCViewController like this
RootViewController.m
-(void)viewDidLoad //not necessarily this one, can be any method
{
// This initialization calls the initWithNibname method implemented in TCViewController.m
TCViewController *viewController = [[TCViewController alloc] initWithNibName:#"TCViewController" bundle:nil];
[self.navigationController pushViewController:viewController animated:YES];
}
If you initialize a viewController like this, control will pass to the initWithNibName method in your subclass if you have implemented it.
I have a uiviewcontroller with two properties: trackName and playerObject. PlayerObject also has a trackName property. I call this uiviewcontroller from my main uiviewController with this code:
SecondaryViewController *nextViewController = [[SecondaryViewController alloc] initWithNibName:#"SecondaryViewController" bundle:nil];
NSString *trackName = #"a track";
nextViewController.trackName = trackName;
[self.navigationController pushViewController:nextViewController animated:YES];
[nextViewController release];
In SecondaryViewController I override the initwithnibname method to set the trackName of the playerObject. I do this with this code:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
playerObject.trackName = trackName;
}
playerObject.trackName = trackName;
return self;
}
Finally my playerObject has all of the view data the SecondaryViewController will need. It looks like:
- (void)awakeFromNib{
NSString *s = trackName;
//more code relevant to the the view controller
}
When I debug, the trackName string in the playerObject is nil. I assume I'm doing something wrong. How can I have this value populated with the trackName I originally passed in the main uiview controller?
It seems like when you are initing the viewController the playerObject variable has not yet been set, could this be possible?
This can sometimes happen when you override initWithNibName:bundle:.
Instead use viewDidLoad to do setup. Apple guarantees all required setup is performed before this method is called (not the case with initWithNibName:bundle:).