目前我的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];
}
我希望在警报
上点击“分享”按钮时运行此代码- (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];
}
到目前为止,我一直在关注堆栈溢出的指南和问题,但无法使其正常工作。
由于
答案 0 :(得分:8)
您需要使用alertView:clickedButtonAtIndex:
委托方法,并检查索引是否与[alertView firstOtherButtonIndex]
匹配。
像这样:
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"];
在同一个班级:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [alertView firstOtherButtonIndex]) {
[self PostToFacebook:self];
}
}