显示警告xcode并在警报后返回

时间:2011-12-12 20:58:33

标签: objective-c

我想显示警告,当有人点击确定时,他们需要先发送到该页面。我该怎么做?

我使用以下代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"BOOYAH!" 
                                                message:@"Saved" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];

3 个答案:

答案 0 :(得分:2)

考虑到警报视图中有1个选项,并且委托是self。在与上面代码相​​同的.m文件中使用此方法

- (void)alertView:(UIAlertView *)alertV didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    //go back a page

    [alertV autorelease];

}

不要忘记释放警报视图。我在委托方法中添加了它,但你可以选择在显示它之后立即释放它(虽然只有1个版本)

答案 1 :(得分:0)

将UIAlertViewDelegate分配给self,然后实现以下方法调用

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

    if (buttonIndex == buttonIndexForOK) { // Where buttonIndexForOK is the index for your ok button, in this case, that would be zero, but if you want an OK and a Cancel button this would be different.
          // go back to the last page
    }

}

答案 2 :(得分:0)

UIAlertView遵循iOS开发中极为常见的委托设计模式。您提供了一个委托对象,当对象想要告诉您某些事情时,它会向该委托对象发送一条消息。

在您的代码中,您提供了self作为代理人。这意味着此对象需要符合the UIAlertViewDelegate protocol

您将看到有几种方法可以实现,以响应与警报视图相关的各种事件。您应该使用the alertView:clickedButtonAtIndex: method,它提供一个索引参数,指示点击了哪个按钮。

相关问题