Regarding navigationcontroller - iphone

Here in my application i created a leftbarbuttonitem programatically in viewWillAppear methos, i have two click twice to work this, my code is as follows.
Code for creating UIBarButtonItem(left bar button)
goingBackButton = [[UIBarButtonItem alloc] init];
goingBackButton.title = #"Back";
goingBackButton.target = self;
goingBackButton.action = #selector(backAction);
self.navigationItem.leftBarButtonItem = goingBackButton;
[goingBackButton release];
Action code
- (IBAction) backAction {
NSLog(#"Inside the backAction of uploadViewController");
[self.navigationController popViewControllerAnimated:YES];
NSLog(#"Inside the backAction1 of uploadViewController");
}

Try the below code:
UIBarButtonItem *bar = [[UIBarButtonItem alloc]initWithTitle:#"back" style:UIBarButtonItemStyleDone target:nil action:#selector(back)];
self.navigationItem.leftBarButtonItem = bar;
[bar release];
-(void)back
{
[self.navigationController popViewControllerAnimated:YES];
}

Related

why setBackBarButtonItem not work in the childViewController viewDIdLoad?

I want to custom the backBarButtonItem in the navigationItem,
here is my code
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:back];
}
but it didn't work while I used the leftBarButtionItem can work.
I don't know ,can anybody give me an answer?
- (void)viewDidLoad
{
[self.navigationItem setHidesBackButton:YES];
UIBarButtonItem *cancelNavButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(dismissController)];
[self.navigationItem setLeftBarButtonItem:cancelNavButton];
}
- (void)dismissController
{
[self.navigationController popViewControllerAnimated:YES];
}
You shouldn't have to create the back button yourself if you are pushing your view controllers using the UINavigationController's pushViewController:animated: method. This will push the new ViewController on the NavigationControllers stack and give you an automatic back button with the title of the previous ViewController.
The reason your button doesn't work is the target: and action: parameters are nil you could bind them to the UINavigationController's popViewControllerAnimated: method to achieve the same action as the default back button.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:[self navigationController]
action:#selector(popViewControllerAnimated:)
];
[[self navigationItem] setBackBarButtonItem:back];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.hidesBackButton = YES;
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
self.navigationItem.leftBarButtonItem = back;
}

Adding back button to a view controller

I have created 3 UIViewControllers for my 3 UIToolBar items but i am not able to create a button on each controller which takes me back to the home page or main view controller. Kindly help me as i am new to objective c.
Here is my code....
"ViewController.h"
#import "ViewController.h"
#import "ContactInfoViewController.h"
#import "DateViewController.h"
#import "MessageViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 417, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc] initWithTitle:#"Contact" style:UIBarButtonItemStyleDone target:self action:#selector(ContactButton)];
UIBarButtonItem *flexiableItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *date = [[UIBarButtonItem alloc] initWithTitle:#"Date" style:UIBarButtonItemStyleDone target:self action:#selector(DateButton)];
UIBarButtonItem *flexiableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithTitle:#"Message" style:UIBarButtonItemStyleDone target:self action:#selector(MessageButton)];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,flexiableItem1,date,flexiableItem,message, nil];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
-(void)ContactButton
{
ContactInfoViewController *secondViewcont = [[ContactInfoViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)DateButton
{
DateViewController *secondViewcont = [[DateViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
-(void)MessageButton
{
MessageViewController *secondViewcont = [[MessageViewController alloc] init];
UINavigationController *navigCont = [[UINavigationController alloc] initWithRootViewController:secondViewcont];
[self presentViewController:navigCont animated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
Add custom button to your second view controller
In viewDidLoad you can do like this
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonPressed:)]autorelease];
- (IBAction)doneButtonPressed:(id)sender
{
[self dismissViewControllerAnimated:YES completion:nil];
}

navigation bar is missing

Navigation bar is missing while pushing kalviewcontroller on button click.How to add navigation bar? so that it can be pop to view controller where its being pushed on button click.
kal = [[[KalViewController alloc] init]autorelease];
kal.title = #"Calendar of game";
kal.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(addEvent)] autorelease];
kal.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered target:self action:#selector(backToPrevious)]
autorelease];
kal.delegate = self;
dataSource = [self init];
kal.dataSource = dataSource;
[self.navigationController pushViewController:kal animated:YES];
Put [self.navigationController setNavigationBarHidden:FALSE] in you viewDidLoad(assuming you have created navigation controller).

Adding a Right "done" Button (UIBarButtonItem) to a UINavigationController

I see that a similar question was asked here: How to add a right button to a UINavigationController? (among others) but its not quite what I'm looking to do and they arent solving my problem.
Essentially, I've created a UIViewController called WebViewViewController with a UIWebView on it which will be shown using presentModalViewController. Essentially its a mini web browser to display a web page while keeping the user in the app rather than launching Safari.
The viewController does the following to get it to show... and the "done" button is meant to provide a place to close the browser.
-(IBAction)visitFacebook {
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
[navigationController.navigationItem setRightBarButtonItem:doneButton animated:YES];
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Unfortunately the "done" button isnt showing.. any ideas where im going wrong?
Try with below
-(IBAction)visitFacebook{
WebViewViewController *rootController = [[WebViewViewController alloc] init];
rootController.webURL = #"http://www.facebook.com/";
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootController];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(done:)];
rootController.navigationItem.rightBarButtonItem = anotherButton;
[navigationController.navigationItem setTitle:#"Facebook"];
if (rootController) {
[self presentModalViewController:navigationController animated:YES];
}
[doneButton release];
[rootController release];
}
Perhaps you're looking for something more like this:
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleDone target:self
action:#selector(dismissModalViewControllerAnimated:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:#selector(done:)];
Just this one line code displayed done button for me.

trying to programmatically create rightBarButtonItem

This doesn't seem to be working. What am i doing wrong?
-(void)awakeFromNib{
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(showNewEventViewController)];
self.navigationItem.rightBarButtonItem = rightBarButtonItem;
NSLog(#"awaked");
[rightBarButtonItem release];
}
my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)
YourRootViewController *theRootController = [[YourRootViewController alloc] init];
UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(dismiss)];
theRootController.navigationItem.rightBarButtonItem = btnCancel
[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?
Try this instead:
- (void) initUI {
UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:#"Cancel"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismiss)]autorelease];
self.navigationItem.rightBarButtonItem = btnCancel;
//[btnCancel release]; no need to explicitly release the item
}