custom back button action

Discussion in 'iPhone/iPad Programming' started by iphoneSDKrules, Jan 19, 2009.

  1. macrumors newbie

    Joined:
    Aug 21, 2008
    #1
    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:
    UIBarButtonItembutton = [[UIBarButtonItem allocinitWithTitle:@"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!!!");
        
    UIAlertViewalert = [[UIAlertView allocinitWithTitle:@"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
     
  2. macrumors newbie

    Joined:
    Jan 20, 2009
    #2

    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];
    }
     
  3. macrumors regular

    Joined:
    Mar 13, 2008
    #3
    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.
     
  4. thread starter macrumors newbie

    Joined:
    Aug 21, 2008
    #4
    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?
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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.
     
  6. macrumors 6502

    Joined:
    Sep 17, 2003
    #6
    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.
     
  7. thread starter macrumors newbie

    Joined:
    Aug 21, 2008
    #7
    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 :)
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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.
     

Share This Page