gpt4 book ai didi

xcode - 我的主视图 Controller 中的功能不起作用

转载 作者:行者123 更新时间:2023-11-30 13:57:51 25 4
gpt4 key购买 nike

我正在尝试使用我在主视图 Controller 中编写的函数,就是这样。

func displayAlert(title: String, message: String)
{
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)

alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler:
{ (action) -> Void in
self .dismissViewControllerAnimated(true, completion: nil)
})))

self.presentViewController(alert, animated: true, completion: nil)
}

我正在尝试在其他 viewController 调用 viewControllerRegistro 上使用它,但由于某种原因不起作用。就是这个...

@IBAction func signUp(sender: AnyObject)
{
//checar que el usuario copero al poner su correo electronico y su contraseña
if usernameRegistro.text == "" || correoRegistro.text == "" || contraseñaRegistro.text == ""
{
ViewController().displayAlert("Informando Error", message: "Porfavor completa los cuadros de registro")
}

有什么帮助吗?我正在使用 xcode 7.0 beta 6 和 swift 2

最佳答案

ViewController() 创建一个新的 ViewController 实例。该 View Controller 不是 View 层次结构的一部分(因为您刚刚创建了它,没有将其添加到任何地方)。

您必须在当前可见的 View Controller 上调用该方法。因此,该函数不应该是您的 MainViewController 类的一部分。它应该是需要它的 ViewController 类的一部分。或者,如果您在多个 View 类中需要它,您可以将该函数添加到 UIViewController 的扩展中:

extension UIViewController {
func displayAlert(title: String, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction((UIAlertAction(title: "Ok", style: .Default, handler:
{ (action) -> Void in
self .dismissViewControllerAnimated(true, completion: nil)
})))
presentViewController(alert, animated: true, completion: nil)
}
}

使用此扩展,您可以在任何 UIViewController 和 UIViewController 子类上调用 displayAlert

关于xcode - 我的主视图 Controller 中的功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33378650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com