我目前有一个UIAlertView与两个选项按钮显示。当用户按下其中一个按钮时,我想要调用一个方法(在同一个对象中),然后从Web上检索文件。UIAlertView clickedButtonAtIndex EXC_BAD_ACCESS
我可以调用的方法很好,可以调用NSLog(),但只要我来使用任何对象变量,我得到一个EXC_BAD_ACCESS错误。
我的第一个想法是它可能是一个线程问题,所以认为调用NSNotificationCenter可能会解决这个问题,但这也会导致同样的错误。
以下是我目前得到的代码。我尝试了几个不同的东西(有些被注释掉了),但没有成功。 'dbURL'对象是该类的一个属性。 (编辑:下面的代码是不完整)
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//If database update
[[NSNotificationCenter defaultCenter] postNotificationName:@"newdb" object:self]; //EXC_BAD_ACCESS happens here
if ([alertView.title isEqualToString: @"Database Update"]){
switch (buttonIndex) {
case 0:
//[self getNewDatabase];
//[self performSelectorOnMainThread:@selector(getNewDatabase) withObject:nil waitUntilDone:NO];
//[NSThread detachNewThreadSelector:@selector(getNewDatabase) toTarget:self withObject:nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"newdb" object:self];
break;
// Get a new database file from the server
- (void)getNewDatabase{
NSLog(@"in database: %@", dbURL);
}
提前感谢!
您是否在同一个类中使用多个UIAlertView实例? – 2011-04-21 11:17:54
是的,但它们不会相互干扰。 – Mabbage 2011-04-21 11:19:14
我很好奇,为什么你使用'(alertView.title == @“数据库更新”)?正如Matteo Alessani发布的那样,这种比较不会如预期的那样发挥作用。你能启用NSZombieEnabled并在你的控制台上转储错误吗?堆栈跟踪也是有帮助的。 – 2011-04-21 11:22:36