开发者

toolbar does not get populated

In my loadView methoad of my开发者_开发百科 view controller I have the following code:

// Populate self.view and add some views/UI elements

// load Gender selection Bar
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.tintColor = [UIColor colorWithRed:0 green:0.37 blue:0.5 alpha:1];

self.genderControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Male", nil), NSLocalizedString(@"Female", nil), nil]];
genderControl.segmentedControlStyle = UISegmentedControlStyleBar;
genderControl.frame = CGRectMake(0, 0, 200, 30);
genderControl.tintColor = [UIColor colorWithRed:0 green:0.37 blue:0.5 alpha:1];
[genderControl addTarget:self action:@selector(changeGender:) forControlEvents:UIControlEventValueChanged];
genderControl.selectedSegmentIndex = GENDER_MALE;

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:genderControl];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

[[[self navigationController] toolbar] setItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];
[item release];

However, if I load the view it displays the toolbar, but the Segmented Control does not get displayed. If I move my code to the viewWillAppear: method it works, but once I hide my view and redisplay it again the segmented control is gone anew.

Does anybody know this problem and/or has an idea how to solve it? Looks very strange to me.


Most of your code looks fine to me, and everything should work when called from the viewDidLoad or the loadView method.

However, when you use the included toolbar from the NavigationController, toolbar items are set on the ViewController rather than the toolbar itself. So replace this line:

[[[self navigationController] toolbar] setItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];

With this:

[self setToolbarItems:[NSArray arrayWithObjects:flexSpace, item, flexSpace, nil] animated:YES];

Documentation here: http://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html#//apple_ref/occ/instm/UIViewController/setToolbarItems:animated:

You will also want to add this line:

[flexSpace release];


Are you sure that this code should be in loadView? Check the UIViewController docs: loadView is used for constructing your view controller's view entirely programmatically - nothing is loaded from a nib if you implement loadView. Should this code be in viewDidLoad instead? I see that you're not even creating and assigning the main UIView in your loadView method -- which you'd normally expect to see, e.g.:

self.view = [[[UIView alloc] init] autorelease];

If you did mean to implement loadView, as you have, you should add the above line for assigning your main view towards the top of the method, rather than later on.

Btw, perhaps don't bother calling [toolbar setItems] with animated=YES; not much point and I've seen the exact value of the animated parameter break things inexplicably in the past!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜