在UIAlertview ios中运行时添加ok按钮

时间:2013-05-31 12:56:33

标签: iphone ios uialertview

我在调用Web服务时调用带有进度指示器视图的警报。我正在设置如下警告视图:

    [self.activityIndicatorView setHidden:NO];
self.alertView = [[UIAlertView alloc] initWithTitle:@"Sending Login Request.."
                                       message:@"\n"
                                      delegate:self
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];

self.activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
self.activityIndicatorView.center = CGPointMake(139.5, 75.5); // .5 so it doesn't blur
[self.alertView addSubview:self.activityIndicatorView];
[self.activityIndicatorView startAnimating];
[self.alertView show];

稍后如果登录失败,我想在警报视图上放置“确定”按钮,同时解除self.alertView,并再次显示self.alertView的新实例。这样的事情:

if (isThereErrorFromJsonResp) {
    [self.activityIndicatorView stopAnimating];
    [self.activityIndicatorView removeFromSuperview];
    self.activityIndicatorView = nil;
    [self.alertView setTitle:isThereErrorFromJsonResp];
    //here i want to show ok button how?
    return;
}

那么我该怎么把OK按钮? 有什么建议吗?

3 个答案:

答案 0 :(得分:4)

在获取响应时删除警报并显示新的警报实例

[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
self.alertView = [[UIAlertView alloc] initWithTitle:isThereErrorFromJsonResp
                                       message:@"\n"
                                      delegate:self
                             cancelButtonTitle:@"OK"
                             otherButtonTitles:nil];
[self.alertview show];

<强>解

好的尝试了,得到了它

使用

    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    [alertView addButtonWithTitle:@"Ok"];
    [alertView show];

这会将按钮添加到alertview

答案 1 :(得分:1)

看看使用ATMHud代替 - 这是一个抬头显示,您可以在显示时进行修改,并可以显示,启动,停止,旋转,添加消息等。当我使用它时,我有一个消息说“点击取消”,然后当登录成功时,显示“成功!”大约一秒钟,然后让它消失。这看起来HUD在动画方面看起来非常专业,你也可以对它进行很多控制。

答案 2 :(得分:-1)

试试这段代码:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"Logout in offline mode may cause of data lose. Do you still want to logout?" delegate:self cancelButtonTitle:@"NO" otherButtonTitles:@"YES",nil];
    alert.tag=11;
    [alert show];
    [self performSelector:@selector(go:) withObject:alert afterDelay:1.0];


-(void)go:(UIAlertView*)alert
{
    UIButton *b = (UIButton*)[alert viewWithTag:1];
    b.titleLabel.text = @"test";

}

您最初必须添加“确定”按钮。并将其属性设置为Hidden = TRUE。并在go方法中设置它的属性Hidden = FALSE