【发布时间】:2016-06-16 12:33:38
【问题描述】:
我怎么能在ViewController、appDelegate或其他地方调用这种方法:
func contactDelete(notification : NSNotification){}
我必须在 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)
【问题讨论】:
-
注册
contactDelete函数处理什么样的通知? -
@PhillipMills 我正在使用 LocalNotificationHelper,所以我在 2 个地方使用它。一个地方是通知操作的位置,现在我必须在第二个函数中使用它
-
我的意思是:既然它是为响应通知而设计的,为什么不发送一个而不是直接调用它呢?
-
什么意思?如果我直接调用它,我必须发出很多通知。我也在 ViewController NSNotificationCenter.defaultCenter().addObserver(self, selector: "contactDelete:", name: DELETECONTACT, object: nil) 中这样称呼它
-
@PhillipMills 检查我的编辑。这是函数
标签: swift function notifications appdelegate