2016-08-21 72 views
0

我試圖從ViewController定義用戶通知操作。根據我的理解,必須在應用程序代表中設置通知與操作。swift,UIUserNotificationAction從視圖控制器,而不是應用程序委託

所以here's我的應用程序代表:

class AppDelegate: UIResponder, UIApplicationDelegate { 




enum Actions:String{ 
    case confirmunlock = "CONFIRMUNLOCK_ACTION" 
} 

var categoryID:String { 
    get{ 
     return "CONFRIMUNLOCK_CATEGORY" 
    } 
} 


// Register notification settings 
func registerNotification() { 

    // 1. Create the actions ************************************************** 

    // confirmunlock Action 
    let confirmunlockAction = UIMutableUserNotificationAction() 
    confirmunlockAction.identifier = Actions.confirmunlock.rawValue 
    confirmunlockAction.title = "Öffnen" 
    confirmunlockAction.activationMode = UIUserNotificationActivationMode.Background 
    confirmunlockAction.authenticationRequired = false 
    confirmunlockAction.destructive = false 




    // 2. Create the category *********************************************** 

    // Category 
    let confirmunlockCategory = UIMutableUserNotificationCategory() 
    confirmunlockCategory.identifier = categoryID 

    // A. Set actions for the default context 
    confirmunlockCategory.setActions([confirmunlockAction], 
           forContext: UIUserNotificationActionContext.Default) 

    // B. Set actions for the minimal context 
    confirmunlockCategory.setActions([confirmunlockAction], 
           forContext: UIUserNotificationActionContext.Minimal) 


    // 3. Notification Registration ***************************************** 

    let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound] 
    let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: confirmunlockCategory) as? Set<UIUserNotificationCategory>) 
    UIApplication.sharedApplication().registerUserNotificationSettings(settings) 
} 


// MARK: Application Delegate 


func application(application: UIApplication, 
       handleActionWithIdentifier identifier: String?, 
              forLocalNotification notification: UILocalNotification, 
                   completionHandler:() -> Void) { 

    // Handle notification action ***************************************** 
    if notification.category == categoryID { 

     let action:Actions = Actions(rawValue: identifier!)! 

     switch action{ 

     case Actions.confirmunlock: 
      print("Well done!") 

     } 
    } 

    completionHandler() 
} 



var window: UIWindow? 




func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    // Override point for customization after application launch. 

    registerNotification() 

    return true 
} 

這種運作良好。當我將「notification.category = categoryID」添加到我的LocalNotifications之一時,它會記錄「做得好」。現在我想調用的動作實際上應該從視圖控制器中調用。它有很多變量,你必須先登錄才能訪問服務器以調用操作等。我該怎麼做?當我把這個功能放在我的視圖控制器中時,動作並沒有被調用:

func application(application: UIApplication, 
       handleActionWithIdentifier identifier: String?, 
              forLocalNotification notification: UILocalNotification, 
                   completionHandler:() -> Void) { 

    // Handle notification action ***************************************** 
    if notification.category == categoryID { 

     let action:Actions = Actions(rawValue: identifier!)! 

     switch action{ 

     case Actions.confirmunlock: 
      print("Well done!") 

     } 
    } 

    completionHandler() 
} 

很多謝謝! Stephan

回答

0

我知道它與NSNotification一起工作。

NSNotificationCenter.defaultCenter().postNotificationName 

在觸發器所在的Appdelegate中。和

NSNotificationCenter.defaultCenter().addObserver 

在我的視圖控制器的viewdidload。

最好 斯蒂芬

0

此功能是UIApplicationDelegate協議的一部分,它由您的AppDelegate實現,它是UIApplication類型的實例。如果您有參考,您可以從此功能中調用您的控制器。

+0

嗨,我沒有嘗試過。不幸的是我的視圖控制器功能不適用於應用程序代理。我已經開始了,所選的行不會被傳輸。所以這在我的ViewController中工作(selectedlock是一個選擇器視圖行):'let key = keys [selectedlock]'在App Delegate中,無論我在選擇器視圖中選擇了哪一個, (ViewController()。selectedlock)' – StephanD

+0

我想你的應用程序架構還沒有準備好像那樣使用,你似乎需要弄清楚類實例和它們的函數是如何相互交互的。例如。你不應該依賴全局變量('ViewController'中的'selectedLock')來在對象之間進行交互。 一種方法可能是使用通知:http://nshipster.com/nsnotification-and-nsnotificationcenter/ –

+0

我可以使用NSNotificationCenter在我的ViewController中觸發此操作嗎? – StephanD

相關問題