PDA

View Full Version : custom back button action




iphoneSDKrules
Jan 19, 2009, 05:11 PM
Hi!

I'm trying to create a custom action, that should be called when the back button in the navigationbar is pressed.

The structure of my app consists of three viewcontrollers pushed onto the stack of a navigationcontroller:
1. Root ViewController
2. FirstViewController
3. SecondViewController

Whenever the user tries to go back from SecondViewController to FirstViewcontroller, i want to show an AlertView asking if he really wants to go back (i know, that's more ms windows style, but i really need it...).

I tried to create a custom backbutton for FirstViewController in its init-method:


UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithTitle:@"myButton" style:UIBarButtonItemStyleBordered target:self action:@selector(backAction:)];
self.navigationItem.backBarButtonItem = button;


the backAction invoked by the button should just write a message and show an AlertView:

- (void)backAction:(id)sender
{
NSLog(@"BACKBUTTON WAS PRESSED!!!");
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you really want to go back?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
[alert release];
}


Unfortunately neither the AlertView nor the console output is ever shown.
Could someone please help me and tell me what I'm doing wrong?

thanks, kate



Tarun Gupta
Jan 20, 2009, 03:11 PM
Hi!

I'm trying to create a custom action, that should be called when the back button in the navigationbar is pressed.

The structure of my app consists of three viewcontrollers pushed onto the stack of a navigationcontroller:
1. Root ViewController
2. FirstViewController
3. SecondViewController

Whenever the user tries to go back from SecondViewController to FirstViewcontroller, i want to show an AlertView asking if he really wants to go back (i know, that's more ms windows style, but i really need it...).

I tried to create a custom backbutton for FirstViewController in its init-method:


UIBarButtonItem* button = [[UIBarButtonItem alloc] initWithTitle:@"myButton" style:UIBarButtonItemStyleBordered target:self action:@selector(backAction:)];
self.navigationItem.backBarButtonItem = button;


the backAction invoked by the button should just write a message and show an AlertView:

- (void)backAction:(id)sender
{
NSLog(@"BACKBUTTON WAS PRESSED!!!");
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you really want to go back?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES", nil];
[alert show];
[alert release];
}


Unfortunately neither the AlertView nor the console output is ever shown.
Could someone please help me and tell me what I'm doing wrong?

thanks, kate


Hi,

Dude you are goin right but you just need to import an CoreGraphics.framework so as to make use of CGAffineTransform class. And after doing this try this code.


- (void)backAction:(id)sender
{
UIAlertView* alert =[[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you really want to go back?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];

CGAffineTransform myTransForm = CGAffineTransformMakeTranslation((CGFloat)0.0, (CGFloat)90.0);
[alert setTransform:myTransForm];
[alert show];
}

drivefast
Jan 20, 2009, 10:15 PM
I tried to create a custom backbutton for FirstViewController in its init-method:


um... dudette... :cool: what bothers me at a first look is what i bolded in the quote. you sure you didnt actually created the button in the second view controller? also, does your button say "myButton" on it? if it does, it means that you have indeed created it properly and the action function is set as it should.

iphoneSDKrules
Jan 21, 2009, 06:56 AM
yes, the button is actually created on initialization of FirstViewController and says "myButton" like it should.
But my problem is, that the backAction seems to NEVER be called. why is that?

dejo
Jan 21, 2009, 09:22 AM
According to the Developer Documentation for UINavigationItem's backBarButtonItem: "The target and action of the back bar button item you set should be nil." So, seems to me that even if you set it, it will be ignored.

Perhaps you should try hiding the backBarButtonItem and using a custom leftBarButtonItem instead.

P.S. I just gotta say that overriding the back with an "Are you sure?" prompt sounds wrong and might go against the Mobile HIG.

Pring
Jan 21, 2009, 09:46 AM
I'd scrap your approach and perhaps do the following.. I'm assuming you want to ask the user if they really want to go back as they may have unsaved changes to a form?

In the viewWillDisappear save the values of the form somewhere. When the view appears again (ie if the user clicks to go back) bring up a prompt asking them if they wish to use the auto saved values or whether they wish to reset the form.

iphoneSDKrules
Jan 22, 2009, 05:42 AM
I'd scrap your approach and perhaps do the following.. I'm assuming you want to ask the user if they really want to go back as they may have unsaved changes to a form?

In the viewWillDisappear save the values of the form somewhere. When the view appears again (ie if the user clicks to go back) bring up a prompt asking them if they wish to use the auto saved values or whether they wish to reset the form.

ok, i'm going to try it the other way round. i didn't even think of this possibility but now when i come to think of it, it really sounds more logic :)

dejo
Jan 22, 2009, 10:24 AM
In the viewWillDisappear save the values of the form somewhere. When the view appears again (ie if the user clicks to go back) bring up a prompt asking them if they wish to use the auto saved values or whether they wish to reset the form.
I'd take it a step further: Just automatically restore the saved values but provide the option to reset the form via a button, avoiding the prompt altogether. This gives the user the option to reset the form at any point rather than only when the view first appears.