Reputation: 393
I have an alert that displays the first time the app is launched, to ask the user's permission to allow the app to send local notifications. I have it in AppDelegate.m
. The alert is ok, but I have an error in the code to test which button was pressed (Yes or No).
I get an error at the - (void)alertView:clickedButtonAtIndex:
line that says Use of undeclared identifier alertview
.
Here's the code:
//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
}
}
Upvotes: 0
Views: 172
Reputation: 6207
Replace line:
NSString *title = [UIAlertView buttonTitleAtIndex:buttonIndex];
with:
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
UIAlertView
class has no class method buttonTitleAtIndex:
. It is an instance method which should be called on instances of UIAlertView
class.
Also make sure that you conform to UIAlertViewDelegate
protocol if you want to make use of alertView:clickedButtonAtIndex:
method.
EDIT:
Like @Sam suggested you can also make use of buttonIndex
instead of button title which can be changed in the future leaving you with a bug if you don't update your if
statements.
Example:
if (buttonIndex == 1) {
// do something
} else {
// do something else
}
EDIT 2:
Make sure you have a closing bracket "}" before method definition.
Example:
- (void)someMethod
{
...
} <= DONT FORGET TO TYPE A CLOSING BRACKET
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
...
}
Upvotes: 2