Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphoneSDKrules

macrumors newbie
Original poster
Aug 21, 2008
9
0
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:

PHP:
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:
PHP:
- (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

macrumors newbie
Jan 20, 2009
1
0
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:

PHP:
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:
PHP:
- (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:mad:"Alert" message:mad:"Do you really want to go back?" delegate:self cancelButtonTitle:mad:"No" otherButtonTitles:mad:"Yes",nil];

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

drivefast

macrumors regular
Mar 13, 2008
128
0
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

macrumors newbie
Original poster
Aug 21, 2008
9
0
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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

macrumors 6502
Sep 17, 2003
310
0
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

macrumors newbie
Original poster
Aug 21, 2008
9
0
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.