iPhone:IBAction导致“无法识别的选择器发送到实例”错误

时间:2013-01-08 14:17:33

标签: iphone ios selector viewcontroller ibaction

我正在开发一个iphone应用程序,您可以在其中登录服务器,在您获得验证后,我的应用程序会将您移动到另一个视图控制器,您可以使用按钮发送您的gps位置。出于某种原因,当我在下一个viewcontroller中按下按钮时,我收到此错误:

Administrator[3148:c07] -[UIViewController SendGPS:]: unrecognized selector sent to instance 0x88b58d0
2013-01-08 16:01:21.662 Administrator[3148:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController SendGPS:]: unrecognized selector sent to instance 0x88b58d0'

如果我在单个视图控制器中应用我的gps代码按钮,结果可以正常工作,但是当我将应用程序与两个viewcontrollers一起使用时,第一个重定向到第二个(如果只有你登录到服务器)我得到了这个错误。我通过检查来自服务器的响应的条件移动到第二个viewcontroller。这是我用来更改viewcontrollers的代码:

NSString *compare = [NSString stringWithFormat:@"true"];

    if ( [compare isEqualToString:string])  {

        UIViewController* FourthViewController = [[UIViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];
        [self.view addSubview:FourthViewController.view];   }
    else
        NSLog(@"validation not complete");

这是我使用NSTimer的按钮:

-(IBAction)SendGPS:(id)sender
 {
     Timer= [NSTimer scheduledTimerWithTimeInterval:30.0 target:self  selector:@selector(requestIt) userInfo:nil repeats:YES];
 }

有什么想法?提前谢谢!

2 个答案:

答案 0 :(得分:6)

代码中不正确的部分很明显,这是初始化视图控制器的方式:

    UIViewController* FourthViewController = [[UIViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];

这就是创建一个新的视图控制器对象,完全来自Apple的代码,没有查克诺里斯的想法SendGPS:是什么。因为您显然创建了一个名为“FourthViewController”的新视图控制器子类,它在Xcode项目中实现了SendGPS:。您正在创建一个名为“FourthViewController”的实例,但它实际上并不指向该类,它是您指定的UIViewController类。

正确的代码应该是:

    FourthViewController *myFourthViewController = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];

你真的需要弄清楚,我不认为你理解Objective-C,拿起书,读书,做对了!

答案 1 :(得分:0)

替换

UIViewController* FourthViewController = [[UIViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];

FourthViewController* FourthViewController = [[FourthViewController alloc] initWithNibName:@"FourthViewController" bundle:[NSBundle mainBundle]];

另请检查您的.h文件中是否已声明SendGPS功能

希望它可以帮到你