button item lost when hide and unhide toolbar - iphone

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

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.

Related

UINavigationController back and close button

I have a navigation controller. The controller works well and shows me the back button for go back to the window. But when I add this code for add the close button:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Chiudi"
style:UIBarButtonItemStylePlain target:self
action:#selector(dismissModalViewControllerAnimated:)];
self.navigationItem.rightBarButtonItem = doneButton;
[doneButton release];
}
...then the back button disappears, and I can only see the close button. Why?
THE PROBLEM was in
dismissModalViewControllerAnimated
I HAD TO USE
[self.navigationController dismissModalViewControllerAnimated:YES];
[self.navigationController popToRootViewControllerAnimated:NO];

Go Back to home screen

I have created button for home like this:
UIBarButtonItem * addButton=[[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStyleBordered target:self action:#selector(GoToHome:)];
[navItem setLeftBarButtonItem:addButton];
GoToHome function has only one line i.e:
[self.navigationController popToRootViewControllerAnimated:YES];
but when i click the Home button it shows no action (not working)
try this inside your GoToHome method
[self.parentViewController.parentViewController.parentViewController dismissModalViewControllerAnimated:YES];
have you connected the button code with the IB button? that can happen a lot and with everyone..!!
Try this,
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:#"Home" style:UIBarButtonItemStylePlain target:self action:#selector(GotoTopScreen)];
self.navigationItem.rightBarButtonItem = addButton;
-(void)GotoTopScreen{
[self.navigationController popToRootViewControllerAnimated:YES];
}
The best way to create a navigation controller is to create it in appDelegate and access it as appDelegate.navigationController. Then those nil object issues do not come.
Use this method:
UIBarButtonItem *flipButtons = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self
action:#selector(GotoTopScreen)];
self.navigationItem.leftBarButtonItem = flipButtons;
[flipButtons release];
-(void)GotoTopScreen
{
[self.navigationController popToRootViewControllerAnimated:YES];
}

Adding UIButton top bar of a navigation controller that pushes to another viewController

I have a UIButton that pushes to another view controller using the following code. How do I go about putting this UIButton to the top bar of a navigation controller.
-(IBAction) nextButtonPressed {
TipsViewController *objYourViewController = [[TipsViewController alloc] initWithNibName:#"TipsViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[TipsViewController release];
}
I was thinking of adding something like: self.navigationItem.rightBarButtonItem somewhere, but can't figure out the syntax.
Here's something else I tried with the following code: I can get a "Next" button in the top bar of the Navigation Controller. How would I get it to push to another view controller?
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStylePlain target:self pushViewController:anotherButton animated:YES];
self.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
thanks for any help.
When you init your UIBarButtonItem, do you see a parameter called action: ? Mention a selector there:
UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(Next:)];
The above item should be your rightBarButton as you want it to be.
And this would be your selector:
-(void)Next:(id)sender {
[self.navigationController pushViewController:nextViewController animated:YES];
}
UIBarButtonItem addButton=[[UIBarButtonItem alloc]initWithTitle:#"+" style:UIBarButtonItemStyleBordered target:self action:#selector(method)];

Putting a back button inside of a Modal view pushed by another modal view

Check this, im pushing a modal view inside of another modal view. But, im trying to put a button inside of this modal view, but without luck.
What im doing wrong?
Thanks!
CadastroViewController *addController = [[CadastroViewController alloc] initWithNibName:#"CadastroViewController" bundle:nil];
// This is where you wrap the view up nicely in a navigation controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addController];
// You can even set the style of stuff before you show it
navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
navigationController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
// And now you want to present the view in a modal fashion all nice and animated
[self presentModalViewController:navigationController animated:YES];
// make sure you release your stuff
[navigationController release];
[addController release];
You'll have to add a new UINavigationItem to the navigationbar of the actual viewcontroller - NOT the navigation controller.
addController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
You should add your button in a
-(void) viewDidLoad of your CadastroViewController controller class
This will look like this:
- (void) viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"OK" style:UIBarButtonItemStyleBordered target:self action:#selector(buy)];
self.navigationController. leftBarButtonItem = button;
[button release];
}
[self presentModalViewController: navigationController animated:YES]; is ok in your example, just all other initializations you should do in viewDidLoad
It seems to me, that the problem is here:
[self presentModalViewController: navigationController animated:YES];
Instead try do this:
[self presentModalViewController: addController animated:YES];

How to remove a Subview from navigationController?

I added a toolBar with this code:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *yesterday = [[UIBarButtonItem alloc]initWithTitle:#"Yesterday"
style:UIBarButtonItemStyleBordered target:self action:#selector(yesterday:)];
UIBarButtonItem *today = [[UIBarButtonItem alloc]initWithTitle:#"Today"
style:UIBarButtonItemStyleDone target:self action:#selector(today:)];
UIBarButtonItem *tomorrow = [[UIBarButtonItem alloc]initWithTitle:#"Tomorrow"
style:UIBarButtonItemStyleBordered target:self action:#selector(tomorrow:)];
UIBarButtonItem *month = [[UIBarButtonItem alloc]initWithTitle:#"Month"
style:UIBarButtonItemStyleBordered target:self action:#selector(month:)];
NSArray *items = [NSArray arrayWithObjects:yesterday,today,tomorrow,month, nil];
[yesterday release];
[today release];
[tomorrow release];
[month release];
UIToolbar *toolbar = [[UIToolbar alloc] init];
[toolbar sizeToFit];
[toolbar setFrame:CGRectMake( 0, 20, 320, 40)];
[toolbar setItems:items];
[self.navigationController.view addSubview:toolbar];
}
but when i change the view using the navigation controller the toolbar stays there...
how can i remove that subview?
UINavigationController has a toolbar built in which is hidden by default. You can display it using [navigationController setNavigationBarHidden:animated:];. You might want to use that instead. Then, before you push a view controller, set that view controller's hidesBottomBarWhenPushed property to true.
The reason your toolbar doesn't go away in this instance is that you're adding it to navigationController's view which displays on top of other views it controls. You could instead add it as a subview of self.
To answer your specific question, though, to remove the toolbar from any superview, use [toolbar removeFromSuperview]. In this case, I would go with the cleaner solution of using the toolbar that is built into navigation controllers.