button item lost when hide and unhide toolbar
i dont know, why the button are disappear after the toolbar set to hide and unhide. how can i fix it?
setup a button code
-(void)viewDidAppear:(BOOL)animated {
//NSLog(@"viewDidAppear ");
[self becomeFirstResponder];
//Create a button
UIBarButtonItem *back = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self action:@selector(goback:)];
UIBarButtonItem *fixspace1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:self action:nil];
UIBarButtonItem *next = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward
target:self action:@selector(gofwd:)];
UIBarButtonItem *stop = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemStop
target:self action:@selector(stopload:)];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self action:@selector(refreshWeb:)];
[self.navigationController.toolbar setItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];
[self.navigationItem setRightB开发者_开发知识库arButtonItem:refresh animated:YES];
[self.navigationController.view addSubview:self.navigationController.toolbar];
[stop release];
[next release];
[back release];
[refresh release];
[fixspace1 release];
}
and i setup my button at this method
-(void)viewDidAppear:(BOOL)animated
this code use for hide toolbar
[self.navigationController setNavigationBarHidden:YES animated:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
[self.navigationController setToolbarHidden:YES animated:YES];
The documented method for setting toolbar items is via the toolbarItems property of the view controller. The same UINavigationController Reference also lists the toolbar
property as read-only and specifically warns
You should not modify the UIToolbar object directly.
Therefore, try changing
[self.navigationController.toolbar setItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];
to
[self setToolbarItems:[NSArray arrayWithObjects:fixspace1, back, fixspace1, stop, fixspace1, next, fixspace1, nil] animated:YES];
Seeing no better answers, I'll promote my earlier comment. Try taking out this line:
[self.navigationController.view addSubview:self.navigationController.toolbar];
I haven't experimented with anything like that but it looks wrong and very much against the iPhone SDK philosophy. If the controller object already has a pointer to the toolbar, why would you need to add it to the view? If that's the right place for it, the controller object would do that itself.
I doub't that you should release your toolbar buttons immediately after adding them to the toolbar. You should save them in instance variables and release them in your dealloc
.
精彩评论