[英]how to stop view disappearing when navigation controller left bar button pressed
I am rookie to iOS .In my child view controller i made some modification in data. 我是iOS的新手。在我的子视图控制器中,我对数据做了一些修改。 I inserted a done button to store a data and pop the view controller from navigation controller stack.
我插入了完成按钮来存储数据,然后从导航控制器堆栈中弹出视图控制器。 if i press navigation controller back button it automatically goes to back without saving data.
如果我按下导航控制器的后退按钮,它将自动返回到后退而不保存数据。 if i made any modification in data and then i pressed back button i need show alert as "modifications are made are sure want to go back".
如果我对数据进行了任何修改,然后按了返回按钮,则需要显示警报,因为“所做的修改肯定要返回”。 if the user press the cancel button in alert view i need to stop view disappearing and still stand on same view controller.If anybody have the answer please help me.
如果用户在警报视图中按“取消”按钮,则我需要停止视图消失并仍然站在同一视图控制器上。如果有人有答案,请帮助我。
I think this is a way to do it 我认为这是一种方法
- (void)viewDidLoad{
[super viewDidLoad];
[self.navigationItem setLeftBarButtonItem:nil];
[self.navigationItem setHidesBackButton:YES];
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
[myButton setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
//back.png = your image name
[myButton setTitle:@"Back" forState:UIControlStateNormal];
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(backButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBackButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[self.navigationItem setLeftBarButtonItem:customBackButton];
}
- (void)backButtonTapped:(id)sender{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message" message:@"modifications are made are sure want to go back" delegate:self cancelButtonTitle:@"Go Back" otherButtonTitles:@"Stay Here", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == [alertView cancelButtonIndex]){
[self.navigationController popViewControllerAnimated:YES];
}else{
//Stay on the page and do something
}
}
Don't forget to add <UIAlertViewDelegate>
不要忘记添加
<UIAlertViewDelegate>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.