如何使用Swift编程以编程方式解除警报?

3

您好,我是一个新手iOS应用程序开发者。
我已经使用了一些cocoapods框架来制作警报视图。
我使用的是下面提到的SweetAlert警报视图。

我尝试在不按警报中的选项按钮的情况下编程方式解除警报。
请有人帮忙解决这个问题。


你可以使用计时器:NSTimer,在指定的时间后隐藏你的AlertView。 - iDeveloper
我已经尝试过了,但它没有起作用。 - vara
1
请添加代码片段,基本上就是你想要实现的功能。 - iDeveloper
实际上,我已经能够使用上述的SweetAlert框架。在其中,它有一个带有按钮的信息。但是我只需要在关闭程序后的文本。我已经隐藏了那个按钮,但它没有关闭。 - vara
5个回答

4
您可以通过在alertController对象上调用dismissViewControllerAnimated方法来关闭警告框。
alertControllerObject?.dismissViewControllerAnimated(true, completion: nil)

3
我认为您可以在SweetAlert类中使用pressed(sender: UIButton!)方法。
@IBAction func aBasicMessageAlert(sender: AnyObject) {
    let sweetAlert = SweetAlert().showAlert("Here's a message!")

    close(sweetAlert, after: 2.0)
}


func close(alert: SweetAlert, after seconds: Double) {
    NSTimer.scheduledTimerWithTimeInterval(seconds,
                                           target: self,
                                           selector: #selector(closeAlert),
                                           userInfo: ["alert": alert],
                                           repeats: true)
}

func closeAlert(timer: NSTimer) {
    let alert = timer.userInfo!["alert"] as! SweetAlert

    let dummyCloseButton = UIButton()
    dummyCloseButton.tag = 0
    alert.pressed(dummyCloseButton)
}

0
请使用这个:
yourAlerView.dismiss(withClickedButtonIndex: 0, animated: true)

当你处理回车键时,ButtonIndex是你想要默认点击以隐藏警报的按钮索引。

希望这能帮到你。


0

你需要在SweetAlert中添加此方法,并调用它。

func closeAlert(){


        UIView.animateWithDuration(0.5, delay: 0.0, options: UIViewAnimationOptions.CurveEaseOut, animations: { () -> Void in
            self.view.alpha = 0.0
        }) { (Bool) -> Void in
            self.view.removeFromSuperview()
            self.cleanUpAlert()

            //Releasing strong refrence of itself.
            self.strongSelf = nil
        }
    }

实现类似这样的警告

let  alert = SweetAlert() // take this as global
func showAlert(){

        alert.showAlert(title as String, subTitle: msg as String, style: style, buttonTitle:buttonOtherTitle as String, buttonColor:UIColor.redColor() , otherButtonTitle:  buttonOkTitle as String, otherButtonColor: colors.KBlueTextColor!) { (isOtherButton) -> Void in
            if isOtherButton
            {
                completionHandler(false)
            }
            else
            {
                completionHandler(true)
            }
        }
    }
func CloseAlert(){


         alert.closeAlert()
}

0

试一下这个

当你调用Alert方法时也叫这个名字,在alert方法内部

NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: "hideAlert:", userInfo: userInfo, repeats: true) //repeats: false

在 Alert 方法之外调用 This

 func hideAlert(){
        isOtherButton == true// isOtherButton getting from your SweetAlert Demo
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接