ios - UIAlertView 将 Storyboard发送回开始

标签 ios storyboard uialertview

带有 Storyboard 设置的 iPhone 应用程序。初始 View 上的按钮可链接到另一个 View 。我已为该按钮单击添加了一个警报,这样当点击该按钮时, View 就会发生变化,并立即出现警报,询问“您想继续吗?”是 否'

如果他们单击"is",警报就会消失,如果他们单击“否”, Storyboard应返回到开头。

这就是我到目前为止所得到的,它不起作用,什么也没有发生。我已经尝试了多种不同的解决方案,但到目前为止没有任何效果。删除按钮索引检查不会执行任何操作。

实际上,我更希望在 View 更改之前显示此警报,但目前我让它在转场发生后显示,这并不理想,但如果我能让它转回来,它仍然可以接受。

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

   if (buttonIndex==1)     {
        [self.navigationController popViewControllerAnimated:YES];

    }
}

有什么想法吗?

最佳答案

我猜您已经使用 Storyboard上的 segue 将第一个 View 上的按钮链接到第二个 View 。要获得它以便 UIAlertView 在第一个 View 中弹出,然后根据单击"is"或“否”触发转场,您需要删除该转场,然后通过从第一个 View 按住 Ctrl 键拖动再次创建它(不是按钮)到第二个 View 。

为您的按钮创建一个 IBAction(如果您还没有创建的话),方法是按住 Ctrl 键并从您的按钮拖动到 views .h 文件并选择 Action。这应该会自动在 .m 文件中创建方法 - 在该方法中,您需要调用 UIAlertView。

- (IBAction)buttonPressed:(id)sender
{
    [myAlertView show];
}

然后 clickedButtonAtIndexMethod 应该与您之前的类似。不要弹出 Controller (因为 segue 不会自动发生),您需要使用 performSegueWithIdentifier 调用您的 segue。

- (void)alertView (UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // They clicked Yes
    if (buttonIndex==1)     
    {
        [self performSegueWithIdentifier:@"YourSegueName" sender:self];
    }
}

关于未调用 clickedButtonAtIndex 方法,您是否已将 View 设为 UIAlertView 的委托(delegate)?因此,在您的 View .h 文件中,您需要像这样拥有 UIAlertViewDelegate:

@interface MyViewController : UIViewController <UIAlertViewDelegate>

关于ios - UIAlertView 将 Storyboard发送回开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19817814/

相关文章:

android - Xamarin - Navigation.PopModalAsync() 后重新加载页面

ios - Storyboard segue 在 UITableView 的 didSelectRow 之前被调用

swift - 使用快速代码在按钮单击中的自定义 UITableViewCell 中添加 UIAlertController?

ios - 可以自定义IB导航栏中提示字符串的字体吗?

ios - 使用在不同项目中创建的多个 Storyboard并从菜单系统 xcode 加载它们

ios - UIAlertView Action 没有 swift 发生

ios - UIAlertView 中的全文在 iOS 8 中未正确显示

ios - 应用程序显示在 iphone App Store 但未显示在 ipad AppStore

c++ - 在 cocos2d-x ios 的 UITestFieldTest 中使用未声明的标识符

ios - 如何找出 iOS8 上当前使用的键盘?