在if语句中,PushViewController会破坏我的程序

时间:2014-03-07 03:56:21

标签: ios objective-c xcode uinavigationcontroller pushviewcontroller

我已经使用根视图控制器(登录屏幕)在另一个类中创建了一个导航控制器。当我点击登录按钮时,我希望它推送到仪表板视图控制器。这是我的代码

- (IBAction)logInHit:(id)sender {

    if (passWord == true) {
        DashBoardViewController *dash = [[DashBoardViewController alloc] initWithNibName:@"DashBoardViewController" bundle:nil];
        [self.navigationController pushViewController:dash animated:YES];
    }

    else if (passWord == false){
        UIAlertView *alert = [[UIAlertView alloc] init];
        [alert setTitle:@"Incorrect Password"];
        [alert setMessage:@""];
        [alert setDelegate:self];
        [alert addButtonWithTitle:@"Try Again"];
        [alert show];
    }
}

当它弹出视图控制器时它起作用,当它不在if语句中时它也可以工作但是由于某些奇怪的原因它在使用上面的代码时会中断。警报视图工作正常!这只是推动!请帮忙!

在App Delegate.h中:

    NavigationViewController *navView;

这是App Delegate(这创建了一个我所做的navigationController类的实例,它基于通用的实例):

    LogInViewController *logInView = [[LogInViewController alloc] init];
navView = [[NavigationViewController alloc] initWithRootViewController:logInView];


[self.window addSubview:navView.view];

这是日志:

2014-03-06 22:15:43.552 TopOPPS REP APP[795:70b] Application windows are expected to have a root view controller at the end of application launch
2014-03-06 22:16:18.728 TopOPPS REP APP[795:70b] -[LogInViewController textEnded:]: unrecognized selector sent to instance 0x109412a90
2014-03-06 22:16:18.730 TopOPPS REP APP[795:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LogInViewController textEnded:]: unrecognized selector sent to instance 0x109412a90'
*** First throw call stack:
(

0   CoreFoundation                      0x0000000101890795 __exceptionPreprocess + 165

1   libobjc.A.dylib                     0x00000001015f3991 objc_exception_throw + 43

2   CoreFoundation                      0x0000000101921bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205

3   CoreFoundation                      0x000000010188209d ___forwarding___ + 973

4   CoreFoundation                      0x0000000101881c48 _CF_forwarding_prep_0 + 120

5   UIKit                               0x00000001002570ae -[UIApplication sendAction:to:from:forEvent:] + 104

6   UIKit                               0x0000000100257044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17

7   UIKit                               0x000000010032b450 -[UIControl _sendActionsForEvents:withEvent:] + 203

8   UIKit                               0x000000010085a6d5 -[UITextField _resignFirstResponder] + 256

9   UIKit                               0x000000010037ee40 -[UIResponder resignFirstResponder] + 222

10  UIKit                               0x000000010085a4de -[UITextField resignFirstResponder] + 114

11  UIKit                               0x000000010029b888 -[UIView setUserInteractionEnabled:] + 285

12  UIKit                               0x000000010087f7b5 -[_UIViewControllerTransitionContext _disableInteractionForViews:] + 194

13  UIKit                               0x0000000100364ce5 -[UINavigationController pushViewController:transition:forceImmediate:] + 1038

14  TopOPPS REP APP                     0x000000010000254a -[LogInViewController logInHit:] + 202

15  UIKit                               0x0000000100257096 -[UIApplication sendAction:to:from:forEvent:] + 80

16  UIKit                               0x0000000100257044 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 17

17  UIKit                               0x000000010032b450 -[UIControl _sendActionsForEvents:withEvent:] + 203

18  UIKit                               0x000000010032a9c0 -[UIControl touchesEnded:withEvent:] + 530

19  UIKit                               0x000000010028bc15 -[UIWindow _sendTouchesForEvent:] + 701

20  UIKit                               0x000000010028c633 -[UIWindow sendEvent:] + 988

21  UIKit                               0x0000000100265fa2 -[UIApplication sendEvent:] + 211


22  UIKit                               0x0000000100253d7f _UIApplicationHandleEventQueue + 9549

23  CoreFoundation                      0x000000010181fec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17

24  CoreFoundation                      0x000000010181f792 __CFRunLoopDoSources0 + 242

25  CoreFoundation                      0x000000010183b61f __CFRunLoopRun + 767

26  CoreFoundation                      0x000000010183af33 CFRunLoopRunSpecific + 467

27  GraphicsServices                    0x00000001039983a0 GSEventRunModal + 161

28  UIKit                               0x0000000100256043 UIApplicationMain + 1010

29  TopOPPS REP APP                     0x0000000100001d53 main + 115

30  libdyld.dylib                       0x0000000101f1f5fd start + 1

31  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

3 个答案:

答案 0 :(得分:0)

我检查了你的代码,它的工作完全正常...... 但你必须在你的appDelegate方法中设置下面

 LogInViewController *loginVC = [[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil];

 UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginVC];

 self.window.rootViewController = navController;

根据您的控制台日志我可以说

您之前创建了一个名为 - (IBAction) textEnd :( id)sender {}的方法,并将该方法链接到任何位置以进行按钮或textfied,现在您删除了该方法而忘记删除该方法来自文件inpector的方法链接...

只需仔细检查文件检查器中的链接是否所有方法都可用...

我在创建时遇到了同样的错误 - (IBAction) textEnd:(id)sender {}方法并从文件操作符链接到UITextfield到EditingDidEnd操作,而不是从我的方法中删除方法整个方法。 m文件,我点击登录按钮时出现同样的错误,所以检查你的所有链接是否有可用的链接方法

答案 1 :(得分:0)

1你必须在appDelegate中设置根视图控制器,如

 LogInViewController *loginVC = [[LogInViewController alloc] initWithNibName:@"LogInViewController" bundle:nil];
 UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginVC];
 self.window.rootViewController = navController;

我认为你不熟悉条件。

你应该这样写

- (IBAction)logInHit:(id)sender {

    if (passWord ) {
        DashBoardViewController *dash = [[DashBoardViewController alloc] initWithNibName:@"DashBoardViewController" bundle:nil];
        [self.navigationController pushViewController:dash animated:YES];
    }
    else {
        UIAlertView *alert = [[UIAlertView alloc] init];
        [alert setTitle:@"Incorrect Password"];
        [alert setMessage:@""];
        [alert setDelegate:self];
        [alert addButtonWithTitle:@"Try Again"];
        [alert show];
    }
}

答案 2 :(得分:0)

好的,我明白了!

在“LogInViewController”的NIB文件中,您创建了一个名为“textEnded”的Action,之后您删除了该方法,但没有删除NIB中的引用。

要解决此问题,请转到LogInViewController NIB以查看Interface Builder,单击您的UITextFields,然后在Connections Inspector中查看Sent Events类别上的方法“textEnded”。之后,单击“X”图标以删除参考。

请参阅此图片作为参考:

enter image description here