I am new to iOS programming. I have created ViewController with MKMapView element, and I wanted to set delegate [mapView setDelegate:self]
First I done it in method initWithNibName:bundle: like:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[self map] setDelegate:self]];
UITabBarItem *item = [[UITabBarItem alloc] init];
[item setTitle:#"Map"];
[self setTabBarItem:item];
}
return self;
}
In this case MKMapView did not send me any messages, but when I placed setting delegate message to viewDidLoad method, it worked fine.
Could someone explain me why it was not working when setting delegate message was in initWithNibName:bundle?
Views do not get loaded in initWithNibName, it just initializes your viewcontroller class and load the xib file which contains your view details.
When viewcontroller calls viewDidLoad, you will have all your view objects allocated and initialized.
In your case, when you setDelegate in initWithNibname, you are calling it on a nil value, so nothing get set, but in viewDidLoad mapView is allocated and initialized, so it works fine.
For a deeper insight refer:
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ViewLoadingandUnloading/ViewLoadingandUnloading.html
Beautiful explanation here: What is the process of a UIViewController birth (which method follows which)?
Looking to understand the iOS UIViewController lifecycle
http://thejoeconwayblog.wordpress.com/2012/10/04/view-controller-lifecycle-in-ios-6/
This line is your problem:
[self map]
In initWithNibName the map is not yet initialized and it returns nil.
In viewDidLoad the map is already initialized.
Related
I am trying to debug why my override of dealloc is not getting called on one of my view controllers.
I have a view controller that is setup by storyboarding. I have override all 3 init methods:
-(id) initWithCoder:(NSCoder *)aDecoder {
NSLog(#"TestViewController init with coder");
return [super initWithCoder:aDecoder];
}
-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
NSLog(#"TestViewController init with nib name");
return [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
}
- (id) init {
NSLog(#"TestViewController init");
return [super init];
}
When I navigate to the said view in my app I see that 'initWithCoder' is called as expected. This is the only init method that I see called at all. It is called once when I navigate to my view.
However when I navigate away from my view controller dealloc does not get called. viewDidDisappear does get called. In view didDisappear I log the reference count:
-(void) viewDidDisappear:(BOOL)animated {
NSLog(#"TestViewController Retain count is %ld", CFGetRetainCount((__bridge CFTypeRef) self));
[super viewDidDisappear:animated];
}
Reference count is 5.
I also log the reference count in viewDidLoad and it is 8.
So it jumps from 5 to 8, then back to 5.
I only see the init method called once. I have no idea where to even start looking....
Do you have an instance variable pointing to your viewController object? If you still have a pointer it will not decrement the retain count even if the view is not on the screen.
The unusual retain counts are normal. The only count that matters is 0. If it is 0 it will be deallocated. There is no guarantee that one pointer means there will be a retain count of 1.
I have 2 view controllers in a tab bar controller. My 2nd Nav Controller wants to set a badge value.
This controller is not loaded when the app starts, so the badge does not show. If I go over to that tab, the badge is properly updated.
this snippet runs when the tab's View Controller loads/reloads/updates/etc...
[self.navigationController.tabBarItem setBadgeValue:[NSString stringWithFormat:#"%u",[self.photos count]]];
Is the correct way to do this: Override the Nav Controller with a custom class and put the badge value in at that level? It seems like that is where I should put this info, but I haven't found a definite answer.
When the TabBarController is loaded, all of it's contained initial viewControllers are initialised. But their views are not loaded until you navigate to the respective tab item. So you can't execute code at this point in any of the view-loading methods (viewDidLoad etc). However you can execute code by overriding one of the initialisation methods.
If using storyboards the process of unarchiving the viewController triggers this method when the NIB has loaded:
- (void) awakeFromNib
{
}
If not using Storyboards, this initialiser is called prior to NIB loading:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
In either case you can override these methods to execute some code...
- (void) awakeFromNib
[super awakeFromNib];
[self.navigationController.tabBarItem
setBadgeValue:[NSString stringWithFormat:#"badgeValue"]]];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[self.navigationController.tabBarItem
setBadgeValue:[NSString stringWithFormat:#"badgeValue"]]];
}
return self;
}
However you will need to take care where you are getting your data from. At this point self.photos may be uninitialised for example. If the data for this is coming out of userdefaults, you should be able to read those in here and set your badge accordingly.
I have a custom view which contains two UILabel. I want to customize their fonts before so I did that in initWithCoder method.
#implementation HomeTitleView
#synthesize ticketLabel;
#synthesize monthLabel;
- (id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
[monthLabel setFont:[UIFactory getFontForKey:#"home_month"]];
[ticketLabel setFont:[UIFactory getFontForKey:#"home_ticket"]];
}
return self;
}
#end
Unluckily, this did not work. Using a debugger, I found that monthLabel and ticketLabel are both nil. Anyone has idea how can I solve this? What callback or method I should implement so that I can access both of my labels?
You can't do that. The views don't exist yet. They are instantiated when the loadView method is called, which happens automatically when the view property is first accessed. If you want to manipulate your views after they have loaded, the correct method to use is viewDidLoad.
Edit: That's assuming you are working with a UIViewController class. If you are working with a UIView class, you can use awakeFromNib or didAddSubview:.
Do you ever assign monthLabel and ticketLabel to UILabel? Something like:
self.monthLabel = [[[UILabel alloc] init] autorelease];
self.ticketLabel = [[[UILabel alloc] init] autorelease];
If so, can you update your post with the code?
I have a view controller that initializes two other view controllers. The view for one controller wasn't showing, and I tracked the problem to the instance being nil when it's added to the superview.
Here is the code. viewDidLoad is being called before the favoritesTableVC is initialized. I can see this by placing breakpoints in the initialization methods of the resultsTableVC and favoritesTableVC view controllers.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
resultsTableVC = [[[ResultsTableVC alloc] initWithController:self andTableView:nil] retain];
favoritesTableVC = [[[FavoritesTableVC alloc] initWithFrame:CGRectMake(0, 10, self.view.frame.size.width, defaultFavoritesTableHeight) andController:self] retain];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:resultsTableVC.view];
[resultsTableVC release];
[self.view addSubview:favoritesTableVC.view];
[favoritesTableVC release];
}
Here is the order the methods are being called:
allResults init
resultsTableVC init
allResults viewDidLoad
addSubview allResultsVC
addSubview favoritesResultsVC
favoritesResultsVC init
This is a single thread, so I don't understand how viewDidLoad can be called before init is complete.
-[ResultsTableVC initWithController:andTableView:] is probably referencing allResults.view.
This would force the allResults controller to load its view (which then of course causes viewDidLoad to fire). All of this happens synchronously, before you actually return from initWithController:andTableView:
I'm taking a guess, but could you try this :
favoritesTableVC = [[[FavoritesTableVC alloc] initWithFrame:CGRectMake(0, 10, SOME_HARD_CODED_INT, SOME_HARD_CODED_INT) andController:self] retain];
And see if you get the same result.
My guess is that self.view is pointing to nil at that time.
But that wouldn't explain why the init is call after... but no harm in trying.
(I haven't tested it)
In post Using initWithNibName changes absolutely nothing, he shows two uses of the same View Nib definition, in the first case, he simply calls alloc/init and the second, he specifies initWithNibName.
So, while this always works:
MyViewController *vctrlr = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
The following works for all the View Controllers I've inherited, but not mine!
TheirViewController *vctrlr = [[TheirViewController alloc] init];
[self.navigationController pushViewController:vctrlr animated:YES];
[vctrlr release];
New to iOS programming, I inherited some code. All the View Controllers' views are defined in IB, but there was inconsistent allocation/init creation of those view controllers. I created a new View Controller and XIB, but it does not work unless I use initWithNibName (it crashes when I push the view controller onto the Nav Controller). I cannot tell how my view controller is different than the others... any hints? I was able to delete the initNibName usage for all the other view controllers in the app except mine.
You can pass any string name to initWithNibName:. You are not just restricted to calling initWithNibName:#"MyClassName" when your class is called MyClassName. It could be initWithNibName:#"MyClassNameAlternateLayout".
This becomes useful if you need to load a different nib depending on what the app needs to do. While I try to have one nib per view controller per device category (iPhone or iPad) whenever possible to make development and maintenance simpler, I could understand if a developer would want to provide a different layout or different functionality at times.
Another important point is that initWithNibName:bundle: is the designated initializer for UIViewController. When you call -[[UIViewController alloc] init], then initWithNibName:bundle: is called behind the scenes. You can verify this with a symbolic breakpoint. In other words, if you simply want the default behavior, it is expected that you can call -[[UIViewController alloc] init] and the designated initializer will be called implicitly.
If, however, you are calling -[[UIViewController alloc] init] and not getting the expected behavior, it's likely that your UIViewController subclass has implemented - (id)init incorrectly. The implementation should look like one of these two examples:
- (id)init
{
self = [super init];
if (self) {
// custom initialization
}
return self;
}
or
- (id)init
{
NSString *aNibName = #"WhateverYouWant";
NSBundle *aBundle = [NSBundle mainBundle]; // or whatever bundle you want
self = [self initWithNibName:aNibName bundle:aBundle];
if (self) {
// custom initialization
}
return self;
}
If you want to work following code:
MyViewController *vctrlr = [[MyViewController alloc] inil];
[self.navigationController pushViewController:vctrlr animated:YES];
Then you should implement following both methods in MyViewController:
- (id)init
{
self = [super initWithNibName:#"MyViewController" bundle:nil];
if (self != nil)
{
// Do initialization if needed
}
return self;
}
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
NSAssert(NO, #"Init with nib");
return nil;
}