如何在DidReceiveLocalNotification的警报处理程序/闭包中添加VC中的方法?

时间:2016-08-11 12:33:24

标签: ios swift uilocalnotification

大家好!

当我的应用程序处于前台时,我希望它在DidReceiveLocalNotification触发时显示警告。

我可以添加警告来模仿AppDelegate.swift中的本地通知,但问题是我不知道如何将方法从我的ViewController添加到UIAlertAction闭包(请参阅注释行),以便在计时器停止时完成动画。 p>

我的代码如下:

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {


    let alertTimerEnds = UIAlertController(title: "Timer finished!", message: nil, preferredStyle: .Alert)

    let okAction = UIAlertAction(title: "OK", style: .Default) { finished in

        print("You've pressed OK button")

        //self.ViewController().finishAnimation()

    }

    alertTimerEnds.addAction(okAction)
    self.window?.rootViewController?.presentViewController(alertTimerEnds, animated: true, completion: nil)

}

也许我应该在ViewController usind AppDelegate中做到这一点?

    let someAppDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
    someAppDelegate?.application(UIApplication.sharedApplication(), didReceiveLocalNotification: UILocalNotification) { code for alert }

2 个答案:

答案 0 :(得分:0)

如果你想这样做

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
{

    var viewController : UIViewController = (application.keyWindow?.rootViewController)!

    while ((viewController.presentedViewController) != nil) {

        viewController = viewController.presentedViewController!
    }

    let alert = UIAlertController(title: "", message: notification.alertBody, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: {(action: UIAlertAction!) in}))

    viewController.presentViewController(alert, animated: true, completion: nil)

})

Show UIAlertController in didReceiveLocalNotification method

答案 1 :(得分:0)

尝试使用下面的代码,这将在viewcontroller上显示警告,该警报暂时存在。

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {

    var latestViewController : UIViewController!
    let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

    if let viewControllers = appDelegate.window?.rootViewController?.presentedViewController {

        latestViewController = viewControllers as UIViewController

    }
    else if let viewControllers = appDelegate.window?.rootViewController?.childViewControllers {

        latestViewController = viewControllers.last! as UIViewController

    }

    //var alert: UIAlertView!

    //alert = UIAlertView(title: "Title", message:"Message , delegate: nil, cancelButtonTitle:"Ok" )

    //alert.show()

    let alert = UIAlertController(title: "Title", message:"Message", preferredStyle: .Alert)
    let action = UIAlertAction(title: "OK", style: .Default) { _ in
    // Put here any code that you would like to execute when
    // the user taps that OK button (may be empty in your case if that's just
    // an informative alert)
    }

    alert.addAction(action)
    latestViewController.presentViewController(alert, animated: true){}

}

How do I migrate from UIAlertView (deprecated in iOS8)