該場景如下:從視圖控制器獲取信息到AppDelegate
我有一個視圖控制器,分析輸入聲音。一旦聲音被識別出來,我就像這樣設置一個本地通知。
var notification: UILocalNotification = UILocalNotification()
notification.fireDate = NSDate(timeIntervalSinceNow: 0.0)
notification.alertTitle = knn as String
UIApplication.sharedApplication().scheduleLocalNotification(notification)
這調用AppDelegate中的application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification)
函數。
現在,我還想根據用戶在通知中選擇的內容在數據庫中保存一些數據。 我的AppDelegate的didReceiveLocalNotification
:
var state: UIApplicationState = application.applicationState
if state == .Active {
var alert = UIAlertController(title: "Alert", message: notification.alertTitle, preferredStyle: .Alert)
var correct = UIAlertAction(title: "Correct", style: .Default, handler: { (test) -> Void in
//create a new sound in context
var newSound = Sound.createInManagedObjectContext(self.managedObjectContext!, title: notification.alertTitle, zcr: vc.zcrArray, spectralCentroid: vc.scArray, spectralFlatness: vc.sfArray, mfcc: vc.mfccArray, spectralSlope: vc.ssArray)
var error : NSError?
self.managedObjectContext!.save(&error)
})
var wrong = UIAlertAction(title: "Incorrect", style: .Default, handler: nil)
alert.addAction(wrong)
alert.addAction(correct)
var view = self.window?.rootViewController
view?.presentViewController(alert, animated: true, completion: nil)
}
爲了將值保存到數據庫中,我需要從視圖控制器抓住他們。但我找不到一種方法來獲取它們。我嘗試使用self.window?.rootViewController
爲了嘗試釣魚正確的視圖控制器,但沒有成功。我試圖存儲的變量在上面的代碼中被稱爲vc.zcrArray
,vc.mfccArray
等。
我的故事板是這樣的:
任何想法我做錯了嗎?
你可以給一個代碼示例嗎?我嘗試了所有可能的組合,我仍然得到零。我也想到了userInfo,但它只接受一個列表,我需要通過4 ... – nevos
Userinfo是一個接受數組的nsdictionary,因此您可以像這樣設置數據:'ln.userInfo = @ {@「zcr」 :vc.zcrArray,@「mfcc」:vc.mfccArray};' –
由於您使用的是故事板,請嘗試使用以下一段代碼來獲取viewcontroller(請記住,您使用故事板和視圖的正確名稱控制器): 'UIStoryboard * storyboard = [UIStoryboard storyboardWithName:@「Main」bundle:nil]; UIViewController * uvc = [storyboard instantiateViewControllerWithIdentifier:@「Details」]; [self.window.rootViewController presentViewController:uvc animated:YES completion:nil];' –