2012-04-05 103 views
0

这段代码是为了显示一个警告窗口,文本输入显示UIAlertView中:不能与文本字段

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" 

这是怎么自我。警报定义为:

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

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

没有什么可以跳出来(而且你正在使用ARC,这应该可以解决大多数内存管理问题)。你可以展示你的UIAlertViewDelegate实现吗?你可以发布回溯? – 2012-04-05 23:02:54

+2

什么版本的iOS试图运行这个? alertViewStyle仅适用于iOS 5+。在iOS 4.3或更低版本尝试此操作将会崩溃。 – jmstone617 2012-04-05 23:03:58

+0

尽管在iOS4上它可能是setAlertViewStyle的“无法识别的选择器”。它在UI线程上吗? – SVD 2012-04-05 23:06:17

回答

1

问题可能是由于定制。

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

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

您可能会在此行中看到一个错误: 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]; 
} 

对不起,帮不了你更多,但我不知道到底是什么发生,但我已经了解的问题时,编辑的东西来展示,在其他线程。

希望它能帮助你!

+0

我现在太累了。明天我会回到你身边。 – 2012-04-06 01:24:21

0

EXC_BAD_ACCESS是由访问发布的对象引起的。为了避免这种情况使您的来电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]; 

我希望这会帮助你。

+0

请编辑您的答案并对代码进行格式化以使其可读。 – kleopatra 2012-12-08 13:40:13