gpt4 book ai didi

ios - 无法通过 UIAlert 中的闭包关闭 View Controller

转载 作者:行者123 更新时间:2023-11-29 02:26:55 24 4
gpt4 key购买 nike


全部,

我正在尝试编写一个将闭包传递给 UIAlertAction 的方法,这样当点击警报上的确定按钮时,警报和调用 View Controller 都会被关闭。

我有的是:

func displayErrMsg( ecode : errorCodes ) ->() {

var etitle = ""
var etext = ""
var completionHandler: (()->())?
switch ecode {
case .NoError :
etitle = "Found You!"
etext = "Check your email for a link to reset your password"
completionHandler = { self.dismissViewControllerAnimated(true, { println("BUHBYE") })}
case .EmailAddressNotFound :
etitle = "Sorry, but we could not find you."
etext = "Have you registered?"
case .MalFormedEmailAddress :
etitle = "Opps!"
etext = "that is not a valid email address"
default : println(" unrecognized error code: \(ecode)")
}

userMessage( self, etitle, etext, completionHandler )

}

它调用这个:

public func userMessage(parent: UIViewController, title:String, message:String, completion:(() ->())?) {
var okButton : UIAlertAction
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
if let comp = completion {
okButton = UIAlertAction(title: "Ok", style: .Default, handler: { (alert) -> Void in
comp()
})
}
else
{
okButton = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, nil)
}
alert.addAction(okButton)
parent.presentViewController(alert, animated: true, completion: nil)
}

在 ecode 为 .NoError 的情况下,我看到控制台中打印了“BUHBYE”,但是在点击确定按钮后 View 不会被删除(警报被删除)。据我所知,应该被关闭关闭的 View Controller 是 displayErrorMsg叫做。正确的?为什么这行不通?

非常感谢任何帮助。:bp:

最佳答案

我还不熟悉 swift,但如果逻辑与 Objective-C 相同,您可能希望在 didDismiss 警报 View 调用中显示此 View Controller ,而不是 点击按钮 :

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

另请注意,在实际关闭警报 View 之前,您不能从层次结构中删除呈现警报 View 的 View Controller 。

关于ios - 无法通过 UIAlert 中的闭包关闭 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27401978/

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