2016-08-11 8 views
1

みんな!DidReceiveLocalNotificationのアラートハンドラ/クロージャでVCからメソッドを追加する方法?

私のアプリがフォアグランドで、DidReceiveLocalNotificationがトリガーされたときに警告を表示したいとき。

AppDelegate.swiftの模擬ローカル通知にアラートを追加することはできますが、タイマー停止時にアニメーションを終了するには、ViewControllerからUIAlertActionクロージャ(コメント行参照)にメソッドを追加する方法がわかりません。

以下の私のコード:

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 } 
+0

あなたがlocalnotificationをタップしたときにのみdidReceiveLocalNotification呼び出しを知っていますか?正確に何をしたいですか? – Hasya

+0

@ハシャそれについては知らなかった。私のコードが働き、アプリケーションがフォアグラウンドで通知を受け取ったときに警告を表示します。少なくともシミュレータで。 問題は、このアラートでメソッドを使用してアニメーションを終了することです。コメント行を参照してください。このメソッドはメインViewControllerにあります。 私の友人はappdeleageteでNSNotificationを使用することを提案し、ViewControllerはこの特定のNSNotificationに反応するようにチューニングする必要があります。後で試してみる。 – odvan

+0

誰でも私の質問に編集できないのですか? – odvan

答えて

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

+0

すばらしいトピックです、すでに読んでいます。 コメントで私の質問を明確にしました。 – odvan

+0

私の答えodvanを試しましたか? – user3182143

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)

+0

私が間違っている場合は私を修正してください。しかし、iOS8 UIAlertViewは廃止予定です。 – odvan

+0

これはiOS9では廃止されましたが、あなたのアプリには影響しません。それはあなたを助けましたか? – Hasya

関連する問題