繁体   English   中英

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

[英]iOS application crashes when button is clicked iPhone

我正在使用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控制器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM