使用 UIAlertView 防止更改 Tabbar 视图控制器
我试图让当用户按下选项卡栏项目时调用 UIAlertView,询问是否真的想更改实际选项卡,问题是 UIAlertView 在获得答案之前不会停止代码,代码会继续运行并根据先前的值更改视图控制器或不更改,而不是实际值。
我试图等待一段时间才能得到答案,但屏幕只会变暗,而且警报也没有弹出。我还阅读了这篇文章暂停代码执行直到 UIAlertview,我尝试过,但无法使其工作,有人可以帮忙吗,谢谢!
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
return boolean_var;
}
return YES;}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}
I'm trying to make that when the user press a tabbar item an UIAlertView gets called, asking if really wants to change the actual tab, the problem is that the UIAlertView doesn't stop the code until getting the answer, the code keeps running and depending on the previous value change the viewcontroller or not, not the actual.
I've tried to wait to the answer with a while, but the screen only gets darker and the alert didn't popup. I also read this post pause code execution until UIAlertview, I tried but i was unable to make it work, can someone help, thanks!
- (BOOL) tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
if (([self Myfunction]) && (viewController != [tabBarController.viewControllers objectAtIndex:0])){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"text1" message:@"text2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
[alert release];
return boolean_var;
}
return YES;}
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex == 0) [self setBoolean_var:NO];
else [self setBoolean_var:YES];}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确定需要显示警报的视图控制器,并将其保存在
candidateViewController
中,并返回NO
以延迟切换。根据警报视图上的响应,您应该更改它。最后一种方法假设了一些事情。您的标签栏控制器由
self.tabBarController
引用,并且您设置boolean_var
将其返回到之前的方法。警报视图在该方法中是非阻塞的,因此使用boolean_var
是没有意义的。Identify which view controller that you need to show alert for and save it in
candidateViewController
and returnNO
to delay the switch. Based on the response on the alert view, you should change it.The last method assumes few things. Your tab bar controller is referenced by
self.tabBarController
and that you were settingboolean_var
to return it to the earlier method. Alert view is non blocking in that method so usingboolean_var
is pointless.