Ios 如何使UIAlert按钮按前进到下一个情节提要?

Ios 如何使UIAlert按钮按前进到下一个情节提要?,ios,iphone,xcode,swift,Ios,Iphone,Xcode,Swift,我对Swift(以及一般的编码)和这个网站都很陌生 我现在有点问题。在我的应用程序中,当计时器达到0时,我会发出警报。在该警报中,有两个按钮。一个说“分享”,另一个说“继续”。我希望这样,当用户点击“继续”时,下一个故事板将显示给他们。到目前为止,我按下的任何按钮都将解除警报,但保持在同一情节提要上。(它也会将我按下的按钮打印到控制台上,但这当然只是让我看看) 我该怎么做呢?这是我的密码,以防有人想知道 let alert = UIAlertController(title: "Time's U

我对Swift(以及一般的编码)和这个网站都很陌生

我现在有点问题。在我的应用程序中,当计时器达到0时,我会发出警报。在该警报中,有两个按钮。一个说“分享”,另一个说“继续”。我希望这样,当用户点击“继续”时,下一个故事板将显示给他们。到目前为止,我按下的任何按钮都将解除警报,但保持在同一情节提要上。(它也会将我按下的按钮打印到控制台上,但这当然只是让我看看)

我该怎么做呢?这是我的密码,以防有人想知道

let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in
    NSLog("You pressed button one")
}

let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
    NSLog("You pressed button two")
}

alert.addAction(firstAction)
alert.addAction(secondAction)
presentViewController(alert, animated: true, completion:nil)

试试这个:

// Create the alert controller
    var alertController = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert)

// Create the actions
    var okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
        UIAlertAction in
        NSLog("OK Pressed")
        let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
        let vc = storyboard.instantiateViewControllerWithIdentifier("someViewController") as UIViewController
        self.presentViewController(vc, animated: true, completion: nil)
    }

    var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel) {
        UIAlertAction in
        NSLog("Cancel Pressed")
        //do whatever you want here
    }

// Add the actions
    alertController.addAction(okAction)
    alertController.addAction(cancelAction)

// Present the controller
    self.presentViewController(alertController, animated: true, completion: nil)
如果尚未设置
ViewControllerIdentifier


您需要设置可以在右侧列中找到的标识符值

以下是可能对您有所帮助的代码。创建包含动作的AlertController时,可以在定义动作时提供按钮的动作和样式。在下面的代码中,动作在闭包(块)中,样式定义为
。默认值
。取消

let alert = UIAlertController(title: "Time's Up!", message: "What would you like to do now?", preferredStyle: .Alert)
let firstAction = UIAlertAction(title: "Continue", style: .Default) { (alert: UIAlertAction!) -> Void in

   // Action when you press button goes here 
      print("Here you show next storyboard item.")
  //  Code to push new ViewController. For example : 
      self.navigationController?.pushViewController(newVCInstance, animated: true)
}

let secondAction = UIAlertAction(title: "Share", style: .Default) { (alert: UIAlertAction!) -> Void in
 print("Here you share things.")
    // Code to share things.
}

let thirdAction = UIAlertAction(title: "Cancel", style: . Cancel) { (alert: UIAlertAction!) -> Void in
     print("Just dismissing the Alert Controller.")
}


alert.addAction(firstAction)
alert.addAction(secondAction)
alert.addAction(thirdAction)

presentViewController(警报、动画:true、完成:nil)

欢迎使用SO。到目前为止,您在展示下一个故事板方面做了哪些尝试?您显示的代码似乎都没有与情节提要交互,因此很难对此提供帮助。看到你指的是同一情节提要中的下一个场景(视图控制器)了吗?或下一个场景中的下一个场景(视图控制器)storyboard@jtbandes诚然,我对在我的应用程序中展示下一个故事板不太了解。现在,我所做的是在Main.storyboard中,我控制将一个ViewController中的按钮拖动到另一个ViewController,当小的黑色弹出窗口出现时,我单击modal,就是这样。在属性检查器中,我只选择一些其他动画。这就是我所知道的。@Johnykutty我希望我没有被弄糊涂,但我有一个Main.storyboard文件和多个“screens”(ViewControllers)在这个文件中。只有一个.storyboard文件意味着你必须从同一个故事板本身呈现下一个场景。我希望能帮助你谢谢你的迅速答复。我一定会试试这个,看看效果如何。@ryanbangras没问题!如果您还没有设置ViewControllerIdentifier,请检查EditOK,因此我尝试将其放入代码中,但每当提到“var”时,我都会出错。错误的弹出窗口称“变量名从未发生突变”。考虑更改为“让”常量。在“PrimeVIEW控制器(VC,动画:true,Neal:NIL)”中,我得到一个错误,它表示“关闭”中的“自我”的隐式使用;使用“self.”使捕获语义显式化。说到编码,我是一个彻头彻尾的傻瓜,我几乎不知道这意味着什么。请告知。编辑:我还没有看到你的编辑,我可以试试看,嗯。。。我似乎在任何地方都找不到ViewControllerIdentifier。我能找到的最接近的东西是视图控制器标题,它下面有一个复选框,上面写着“是初始视图控制器”,然后是一个专门用于自定义类、模块、情节提要ID和恢复ID的部分。我也不知道如何处理这些内容。@ryanbangras我已经编辑了一些代码,现在试试。您必须设置要启动的ViewController的情节提要ID(在我的代码示例中,ViewController标识符为“someViewController”)。你需要在代码上做的就是用你的故事板名称编辑故事板的名称“MyStoryboardName”,用你给ViewController的故事板ID编辑ViewController ID“someViewController”谢谢回复。我也会尝试你的代码,让你知道它是如何运行的!嘿,我试过你的代码,和其他人一样,我也犯了同样的错误。Xcode告诉我使用let而不是var,然后“在闭包中隐式使用‘self’;使用‘self.”使捕获语义显式化。行中:presentViewController(vc,animated:true,completion:nil)我该怎么办?Xcode告诉我使用let而不是var,所以用let替换var。实际上,这里Xcode检测您是否创建了一个数据成员,并且以后不更新它。所以Xcode建议您使用let,这意味着它在将来不会改变。关于“在闭包中隐式使用‘self’;…”用self替换presentViewController(vc,动画:true,完成:nil)。presentViewController(vc,动画:true,完成:nil)错误消失,但是一个新的出现了——它说使用了未解析的标识符“vc”。我该怎么办@阿努帕姆