转发UIAlertView单击另一个控制器

时间:2013-02-25 01:57:52

标签: ios objective-c uialertview

当我们点击UIAlertView上的“确定”按钮时,我正试图将用户发送到另一个控制器,但是我收到以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '***   setObjectForKey: object cannot be nil (key: classname)'

我的UIAertView在if语句中,如下所示:

if([placemarks count] > 0)
  {
    CLPlacemark *foundPlacemark = [placemarks objectAtIndex:0];
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Location set"
                       message:[NSString stringWithFormat:@"Your location, was set to %@", foundPlacemark.country]
                                                 delegate:self
                                        cancelButtonTitle:@"OK"
                                        otherButtonTitles:nil];
             [message show];

  }

我正在检查点击的按钮:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if(buttonIndex == 0)
{
    MasterViewController *controller=[[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
   [self.navigationController pushViewController:controller animated:YES];
  }
}

我确实有委托集:

@interface LocationViewController : UIViewController <CLLocationManagerDelegate, UIAlertViewDelegate>

UIAlertView可以自行运行。对我缺少什么的想法?

2 个答案:

答案 0 :(得分:1)

您是否正在使用Interface Builder来制作MasterViewController的UI?看来MasterViewController的nib文件有问题。当MasterViewController从xib文件获取init时,设置丢失并且您有一个例外。

最可能的情况是xib文件和文件所有者之间可能存在一些错误的连接。请检查您的代码中是否删除了已删除的IBOutlet,但是将连接保留在xib文件中。如果是这样,删除连接就可以解决了。

答案 1 :(得分:1)

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  if(buttonIndex == 0)
{
    // MasterViewController *controller=[[MasterViewController alloc]initWithNibName:@"MasterViewController" bundle:nil];
       MasterViewController *controller=[[MasterViewController alloc]init]; // creat your MasterViewController like this. 
   [self.navigationController pushViewController:controller animated:YES];
  }
}

如果没有像这样的编码崩溃,我认为问题是你的IB