单击iPhone按钮时,iOS应用程序崩溃



我正在使用xcode 4.2和ARC。下面是我正在使用的代码。当我点击任何按钮时,我的应用程序都会崩溃,它会在main.m 中突出显示这段代码

有时我会得到这个错误

-[__NSCFTimer parentLogin:]: unrecognized selector sent to instance

有时应用程序崩溃而没有任何错误。

return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

我的代码是:

在我的ViewController.m中,我使用这段代码来访问ChooseLogin视图控制器。

- (IBAction)action:(id)sender {

    ChooseLogin *loginScreen = [[ChooseLogin alloc] initWithNibName:@"ChooseLogin" bundle:nil];
    [UIView beginAnimations:@"flipview" context:nil];
    [UIView setAnimationDuration:2];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
    [self.view addSubview:loginScreen.view];
    [UIView commitAnimations];
}

然后在ChooseLogin.m:中

@implementation ChooseLogin

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}
- (IBAction)parentLogin:(id)sender {
    NSLog(@":::::::");
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *passwd = [prefs stringForKey:@"password"];
    NSLog(@"data saved");
    if (passwd == nil) {

    CreatePassword *cPassword = [[CreatePassword alloc] initWithNibName:@"CreatePassword" bundle:nil ];
        [UIView beginAnimations:@"flipview" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
        [self.view addSubview:cPassword.view];
        [UIView commitAnimations];
    }else {
        UserPassword *uPasswd = [[UserPassword alloc] initWithNibName:@"UserPassword" bundle:nil];
        [UIView beginAnimations:@"flipview" context:nil];
        [UIView setAnimationDuration:1];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
        [self.view addSubview:uPasswd.view];
        [UIView commitAnimations];
    }
}
- (IBAction)childLogin:(id)sender {
    ChildLogin *loginScreen = [[ChildLogin alloc] initWithNibName:@"ChildLogin" bundle:nil];
    [UIView beginAnimations:@"flipview" context:nil];
   // [UIView setAnimationDuration:1];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.view cache:NO];
    [self.view addSubview:loginScreen.view];
    [UIView commitAnimations];
}
@end
-[__NSCFTimer parentLogin:]: unrecognized selector sent to instance

这意味着一条消息被发送到一个类无法识别的类——您可能没有编写这个自定义选择器(函数),或者某个函数被发送到错误的类,因此无法识别。XCode在编写代码时通常非常擅长捕捉这些信息,但它似乎不会在XIB文件中检查它们。

根据我的经验,这种类型的问题最常见的原因是当您删除或重命名一个函数,却忘记更新与该类相关的XIB文件时——或者您确实更新了它,它添加了该函数的新版本,而没有忘记旧版本。

  1. 您不应该将一个视图控制器的视图添加到另一个
  2. 如果你还想这么做。您创建控制器,获取它的视图,然后释放它,因为您将对它的引用存储在任何位置。使ivar loginScreen和ARC将为您保留ChooseLogin控制器

相关内容

最新更新