【问题标题】:how to navigate a view controller after pressing UIAlert button按下 UIAlert 按钮后如何导航视图控制器
【发布时间】:2016-01-13 06:50:10
【问题描述】:

我有两个屏幕。从第一个屏幕到下一个屏幕,我在情节提要中连接push segue。现在在我的第二个屏幕中,将显示一个带有确定按钮的警报。按确定按钮后,我需要转到我的第一个屏幕

这是我的代码:

- (void)signup:(NSDictionary *)params {
    if (error)
    {
        [PCUtilities showAlertWithTitle:@"Oops" message:error.localizedDescription cancelButtonTitle:@"ok"];
    }
}

在一种方法中我有UIAlert。

下面的代码我试过了,但是不行!!

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"ok"])
    {
        [self.navigationController pushViewController:[self.storyboard instantiateViewControllerWithIdentifier:@"AppMainNavController"] animated:YES];
    }
}

AppMainNavController = 是我的导航控制器故事板 ID

【问题讨论】:

标签: ios objective-c


【解决方案1】:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if  (buttonIndex==0)
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

【讨论】:

    【解决方案2】:

    只需弹出到上一个视图控制器,如下所示。它将弹出到您从中推送的上一个视图控制器。

            - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
            {
                NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
                if([title isEqualToString:@"ok"])
                {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            }
    

    【讨论】:

    • 你需要在方法内部为 UIAlertView 设置 delegate:self 并将 UIALertViewDelegate 添加到你的 .h 文件中
    • 没得到你??我在我的.h 中添加了uialertdelegate。以及在我的` - (void)signup:(NSDictionary *)params {` method ??/ 中设置委托的内容
    • 在方法内的警报视图代码中更改为 delegate:self 而不是 delegate:nil。
    【解决方案3】:

    如果应用程序有多种语言,使用按钮索引而不是按钮标题会很困难。如果您使用的是 push segue,那么只需关闭当前视图控制器,它将自动转到上一页,天气有 Push segue 或 modal segue。

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        if(buttonIndex == 0)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
    

    【讨论】:

    • 仍然在我的第二个视图中..不会回到我的第一个视图...我正在使用show(e.g push) segue
    • 在我的第二个视图控制器中仍然没有......不会回去
    • 故事板文件意味着..我没有太多更新图像的意义??
    • 我有一个主屏幕和第二个屏幕都连接到一个导航控制器。从我的第一个到第二个视图控制器,它会像 `show(eg. push)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2012-01-01
    • 2013-07-31
    • 2012-11-07
    相关资源
    最近更新 更多