簡體   English   中英

征求用戶許可使用本地通知

[英]asking user's permission use local notifications

我有一個警報,顯示第一次啟動該應用程序,以征詢用戶的許可以允許該應用程序發送本地通知。 我在AppDelegate.m有它。 警報是可以的,但是我在代碼中無法測試按下哪個按鈕(是或否)。

我在- (void)alertView:clickedButtonAtIndex:行收到錯誤- (void)alertView:clickedButtonAtIndex: ,提示使用未聲明的標識符 alertview

這是代碼:

//ask user if its ok to send local notifications
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Notifications?"
                                                  message:@"Is it ok for this app to send you reminder notifications? (You can change this in Settings)"
                                                 delegate:self
                                        cancelButtonTitle:@"No"
                                        otherButtonTitles:@"Yes", nil];
[message show];

//which button was clicked?
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *title = [UIAlertView buttonTitleAtIndex:buttonIndex];
    if([title isEqualToString:@"No"])
    {
        NSLog(@"NO was selected.");
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"notifications"]; //0 = off, 1 = on
    }
    else
    {
        NSLog(@"YES was selected.");
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"notifications"]; //0 = off, 1 = on
    }    
}

替換行:

NSString *title = [UIAlertView buttonTitleAtIndex:buttonIndex];

與:

NSString *title = [alertView buttonTitleAtIndex:buttonIndex];

UIAlertView類沒有類方法buttonTitleAtIndex: 這是一個實例方法 ,應在UIAlertView類的實例上調用。

如果要使用alertView:clickedButtonAtIndex:方法,還請確保您遵循UIAlertViewDelegate協議。

編輯

就像@Sam建議的那樣,您還可以使用buttonIndex代替按鈕標題,以后可以更改按鈕標題,如果不更新if語句,則會出現錯誤。

范例

if (buttonIndex == 1) {
    // do something
} else {
    // do something else
}

編輯2

確保方法定義之前有一個右括號“}”。

范例

- (void)someMethod
{
    ...
}  <= DONT FORGET TO TYPE A CLOSING BRACKET 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex    
{
    ...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM