Problem over-riding the Back button in iOS - iphone

I have a UITableViewController A that is pushing UITableViewController B onto the stack.
In A i have the code:
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)];
backButtonClicked: is also implemented.
B has the title Trending, but when I click it, it doesn't ever reach backButtonClicked:
Why is this?

Try either setting the delegate:
[navigationController setDelegate:self];
Or using the left button. Sometimes the back button doesn't work with certain views and I have had to use the left button instead:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
Also, you can try setting B's button item instead of A:
viewControllerB.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];

In the Xcode documentation, it states that the backBarButtonItem target and action should be set to nil. So even if you do set it, it's probably a good bet that it will be ignored. You could check out the link below to add custom behaviour to the back button.
Custom Action on Back Button Item
Or you could just do the following in viewControllerB:
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Trending"
style:UIBarButtonItemStylePlain
target:self
action:#selector(backButtonClicked:)] autorelease];
Then also add this to viewControllerB
- (void)backButtonClicked:(id)sender {
[[[self.navigationController viewControllers] objectAtIndex:0] backButtonClicked:sender];
[self.navigationController popViewControllerAnimated:YES];
}
The above method will find the RootViewController and send it the backButtonClicked message. It will then pop the current view controller, which should allow you to emulate the backBarButtonItem. Also you can change which view controller you want to send the message by changing the value in the objectAtIndex method.

try not backBarButtonItem but leftBarButtonItem instead
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.hidesBackButton=YES;
for me it works like a charm. And don't forget that you didn't release this button - and it can cause memory leak. So you can add autorelease when you assign you button or make it like this
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Trending" style:UIBarButtonItemStylePlain target:self action:#selector(backButtonClicked:)];
self.navigationItem.leftBarButtonItem = myBackButton;
[myBackButton release];
self.navigationItem.hidesBackButton=YES;

Related

How to make navigation button with Login and Setting title

I want to add two buttons two navigation bar on right hand side one for settings and one for login but problem is that only one button i have searched comes on right that is edit is there any other way in which we can make two button and give them desired title.
UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(#"Send") style:UIBarButtonItemStyleBordered target:self action:#selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = #[addAttachButton,sendButton];
Here is the code I am using in My UIViewController's implementation file (.m file)
-(void)viewDidLoad{
[super viewDidLoad];
//back button
UIBarButtonItem *backButton = [[UIBarButtonItem alloc]initWithTitle:#" Back " style:UIBarButtonItemStyleBordered target:self action:#selector(backTo:)];
//Optional: if you want to add space between the back & login buttons
UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixedSpaceBarButtonItem.width = 12;
//login button
UIBarButtonItem *signIn_BarButton = [[UIBarButtonItem alloc]initWithTitle:#" SIGN IN " style:UIBarButtonItemStyleBordered target:self action:#selector(signInUser)];
//add all buttons to right side
self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:backButton, fixedSpaceBarButtonItem,signIn_BarButton, nil];
}
now here are the metthods for both button clicks
-(IBAction)backTo:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}
-(void) signInUser{
//handle your sigin logic here
}
Please! let me if you need more help!

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

bug in my BarButton

I have a bug in my code and I cannot seem to find it! I've created a button called addButton programatically and set its selector to add—but the app crashes in the simulator every time the button is pressed. Here is my code.
-(void)viewDidLoad{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
and the code for the add button is :
- (IBAction)add
{
MyDetailViewController * detail = [[MyDetailViewController alloc]init];
detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:detail animated:YES];
//[detail.text becomeFirstResponder];
[detail release];
}
Thanks for the help guys :D
Your add selector has a colon at the end which means it is trying to use an add method with a parameter but your add method does not expect a parameter object. You need to remove the colon from your selector by changing your Bar Button Item allocation to this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add)];

iphone : unable to show back button on navigation bar

please help me with this
this is really erratic I am unable to add a button to navigation bar of modalview
UINavigationController *tempModalVC=[[UINavigationController alloc] init];
[tempModalVC.navigationBar setBarStyle:UIBarStyleBlack];
//UIBarButtonItem *tempDoneBTN=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(hideModalView:)];
UIBarButtonItem *tempDoneBTN=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(hideModalView:)];
[tempModalVC.navigationItem setBackBarButtonItem:tempDoneBTN];
[tempModalVC.navigationItem setTitle:#"Title"];
tempModalVC.navigationItem.backBarButtonItem.enabled=YES ;
//[tempModalVC.navigationBar setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:self action:#selector(hideModalView:)]];
//[tempModalVC.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStyleBordered target:self action:#selector(hideModalView:)]];
[tempDoneBTN release];
[tempModalVC.view addSubview:mapView];
tempModalVC.modalPresentationStyle = UIModalPresentationFormSheet;
Please help me with this I have tried almost every permutation combination.
Thnx in advance
It looks as if you want to display a mapView in a modal view.
On iPhone, modal views occupy the entire screen, so there is no room for a nav bar. Thus you do not need a navigation controller.
I assume the above code is inside a UIViewController instance. If so, just do this:
[self presentModalViewController:mapView animated:YES/NO];
In this case, in your your mapView controller, you should add a button that, when pressed does this:
[self dismissModalViewControllerAnimated:YES/NO];

need help about add navigationItem

As you see in the images, the top picture is created automatically when navigation controller is pushed
If I try to create one like this it will appear like the bottom picture.
How can I programmatically create a Back Button like the top picture?
Here is my code to create the Done button
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(backtohome)];
UIBarButtonItem *theBackButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem = theBackButton;
[theBackButton release];