我怎样才能在ViewController
内,appDelegate
内或其他地方调用这种方法:
func contactDelete(notification : NSNotification){}
我必须在<{p>里面的appDelegate
里面打电话
`didFinishLaunchingWithOptions` here or inside other classes:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
if let aLaunchOptions = launchOptions { // Checking if there are any launch options.
// Check if there are any local notification objects.
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
// Handle the notification action on opening. Like updating a table or showing an alert.
UIAlertView(title: notification.alertTitle, message: notification.alertBody, delegate: nil, cancelButtonTitle: "OK").show()
let root : UIViewController = self.window!.rootViewController! as UIViewController
let alertview = JSSAlertView().show(root, title: "Oops!", text: "Unable to add the new Contact, check contact permission from Settings.", buttonText: "Check it", cancelButtonText: "Nope", color: UIColor.init(red: 0.216, green:0.043, blue:0.129, alpha: 1))
alertview.addAction(contactDelete)
alertview.setTextTheme(.Light)
}
}
它需要某种额外的论据,但我不知道是什么论点。我甚至无法在viewDidLoad
方法中调用它。我试图像这样触发它,但它不会起作用:
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.contactDelete(_:)), name:DELETECONTACT, object: nil)
答案 0 :(得分:2)
在您的Appdelegate代码中,您将根据Notification到达或使用以下简单启动分配RootviewController:
if let notification = (aLaunchOptions as NSDictionary).objectForKey("UIApplicationLaunchOptionsLocalNotificationKey") as? UILocalNotification {
但是您将NotificationViewController
设置为rootViewController
并且您在MainViewController上为addObserver
,因此当您收到通知且rootView设置为NotificationViewController
时,您将不会创建NSNotificationCenter你不能发布它。所以我建议创建如下代码:
您的appdelegate didFinishLaunchingWithOptions
方法:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
let tintColor = UIColor(red: 252/255, green: 72/255, blue: 49/255, alpha: 1)
window!.tintColor = tintColor
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert , categories: nil))
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("first") as UIViewController
let navController: UINavigationController = UINavigationController(rootViewController: initialViewControlleripad)
navController.navigationBarHidden = true
self.window?.rootViewController = navController
self.window?.makeKeyAndVisible()
let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
if (notification != nil) {
runAfterDelay(2.0) { // add 2 second delay of call method open notificationViewController from mainViewController
self.FireNewViewControlelr(notification.userInfo!)
}
}
return true
}
以下是两种调用NotificationViewController的方法:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: Value)
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
func runAfterDelay(delay: NSTimeInterval, block: dispatch_block_t) {
let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))
dispatch_after(time, dispatch_get_main_queue(), block)
}
如果你的应用程序处于后台模式,而你从通知横幅中打开了应用程序,则点按didReceiveLocalNotification
,如下所示:
func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
print("notification - tapped")
if application.applicationState == UIApplicationState.Active {
print("App already open")
} else {
print("App opened from Notification")
runAfterDelay(2.0) {
self.FireNewViewControlelr(notification.userInfo!)
}
}
}
使用以下方法更改代码:
func FireNewViewControlelr(Value: [NSObject : AnyObject]) {
print("userInf \(Value)")
let navigationController: UINavigationController = (self.window!.rootViewController as! UINavigationController)
let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let initialViewControlleripad : NotificationViewController = mainStoryboardIpad.instantiateViewControllerWithIdentifier("Main") as! NotificationViewController
initialViewControlleripad.notificationInfo = Value
navigationController.pushViewController(initialViewControlleripad, animated: true)
}
NotificationViewController
中的
var notificationInfo = [NSObject : AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
print("data is \(notificationInfo)")
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func action(sender: AnyObject) {
NSNotificationCenter.defaultCenter().postNotificationName(DELETECONTACT, object: notificationInfo)
self.navigationController?.popToRootViewControllerAnimated(true)
}