【问题标题】:iPhone: IBAction Causes “Unrecognized Selector Sent to Instance” ErroriPhone:IBAction 导致“Unrecognized Selector Sent to Instance”错误
【发布时间】:2013-01-08 14:17:32
【问题描述】:

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

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 代码按钮,结果证明可以正常工作,但是当我将我的应用程序与两个视图控制器一起使用时,第一个将您重定向到第二个(如果您只登录到服务器)我得到了这个错误。我通过检查来自服务器的响应的条件移动到第二个视图控制器。这是我用来更改视图控制器的代码:

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];
 }

有什么想法吗?提前致谢!

【问题讨论】:

  • 粘贴你的requestIt函数

标签: iphone ios selector viewcontroller ibaction


【解决方案1】:

代码的错误部分很明显,这就是您初始化视图控制器的方式:

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

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

正确的代码应该是:

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

你真的需要弄清楚,我认为你不懂Objective-C,拿起一本书,开始阅读,然后做对!

【讨论】:

  • 确实是这样。尽管该代码来自我试图弄清楚如何正确访问新视图控制器的答案,但确实是一个严重的错误。无论如何感谢您的帮助和建议。
  • 很好,很高兴我能帮上忙。我们都会犯错……即使是错别字也会让你花上一周的时间去调试、压力和沮丧——我的经验!
【解决方案2】:

替换

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

与

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

还要检查您是否在 .h 文件中声明了 SendGPS 函数

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    相关资源
    最近更新 更多