Re: Adding toolbar items only works in viewWillAppear: (?)
Re: Adding toolbar items only works in viewWillAppear: (?)
- Subject: Re: Adding toolbar items only works in viewWillAppear: (?)
- From: David Rowland <email@hidden>
- Date: Wed, 20 Mar 2013 15:24:02 -0700
I defer to your technical knowledge, but I think one should do related initializations in one place, and that isn't the place I would choose.
Yet, it does solve the OP's problem at no cost, making it a puzzle rather than a problem. Why isn't it appearing as expected?
David
On Mar 20, 2013, at 10:55 AM, Matt Neuburg <email@hidden> wrote:
> Far be it from me to step into a controversy (ha ha), but I feel compelled to push back against this oversimplification.
>
> (1) It's true that viewWillAppear can occur multiple times, but that doesn't make it a bad place to do one-time initialization. It is easy to distinguish the *first* time from all the other times, so the "once" problem is not really a problem at all.
>
> (2) viewDidLoad can also occur multiple times - well, it could before iOS 6. So that's no solution, per se, to the "once" problem.
>
> (3) For many things, viewWillAppear is much better place to initialize than viewDidLoad. Remember, viewDidLoad doesn't mean the view has been placed into the interface yet. It just means that the view controller has a view - no more. So, for example, dimensions may not yet be correct.
>
> (4) The tendency to fall back on viewDidLoad mindlessly can be wrong for other reasons. As I point out in my book, Apple warns (in the UIViewController class reference, under navigationItem) that configuring a view controller's navigation item in conjunction with the creation of its view is not a good idea, because the circumstances under which the view is needed are not identical to the circumstances under which the navigation item is needed. (However, Apple's own code examples then proceed to violate this warning quite shamelessly.)
>
> m.
>
>
> On Tue, 19 Mar 2013 20:28:29 -0700, David Rowland <email@hidden> said:
>> viewWillAppear occurs every time the view reappears, which may be frequently and has nothing to do with the creation and initialization of the view. Each time it appears you are recreating the button and setting in self.prevButton, e.g. which will release the former button and install an identical one. So, no, this is not the recommended place to do this.
>>
>> Why viewDidLoad fails isn't clear. Is it possible that the toolbar is hidden or behind some other view?
>>
>>
>> On Mar 19, 2013, at 6:48 PM, Koen van der Drift <email@hidden> wrote:
>>
>>> I am programmatically adding some toolbar items to a view in a UIViewController subclass, and it only seems to work when I put the code in viewWillAppear:
>>>
>>> // add a toolbar with a prev and next button
>>> self.navigationController.toolbarHidden = NO;
>>> UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: self action: nil];
>>> self.prevButton = [[UIBarButtonItem alloc] initWithTitle: @"Prev" style: UIBarButtonItemStyleBordered target: self action: nil];
>>> self.nextButton = [[UIBarButtonItem alloc] initWithTitle: @"Next" style: UIBarButtonItemStyleBordered target: self action: nil];
>>>
>>> self.toolbarItems = [NSArray arrayWithObjects: self.prevButton, flexibleItem, self.nextButton, nil];
>>>
>>> If I put it in viewDidLoad, the toolbar never shows up. In the book by Conway and Hillegass (3rd ed), they put similar code in init, but that also doesn't work in my case. Interestingly, I am adding a UISearchBar in init, and that works just fine.
>>>
>>> Why is that? Is viewWillAppear the recommended place to do this?
>>>
>
> --
> matt neuburg, phd = email@hidden, <http://www.apeth.net/matt/>
> A fool + a tool + an autorelease pool = cool!
> Programming iOS 6! http://shop.oreilly.com/product/0636920029717.do
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden