简体   繁体   English

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

[英]iOS application crashes when button is clicked iPhone

I am using xcode 4.2 and ARC. 我正在使用xcode 4.2和ARC。 Below is the code I am using. 下面是我正在使用的代码。 When I click on any button my application crashes and it highlights this code in main.m 当我单击任何按钮时,我的应用程序崩溃,并在main.m中突出显示此代码

Sometimes I get this error 有时我会收到此错误

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

and sometimes application crashes without any error. 有时应用程序崩溃而没有任何错误。

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

My code is: 我的代码是:

In my ViewController.m I am using this code to get to the ChooseLogin view controller. 在我的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];
}

Then in ChooseLogin.m: 然后在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

This means that a message is being sent to a class that the class doesn't recognize -- you either haven't written this custom selector (function), or somehow a function is being sent to the wrong class and therefore isn't recognized. 这意味着消息正在发送到该类无法识别的类-您尚未编写此自定义选择器(函数),或者某种程度上函数被发送至错误的类,因此无法识别。 XCode is usually pretty good about catching these when trying to write code, but it doesn't seem to check for them in XIB files. XCode通常在尝试编写代码时很好地捕获它们,但是似乎没有在XIB文件中检查它们。

The most frequent cause of this type of issue, in my experience, is when you delete or rename a function, and forget to update the XIB file associated with that class -- or you do update it, and it adds the new version of the function, without forgetting the old one. 根据我的经验,此类问题的最常见原因是删除或重命名函数,而忘记更新与该类关联的XIB文件-或者您确实对其进行更新,并且它添加了新版本的功能,而不会忘记旧的功能。

  1. You should not add view of one view controller to another. 您不应将一个视图控制器的视图添加到另一个。
  2. If you still want to do this. 如果您仍然想这样做。 You create controller, take it's view amnd then it get released, because you store reference to it nowhere. 创建控制器,将其作为视图,然后将其释放,因为您无处存储对它的引用。 make ivar loginScreen and ARC will retain ChooseLogin controller for you. 使ivar loginScreen和ARC将为您保留ChooseLogin控制器。

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

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