Reputation: 979
I have this code currently for my UIAlertView:
if (counter > highscore) { //if highscore is achieved show an alert
highscore = counter;
NSString *nssHighscore = [NSString stringWithFormat:@"%i", highscore];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:@"Share"];
[alert show];
}
I want to run this code when the 'Share' button is clicked on the Alert
- (IBAction)PostToFacebook:(id)sender {
NSString *nssHighscore = [NSString stringWithFormat:@"%i", highscore];
mySLComposerSheet = [[SLComposeViewController alloc] init];
mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[mySLComposerSheet setInitialText:[NSString stringWithFormat:@"My New Highscore is %d - Try to Beat it!, highscore]];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
So far I've been following guides and questions on stack overflow but can't get it working.
Thanks
Upvotes: 2
Views: 6180
Reputation: 2941
You'll want to use the alertView:clickedButtonAtIndex:
delegate method, and check that the index matches [alertView firstOtherButtonIndex]
.
Like this:
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Congratulations, You Got A New High Score!!"
message:nssHighscore
delegate:self // <== changed from nil to self
cancelButtonTitle:@"Ok"
otherButtonTitles:@"Share"];
And in the same class:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView firstOtherButtonIndex]) {
[self PostToFacebook:self];
}
}
Upvotes: 8