2

ナビゲーションバーのbackButtonItemにアクションを設定するにはどうすればよいですか?ナビゲーションバーがあります。戻るボタンを押しているときに、ユーザーにメッセージを通知する必要があります。ユーザーの反応があった場合にのみ、前のビューに戻ります。どうすればいいですか?ありがとう!

- (void)viewDidLoad 
{
    [super viewDidLoad];

    //no one field don't changed yet
    isDirty = FALSE;

    //edited user
    //set default values
    newData = [data copy];

    //setting navigation controller rigth button
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"Save"
                                                                style:UIBarButtonSystemItemDone 
                                                                   target: self 
                                                                   action: @selector(saveBtnUserClick)];
    self.navigationItem.rightBarButtonItem = rightButton; 
    [rightButton release];


    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonSystemItemDone 
                                                                  target: self 
                                                                  action: @selector(backBtnUserClick)];

    self.navigationItem.backBarButtonItem = leftButton;
    [leftButton release];
}

//そして私の反応方法

-(IBAction) backBtnUserClick
{
    NSLog(@"\n Back pressed");

    //back to previous view
    [self.navigationController popViewControllerAnimated: TRUE];
}
4

2 に答える 2

11

ヘッダーファイルに<UINavigationControllerDelegate>を追加し、これを.mで使用します

- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem     *)item
{
  //insert your back button handling logic here
  // let the pop happen
  return YES;
}     
于 2012-01-12T20:06:58.830 に答える
3

これはの仕事のように聞こえUIAlertViewます。popViewControllerAnimated:を呼び出す代わりに、IBActionメソッドでalloc / initaを呼び出して表示しますUIAlertView。次に、ユーザーがのボタンをタップしたら、をUIAlertView閉じてUIAlertView呼び出しますpopViewControllerAnimated:

- (IBAction)backBtnUserClicked:(id)object {
    UIAlertView *av = [[[UIAlertView alloc] initWithMessage:@"Wait!"
          delegate:self
               cancelButtonTitle:@"Ok"
               otherButtonTitles:nil] autorelease];
   [av show];
}

UIAlertViewDelegateメソッドで。を呼び出しますpopViewControllerAnimated:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [[self navigationController] popViewControllerAnimated:YES];
}

戻るボタンのアクションを設定するには:

[[[self navigationController] leftBarButtonItem] setTarget:self];
[[[self navigationController] leftBarButtonItem] setAction:@selector(backBtnUserClicked:)];
于 2010-11-17T18:34:23.347 に答える