我在ViewController2中有一个backbarButtonItem,当屏幕返回到其父视图控制器ViewController1时,我想问用户是否准备好返回。如果他还没准备好,我想给他留在ViewController的选择权。所以要问这样的问题--“你准备好离开这个屏幕了吗?-是还是否”
我知道backbarButtonItem不能像苹果文档中定义的那样调用'action‘。你有什么好的解决方案吗?
发布于 2013-04-03 11:59:57
我会将您自己的自定义leftBarButtonItem添加到导航项中,并向其中添加您想要的任何操作方法。它不会有与标准back按钮相同的外观,但我认为这是一件好事--用户期望标准按钮将具有标准行为。使用常规的矩形按钮将提醒用户发生了一些不同的事情。
发布于 2013-04-03 13:26:38
在视图didLoad方法中写入以下内容:
UIImage* myimage = [UIImage imageNamed:@"ButtonImage.png"];
CGRect backFrame = CGRectMake(0, 0, 80, 30);
backButton = [[UIButton alloc] initWithFrame:backFrame];
[backButton setBackgroundImage:myimage forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(MyBtnclicked)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.leftBarButtonItem = btn;然后编写ibaction,如下所示
-(IBAction)MyBtnclicked
{
UIAlertView *av=[[UIAlertView alloc] initWithTitle:nil message:@"Do you really want to Go back" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[av show];
}发布于 2013-04-03 11:55:50
一种方法是继承UINavigationController对象的子类并覆盖popViewController:animated:方法。然后,您可以根据用户的响应来决定是否调用super的popViewController:animated:。
https://stackoverflow.com/questions/15778531
复制相似问题