views:

739

answers:

3

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 setRightBarButtonItem: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];

alt text

A: 

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.

calmh
There's nothing wrong with that part of it. The navigation controller keeps references to them, and that's enough.
Amagrammer
+1  A: 

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.

Amagrammer
thanks Amagrammer, you are right about that line, but when i hide and unhide toolbar, my button are disappear how can i call them back?
RAGOpoR
Please show us the code you use to hide AND unhide.Also, if the toolbar is in the navigation bar, you probably don't have to hide both -- just the nav bar.
Amagrammer
this code use for hide toolbar [self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES];
RAGOpoR
...and unhide? That's the one I really need to see.
Amagrammer
if (toggle) { [self.navigationController setNavigationBarHidden:YES animated:YES]; [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES]; [self.navigationController setToolbarHidden:YES animated:YES]; self.navigationController.hidesBottomBarWhenPushed = YES; } else { [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES]; [self.navigationController setNavigationBarHidden:NO animated:YES]; [self.navigationController setToolbarHidden:NO animated:YES]; }
RAGOpoR
Amagrammer
+3  A: 

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];
David Gelhar
thanks for your suggestion David Gelhar
RAGOpoR
thanks David Gelhar it work, my button are back after it hide!
RAGOpoR
Great answer...saved me 20 mins I'm sure :)
jkp