iphone - 无法显示带有文本字段的 UIAlertView

标签 iphone objective-c ios xcode

这段代码应该显示一个带有文本输入的警报窗口:

self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[self.alert show];

导致此错误:

Thread 7: Program received signal: "EXC_BAD_ACCESS"

这是 self.alert 的定义方式:

@interface MyClass : NSObject
{
    UIAlertView *alert;
    id <MyClassDelegate> __unsafe_unretained delegate;
}

@property (nonatomic, retain) UIAlertView *alert;
@property (unsafe_unretained) id <MyClassDelegate> delegate;

最佳答案

问题可能是因为自定义。

我不知道为什么,但在我看来,问题是因为使用了线程+自定义了警报。

您可以尝试在主线程上显示此警报吗?会发生什么?

您可能会在这一行中遇到错误: self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;

如果是,您需要做的是在主线程中执行此操作。

- (void) yourMethod{
    [self performSelectorOnMainThread:@selector(yourMethod2) withObject:nil waitUntilDone:NO];
}

- (void) yourMethod2{
    self.alert = [[UIAlertView alloc] initWithTitle:@"Hello" message:@"How are you?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    self.alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    [self.alert show];
}

抱歉,无法为您提供更多帮助,但我不知道到底发生了什么,但我已经在其他线程中阅读了编辑要显示的内容时出现的问题。

希望对你有帮助!

关于iphone - 无法显示带有文本字段的 UIAlertView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10037123/

相关文章:

iphone - iPhone 中行 Tcp/Ip 的 SSL 客户端

ios - 转换为 ARC 后,我得到 "message sent to deallocated instance"

objective-c - 我无法在URL中发送带有阿拉伯字母的请求

ios - iOS-compelationHandler:nil导致应用程序崩溃

iphone - 我应该如何在 iOS 中动态创建表单?

ios - 解析 Koloda 库上的数据 - 无法在 View 中显示

iphone - 引用 UIbutton 的 alpha 的 if/else 语句不起作用

来自 [String : AnyObject] 的 iOS Swift 值

ios - EZAudio iOS - 将波形保存到图像

iphone - UILabel 文本的大纲