Iphone 无法使用文本字段显示UIAlertView

Iphone 无法使用文本字段显示UIAlertView,iphone,objective-c,ios,xcode,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 s

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

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;
@接口MyClass:NSObject
{
UIAlertView*警报;
id uu不安全u未获得授权的代表;
}
@属性(非原子,保留)UIAlertView*alert;
@属性(不安全\未维护)id委托;

问题可能是由于自定义

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

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

您可能会在这一行中遇到错误: 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];
}
很抱歉,我帮不了你更多的忙,但我不知道到底发生了什么,但我已经在其他线程中阅读了有关编辑要显示的内容时出现的问题


希望它能帮助你

EXC\u BAD\u访问
是由访问已释放的对象引起的。要避免这种情况,请调用
UIAlertView
某种模式:

职能机构:

-(void)checkSaving
{
    UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle:@"Do you want to add these results to your database?"
        message:@"\n\n"
        delegate:self
        cancelButtonTitle:@"No"
        otherButtonTitles:@"Save", nil];

    alert.alertViewStyle = UIAlertViewStyleDefault;
    [alert show];

    //this prevent the ARC to clean up :
    NSRunLoop *rl = [NSRunLoop currentRunLoop];
    NSDate *d;
    d= (NSDate*)[d init];
    while ([alert isVisible]) 
    {
     [rl runUntilDate:d];

    }
}
您的选择结果:

- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    // the user clicked one of the OK/Cancel buttons

    if (buttonIndex == 1)//Save
    {
        //do something

    }
    if (buttonIndex == 0)//NO
    {
        //do something
    }
}
在接口声明中注册函数:

@interface yourViewController ()
    -(void)checkSaving
    - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
//...
@end
致电:

[self checkSaving];

我希望这能对您有所帮助。

我没有什么好担心的(您使用的是ARC,它可以解决大多数内存管理问题)。能否显示UIAlertViewDelegate实现?你能发布回溯吗?你想在哪个版本的iOS上运行它??alertViewStyle仅在iOS 5+上可用。在iOS 4.3或更低版本上尝试此操作将崩溃。尽管在iOS 4上,它可能是setAlertViewStyle的“无法识别的选择器”。它在UI线程上吗?啊,很好的捕获@SVD。我没有注意到线程7上出现了异常,这至少引起了在后台线程上使用UIKit不当的问题。@jmstone iOS 5,我正在使用Xcode 4.2和Snow Leopard。我现在太累了。我明天会给你回复。请编辑你的答案并格式化代码使其可读。