简体   繁体   中英

iOS 9 Local Notifications using Swift 2

I have the following code

@IBAction func triggerNotif(sender: UIButton) {
    let dateComp:NSDateComponents = NSDateComponents()
    dateComp.year = 2015
    dateComp.month = 11
    dateComp.day = 12
    dateComp.hour = 21
    dateComp.minute = 30
    dateComp.timeZone = NSTimeZone.systemTimeZone()

    let calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
    let date:NSDate = calender.dateFromComponents(dateComp)!

    let notification:UILocalNotification = UILocalNotification()
    notification.alertBody = "body of notification"
    notification.fireDate = date
    notification.repeatInterval = NSCalendarUnit.Day

    UIApplication.sharedApplication().scheduleLocalNotification(notification)
}

Basically, I'm trying to trigger a notification that repeats every day using a UIButton. I referred to many other questions on this site to form up this code, and it builds successfully.

But when I click the button, I'm faced with this:

2015-11-12 18:30:17.025 Application Name[95723:485982] -[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70
2015-11-12 18:30:17.035 Application Name[95723:485982] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Application_Name.SettingsViewController triggerNotifBtn:]: unrecognized selector sent to instance 0x7f9163727d70'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010d293f45 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010efb7deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010d29c56d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x000000010d1e9eea ___forwarding___ + 970
    4   CoreFoundation                      0x000000010d1e9a98 _CF_forwarding_prep_0 + 120
    5   UIKit                               0x000000010dab1e91 -[UIApplication sendAction:to:from:forEvent:] + 92
    6   UIKit                               0x000000010dc1d4d8 -[UIControl sendAction:to:forEvent:] + 67
    7   UIKit                               0x000000010dc1d7a4 -[UIControl _sendActionsForEvents:withEvent:] + 311
    8   UIKit                               0x000000010dc1c8d4 -[UIControl touchesEnded:withEvent:] + 601
    9   UIKit                               0x000000010db1fed1 -[UIWindow _sendTouchesForEvent:] + 835
    10  UIKit                               0x000000010db20c06 -[UIWindow sendEvent:] + 865
    11  UIKit                               0x000000010dad02fa -[UIApplication sendEvent:] + 263
    12  UIKit                               0x000000010daaaabf _UIApplicationHandleEventQueue + 6844
    13  CoreFoundation                      0x000000010d1c0011 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    14  CoreFoundation                      0x000000010d1b5f3c __CFRunLoopDoSources0 + 556
    15  CoreFoundation                      0x000000010d1b53f3 __CFRunLoopRun + 867
    16  CoreFoundation                      0x000000010d1b4e08 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x0000000111887ad2 GSEventRunModal + 161
    18  UIKit                               0x000000010dab030d UIApplicationMain + 171
    19  Application Name                            0x000000010d0a8b6d main + 109
    20  libdyld.dylib                       0x000000010fabf92d start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

What's wrong?

您将 triggerNotifBtn 设置为函数名称,但您的函数名称是 triggerNotif

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM