如何给导航栏上的backButtonItem设置action?
如何给导航栏上的backButtonItem设置action?我有一个导航栏,当我按下后退按钮时,我需要向用户提醒一些消息,并且只有在用户做出反应后才返回到上一个视图。我该怎么做呢?谢谢!
- (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];
}
How to set action to the backButtonItem on the navigation bar? I have a navigation bar, when I'm pressing the back button, I need to alert some message to the user, and only after user's reaction - return to the previous view. How can I do it? Thanx!
- (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];
}
//and my method for reaction
-(IBAction) backBtnUserClick
{
NSLog(@"\n Back pressed");
//back to previous view
[self.navigationController popViewControllerAnimated: TRUE];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这听起来像是 UIAlertView 的工作。不要在
IBAction
方法中调用 popViewControllerAnimated:,而是分配/初始化一个UIAlertView
并呈现它。然后,当用户点击UIAlertView
上的按钮时,关闭UIAlertView
并调用popViewControllerAnimated:
。在您的
UIAlertViewDelegate
方法中调用popViewControllerAnimated:
。要设置后退按钮的操作:
This sounds like a job for
UIAlertView
. Instead of calling popViewControllerAnimated: in yourIBAction
methods, alloc/init aUIAlertView
and present it. Then, when the user taps a button on theUIAlertView
, dismiss theUIAlertView
and callpopViewControllerAnimated:
.In your
UIAlertViewDelegate
methods callpopViewControllerAnimated:
.To set the action on the back button:
使用 iOS 16 或更高版本时,您可以通过设置导航栏的
backAction
来插入自定义逻辑。例子:When using iOS 16 or newer, you can insert custom logic by setting the
backAction
of the navigation bar. Example:添加< UINavigationControllerDelegate >在头文件中并在 .m 中使用它
Add the < UINavigationControllerDelegate > in the header file and use this in the .m