1

我希望 [User logOut];在用户触摸下面的Log Me out按钮时运行此方法UIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged in!" message:@"Logged in to App!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:@"Log me out", nil];
        [alert show];
        [alert release];

我该怎么做呢?“好的”适用于取消按钮。我只需要另一个按钮/方法来工作。

谢谢你的帮助

4

4 回答 4

6

你可以做类似的事情;

- (void)AlertConfirm
{
    UIAlertView *alert = [[UIAlertView alloc] init];
    [alert setTitle:@"Confirm"];
    [alert setMessage:@"Log out?"];
    [alert setDelegate:self];
    [alert addButtonWithTitle:@"Yes"];
    [alert addButtonWithTitle:@"No"];
    [alert show];
    [alert release];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        // Yes, do something
    }
    else if (buttonIndex == 1)
    {
        // No
    }
}

祝你好运,内森

于 2012-05-11T15:12:02.890 回答
2

您需要使用 UIAlertView 委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

确保将自己设置为 alertView 的代表。

于 2012-05-11T15:05:31.153 回答
1

将警报视图的委托设置为 self,并在您的 .h 中添加

按下按钮时将调用以下委托方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex

然后您可以通过执行以下操作查看按下了哪个按钮:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if ((alertView == alert) && (buttonIndex == 0))
        NSLog(@"alert's \"Okay\" button was pressed");
    else 
        NSLog(@"alert's \"Log Out" button was pressed");
}
于 2012-05-11T15:09:51.743 回答
1

将您的视图控制器设置delegateUIAlertView

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Logged in!" message:@"Logged in to App!" delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:@"Log me out", nil];
        alert.delegate = self;
        [alert show];
        [alert release];

然后在委托回调中处理:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{
    if (buttonIndex == alertView.cancelButtonIndex) {
        // Cancelled
        return;
    }

    // Log them out
}

注意测试(buttonIndex == alertView.cancelButtonIndex)。这比检查按钮索引的绝对值更好。

于 2012-05-11T15:16:21.747 回答