2012-03-03 139 views
1

我使用xcode 4.2和ARC。以下是我正在使用的代碼。當我上的任何按鈕點擊我的應用程序崩潰,它強調在main.m文件點擊按鈕時iOS應用程序崩潰iPhone

這段代碼有時我得到這個錯誤

-[__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 
+0

您需要檢查如何在Interface Builder nib文件中定義'UIView'子類,因爲您可能已將自定義視圖連接到不存在的選擇器(方法)。當你點擊視圖時,你必須編寫選擇器。 – 2012-03-03 21:28:39

+0

找不到它......你是說我打來的控制器可能有問題嗎? – 2012-03-03 21:31:23

+0

我編輯了我的問題,請檢查 – 2012-03-03 21:39:17

回答

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

這意味着,一條消息被髮送到該類不承認一類 - 你要麼不寫這個自定義選擇器(函數),或者某種功能被髮送到錯誤的類,因此無法識別。在嘗試編寫代碼時,XCode通常很擅長捕獲這些代碼,但似乎並未在XIB文件中檢查它們。

根據我的經驗,這種類型的問題最常見的原因是當您刪除或重命名函數,並忘記更新與該類關聯的XIB文件 - 或者您更新它時,它會添加新版本的功能,而不會忘記舊版本。

+0

基本上這個。在對nib文件進行更改後,或者對類定義進行更改後,很容易忘記對其他文件進行更改... – 2012-03-04 00:44:42

0
  1. 您不應該將一個視圖控制器的視圖添加到另一個視圖控制器。
  2. 如果你還想這樣做。你創建控制器,把它的視圖,然後它發佈,因爲你沒有存儲引用。讓伊娃loginScreen和ARC將爲您保留ChooseLogin控制器。
+0

ok,然後如何在不添加視圖的情況下實現此目的。 – 2012-03-03 21:53:19

+0

[使用模態控制器](https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html)。如果需要,你也可能需要制定一個協議來返回選定的值。 – 2012-03-04 07:01:38

+0

我不知道我明白 - 爲什麼不把視圖控制器的視圖添加到另一個視圖作爲子視圖?或者你是否說我不應該設置ViewController1.view = viewController2.view? – RonLugge 2012-03-04 07:11:11

相關問題