Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在iOS 9中的alert.addAction中按下OK时如何打开另一个视图控制器_Ios_Swift - Fatal编程技术网

在iOS 9中的alert.addAction中按下OK时如何打开另一个视图控制器

在iOS 9中的alert.addAction中按下OK时如何打开另一个视图控制器,ios,swift,Ios,Swift,我想在按下add.alertAction中的“OK”按钮时显示名为InViewController的viewcontroller if ((user) != nil) { let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert) alert.addAction(UIAlertAction(title: "OK", styl

我想在按下
add.alertAction
中的“OK”按钮时显示名为InViewController的viewcontroller

if ((user) != nil) {               
   let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)
   alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
   self.presentViewController(alert, animated: true){}
}

当您添加completionHandler以执行所需操作时,可以将其添加到
UIAlertAction
,如下所示:

if ((user) != nil) {               
    let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)

    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: { _ -> Void in 
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerA") as! ViewControllerA
        self.presentViewController(nextViewController, animated: true, completion: nil)
    })

    alert.addAction(OKAction)
    self.presentViewController(alert, animated: true){}
}
要设置
情节提要ID
,可以在标识检查器中使用Interface Builder,请参见下图:

我将所有内容都放在上面的代码中,引用
ViewControllerA
,您必须根据需要设置
UIViewController
的名称

编辑

您指向的是
ui视图
或故事板上的其他对象。按下
UIViewController
中其他对象顶部的黄色指示灯,如下图所示:


我希望这对您有所帮助。

您可以在添加completionHandler以执行所需操作时,将其添加到
UIAlertAction
,如下所示:

if ((user) != nil) {               
    let alert = UIAlertController(title: "Success", message:   "Logged In", preferredStyle: .Alert)

    let OKAction = UIAlertAction(title: "OK", style: .Default, handler: { _ -> Void in 
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("ViewControllerA") as! ViewControllerA
        self.presentViewController(nextViewController, animated: true, completion: nil)
    })

    alert.addAction(OKAction)
    self.presentViewController(alert, animated: true){}
}
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
  let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewControllerIdentifier")
  self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
要设置
情节提要ID
,可以在标识检查器中使用Interface Builder,请参见下图:

我将所有内容都放在上面的代码中,引用
ViewControllerA
,您必须根据需要设置
UIViewController
的名称

编辑

您指向的是
ui视图
或故事板上的其他对象。按下
UIViewController
中其他对象顶部的黄色指示灯,如下图所示:


我希望这对你有帮助。

以下是你可以做到这一点的方法,
let alert = UIAlertController(title: "Success", message: "Logged In", preferredStyle: .Alert)
let action = UIAlertAction(title: "OK", style: .Default) { (action) -> Void in
  let viewControllerYouWantToPresent = self.storyboard?.instantiateViewControllerWithIdentifier("SomeViewControllerIdentifier")
  self.presentViewController(viewControllerYouWantToPresent!, animated: true, completion: nil)
}
alert.addAction(action)
self.presentViewController(alert, animated: true, completion: nil)
我只是在更新维克多·西格勒的优秀作品

你通过这个小更新来了解他的答案

 private func alertUser( alertTitle title: String, alertMessage  message: String )
{
   let alert = UIAlertController(title: title, message: message,   preferredStyle: .alert)
    let actionTaken = UIAlertAction(title: "Success", style: .default) { (hand) in
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyBoard.instantiateViewController(withIdentifier: "IntroPage") as? StarterViewController
        self.present(destinationVC!, animated: true, completion: nil)

    }
    alert.addAction(actionTaken)
    self.present(alert, animated: true) {}
}

你可以这样做, 我只是在更新维克多·西格勒的优秀作品

你通过这个小更新来了解他的答案

 private func alertUser( alertTitle title: String, alertMessage  message: String )
{
   let alert = UIAlertController(title: title, message: message,   preferredStyle: .alert)
    let actionTaken = UIAlertAction(title: "Success", style: .default) { (hand) in
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let destinationVC = storyBoard.instantiateViewController(withIdentifier: "IntroPage") as? StarterViewController
        self.present(destinationVC!, animated: true, completion: nil)

    }
    alert.addAction(actionTaken)
    self.present(alert, animated: true) {}
}


非常感谢您的快速回复。但是,当我输入我想要的视图控制器的名称时,我遇到了一个错误,您需要在这里输入一个
UIViewController
的实例,有关更多信息,请参阅更新的答案我的标识检查器下只有还原ID。你知道我还能在哪里设置情节提要ID吗?你需要首先在情节提要中标记你的viewcontroller别担心,但奇怪的是它在
恢复ID上,你确定吗?非常感谢你的快速响应。但是,当我输入我想要的视图控制器的名称时,我遇到了一个错误,您需要在这里输入一个
UIViewController
的实例,有关更多信息,请参阅更新的答案我的标识检查器下只有还原ID。你知道我还能在哪里设置情节提要ID吗?你需要先在情节提要中标记你的viewcontroller别担心,但奇怪的是它在
恢复ID上,你确定吗?你想如何实例化应该打开的控制器?从故事板上?你给它一个标识符了吗?我不是指它的类,而是一个故事板标识符!因为这就是你必须放在这里的东西…我把它改成了我给视图控制器设置的segue的标识符,但是它仍然终止,说没有这样的标识符。抱歉最后一个问题,老兄,我是一个硬核初学者,也是半个白痴。如果你设置了segue而不是
让ViewControlleryYouwant…
self。presentViewController…
你可以简单地调用
self.PerformsgueWithIdentifier
并传递你的segues标识符!我得到的是,伙计,我分配了标识符,一切都正常了。谢谢一堆你想如何实例化应该打开的控制器?从故事板上?你给它一个标识符了吗?我不是指它的类,而是一个故事板标识符!因为这就是你必须放在这里的东西…我把它改成了我给视图控制器设置的segue的标识符,但是它仍然终止,说没有这样的标识符。抱歉最后一个问题,老兄,我是一个硬核初学者,也是半个白痴。如果你设置了segue而不是
让ViewControlleryYouwant…
self。presentViewController…
你可以简单地调用
self.PerformsgueWithIdentifier
并传递你的segues标识符!我得到的是,伙计,我分配了标识符,一切都正常了。非常感谢