objective-c - 没有任何按钮的 UIAlertView

标签 objective-c iphone uialertview

我想知道下面的代码是否可以。我试图在 2 秒后(并且 alertView 中没有任何按钮)从“timedAlert”方法自动关闭 alertView。

    //this is in another method  
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [alert release];
    [self timedAlert];
}

-(void)timedAlert
{
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:2];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
}

如果 alertView 的 cancelButton 设置为“nil”,“[alertView dismissWithClickedButtonIndex:0 animated:YES];”将如何处理?事情的工作???我尝试将 cancelButton 设置为“nil”并且它起作用了,但无法弄清楚如何......

P.S: 我从另一个调用 timedAlert 方法

感谢任何帮助!谢谢!

最佳答案

首先让我说如果你用自定义 View 处理这个会更好,但话虽如此,问题看起来出在

[alert release];

您在使用完对象之前就释放了它(我很惊讶它没有崩溃)。

做这样的事情

// other code
alert = [[UIAlertView alloc] initWithTitle:nil message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
    [alert show];
    [self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:3.0f];
}

-(void)dismissAlert:(UIAlertView *) alertView
{
    [alertView dismissWithClickedButtonIndex:nil animated:YES];
    [alertView release];
}

关于objective-c - 没有任何按钮的 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7486466/

相关文章:

ios - Storyboard - 在没有 UINavigationController 的情况下弹出到根 UIViewController?

ios - 为什么在使用 AFNetworking 库将 wave 文件上传到服务器时应该得到二进制响应

swift - 如何使用 Swift 更改 iOS 警告消息框标题和消息中的字体大小

ios - 如何在 Swift 的 Alert 中为按钮添加 Action

objective-c - 核心动画动画滞后

objective-c - Objective-C NSStack 和 NSQueue?

iphone - 当方向改变时, View 中何时重新加载资源?

ios - 在 didFinishLaunchingWithOptions 完成之前显示 UIAlertView?

Objective-c/cocoa 将图像放置在标题栏中

iphone - 沿路径 iOS 移动两个形状?