Iphone Uitoolbar next button - iphone

Here is what I did, this is a dynamic questionary application;
-Created a UINavigationBased application
-Created some new UIVIewController subclasses with their xib's and designed them.
-I opened mainwindow.xib and set the navigationcontroller to has a navigation bar and a toolbar in black.
-So now when I switch back and forth between views by pushing them with the navigation controller I can see that shared toolbar and a navigationbar in every page with a backbutton, which is cool.
some of my uiviews are tableviews and some are textboxes
What I want is I want to put a "next" button on that toolbar and be able to go through my views but not with getting the row tap. With each next click I will decide which view to push.
How can I put this "shared" button? and in which class can I define its functionality? also I want to make it sometimes invisible for instance on first page and on last page of questionary.

In every page that you want to have a next button add this in the init or viewDidLoad
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Next"
style:UIBarButtonItemStylePlain
target:self
action:#selector(pushNextView)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
Now just implement pushNextView method to push what ever view you want
-(void)pushNextView{
//push next view
}
Edit
By specifying the target of your rightButton to AppDelegate (right now it points to self) you can setup the pushNextView method in your AppDelegate and manage all your views there.

Related

NavigationController UINavigationBar Back Button

I'm trying to change the text of the back button from within Interface Builder, but when I select the Navigation Bar and go into the Attributes Inspector and set the Back Button text to "Close" it still shows up with the title of the previous view.
How do I get this to work?
The back button will always show the previous UIViewController title or backBarButtonItem defined.
So if you have "view1" and move to "view2" you need to set the backButton in "view1" so it's displayed correctly while "view2" is presented.
(In other word "view1" is responsible to provide what should be displayed in a back button that point to it)
In this way if "view1" is follow by any views all those will have the correct back button.
Try this in -(void)viewDidload, as this method is fired after the nib has loaded:
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Close" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarButtonItem;
[backBarButtonItem release];
A quick and dirty trick is to set the "title" to what name you want in the button in the willDisappear override in the view controller. Make sure to set the "title" back to the correct name on the willAppear override view controller. One drawback with this technique is that on slower units you can see it change. On iPhone 4 and later it's hardly noticeable.
Hope this helps.

Switch between different 'child' view controllers using a toolbar and its buttons

Does anyone have any tips for this scenario.
My app delegate's nib has a viewcontroller set as the rootviewcontroller, so it loads this view controller when the app loads.
This viewcontroller has a toolbar with various buttons. These buttons are meant to switch between different view controllers.
I have tried using addChildViewController, presentViewController, presentModalViewController nothing allows me to switch between view controllers BUT still keep the toolbar visible.
If I use addSubView then all the orientation stuff goes mental and I have to resize the view controller manually which doesn't seem like something I should be doing.
EDIT: I want to keep the nav controller's button visible even when pushing controllers on i.e. if I have an EDIT and DELETE button I want those same buttons to remain on the toolbar even when I push different controllers onto the stack
It sounds like you should be using a tab bar.
Alternatively, you should be using a navigation controller with a toolbar and push/pop view controllers on this when the toolbar buttons are pressed:
Please note the navigation bar does not have to be visible if you use a navigation controller.
//create first button
buttonOne = [[UIBarButtonItem alloc] initWithTitle:#"EDIT" style:UIBarButtonItemStyleBordered target:self action:#selector(editStuff)];
[buttons addObject:buttonOne];
//create second button
buttonTwo = [[UIBarButtonItem alloc] initWithTitle:#"DELETE" style:UIBarButtonItemStyleBordered target:self action:#selector(deleteStuff)];
[buttons addObject:buttonTwo];
// Add buttons to toolbar and toolbar to nav bar.
[buttonsToolbar setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:buttonsToolbar];
self.navigationItem.leftBarButtonItem = twoButtons;
[twoButtons release];
addSubview: doesn't permit use of a secondary view controller, so that's not ideal.
You can use a toolbar with bar buttons to switch view controllers, but the simplest implementation is to have identical toolbars in each view controller's nib, and make the view controllers subclasses of a superclass that handles all of the toolbar actions.
There is no need to use a tab bar or navigation bar, although either of these would be a simpler approach in someways (but less obvious in the ways that matter).
Use a UITabBarController as your root controller. This acts as a parent container for your child viewcontrollers, provides a tab bar and implements switching between child views. Check out the class reference or the View Controller Programming Guide

Disabling back button and put Cancel button in navigation view controller -- like contacts iPhone application

I have a navigation view controller and there are 3 view controllers in the navigation stack. Now on the third and top most visible view controller I have a default back button coming in.
I need to bring this view controller in edit mode which I did... Now the requirement is to have a cancel button as the left bar button item instead of back button.
This is similar to the functionality given by contacts application of iPhone where you edit a particular contact.
Any clue how to achieve this?
To hide back button and add a left bar button use-
[self.navigationItem setHidesBackButton:TRUE];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector()];
[self.navigationItem setLeftBarButtonItem:leftBarButton];
[leftBarButton release];
And then to programmatically return to the previous view controller, you can do-
[self.navigationController popViewControllerAnimated:YES];
This is a simpler way:
[self.navigationItem setHidesBackButton:YES];
If you are using storyboard, you can also just drag a bar button item onto the navbar where the back button would normally show up. This will override it.

Can you manually add to an iPhone/iPad Navigation Bar a back button item?

I have a reference to a "UIBarButtonItem", is there a way I can add a custom "Back Navigation Button" to that item when it is not part of a Navigation based view?
I can add a left button:
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
initWithTitle:#"Custom Back"
style:UIBarButtonItemStylePlain target:self
action:#selector(backAction:)];
menuItem.backBarButtonItem = backButton; //This doesn't seem to work.
menuItem.popOverNavigationItem.leftBarButtonItem = backButton; //This shows a normal button
So how could I make the leftmost button look like a back navigation button?
UPDATE: This other question answered the root of my problem that was leading me to try and do this non-standard UI setup:
iPad: Merge concept of SplitViewController and NavigationController in RootView?
I think the correct way to set the back button is to set it for the view controller that you would be going back to. For example:
RootViewController > DetailViewController
If you want the back button to say "Custom Back" whilst you're on DetailViewController, you have to actually set RootViewController's back button to "Custom Back".
Hope that makes sense.
Having just struggled with this: I think the answer is “no.” You'll need a controller hierarchy.

What happens when back button is pressed in navigationBar

I wonder what is the function is called when the back button is pressed on the navigationBar.
I want to add some functionality when the button is pressed, who knows it?
Thanks in advance
The functionality you want is in the UINavigationBarDelegate protocol. Implement the -navigationBar:shouldPopItem: method and set your class as the delegate of the navigation bar in question.
Assuming you're referring to native controls, there's no way to do quite what you want, just using the built-in stuff. What you want to do is create a 'fake' back button, and stick it up in the left side of the navigation bar. Then you can set its target and action to whatever you like.
I suppose you're talking about the back button that automatically is added to a UINavigationBar when you push a new viewcontroller on a navigationcontroller.
The default action for the back button is to pop the current viewcontroller from the navigation stack and return to the previous viewcontroller.
If you want to define a custom behaviour to the backbutton you'll have to create a new button and tie a selector to it's action property:
//Create a new barbutton with an action
UIBarButtonItem *barbutton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleDone target:self action:#selector(doSomething)];
// and put the button in the nav bar
self.navigationItem.leftBarButtonItem = barbutton;
[barbutton release];
Edit:
//An example on how the doSomething method could be implemented.
-(void) doSomething
{
//Do your custom behaviour here..
//Go back to the previous viewcontroller
[self.navigationController popViewControllerAnimated:YES];
}