2014-02-10 41 views
0

因此,我正在为上学期的项目制作一个学校的照片编辑应用程序。当用户点击后退按钮(在编辑模式下),在我的情况下称为“产品选择”,我想弹出一个提示,并说“你想删除所有内容并返回到产品选择? “看看他们是否想放弃他们的工作,如果用户选择是,则整个项目被丢弃,并且他们被放回大厅。如何在退出视图时创建弹出窗口?

我在哪里可以做到这一点?我在最小故事板中找到了“产品选择”按钮,但不知道该从哪里做什么。

的弹出我会使用的代码是:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" 
message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 
    [alert show]; 
    [alert release] 

任何帮助/智慧将非常感激!

回答

2

我想创建一个“取消” UIBarButton,其执行自定义函数:

- (void)cancelTapped { 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 
    [alert setTag:1]; 
    [alert show]; 
} 

并监听警示返回上: - alertView:didDismissWithButtonIndex:

从那里,我会写逻辑隐藏该页面并弹出视图。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if (alertView.tag == 1 && buttonIndex == 1) { 
     // Delete data and return to lobby 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 
+0

我喜欢你的解决方案(和你的编辑)@Ramon,而是一个建议为@ user3275​​721 ...我'd将标签添加到警报中,以便专门针对该警报执行didDismissWithButtonIndex操作。 –

+1

好建议@LyndseyScott!我已经做出相应的修改。 – Ramon

+0

我也有一个想法。如果我只在编辑器页面上隐藏“产品选择”按钮并显示如上所述的“取消”按钮,该怎么办?但是,我可以做什么文件/位置? – Camerz007

0

使用该委托的方法(不要忘了在你的.h加入UIAlertViewDelegate):

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:@"Do you want to delete everything and go back to product selection?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes"]; 
alert.cancelButtonIndex = 0; 

[alert show]; 



-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 

    // If user confirmed: 
    if (buttonIndex != 0) { 

    // Do what you need. 
    } 
} 
+0

谢谢你的时间! – Camerz007