2

我的 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];
}

到目前为止,我一直在关注有关堆栈溢出的指南和问题,但无法使其正常工作。

谢谢

4

1 回答 1

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];
    }
}
于 2013-08-22T03:07:43.873 回答