Search code examples
uiviewcontrolleruinavigationcontrolleruinavigationbarpresentmodalviewcontrollerloginview

Insert LoginViewController (UIViewController) before UINavigationController throws exception


We've developed an application with UINavigationController where the user can also switch between different 'Master-Detail' views. Now we'd like to insert a LoginViewController before UINavigationController and push NavigationController from it if Login is successful.

LoginViewController.m:

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

}
return self;
}

- (void)viewDidLoad
{

UIImage *background = [UIImage imageNamed:@"LoginViewBG.png"];
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:background];

[self.view addSubview: backgroundView];

[super viewDidLoad];


}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];

}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{

 }
- (IBAction)loginButtonPress:(id)sender {
[self performSegueWithIdentifier:@"login_success" sender:self];
}

- (IBAction)backgroundTap:(id)sender {
[self.view endEditing:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
 return YES;
}

The Exception Thrown :

2014-08-19 11:01:31.337 iRehberBeta[24847:60b] -[LoginViewController navigationBar]:   unrecognized selector sent to instance 0xc0816c0
2014-08-19 11:01:31.363 iRehberBeta[24847:60b] *** Terminating app due to uncaught exception  'NSInvalidArgumentException', reason: '-[LoginViewController navigationBar]: unrecognized  selector sent to instance 0xc0816c0'
*** First throw call stack: 
( 
0   CoreFoundation                      0x051111e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x047c38e5 objc_exception_throw + 44
2   CoreFoundation                      0x051ae243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0510150b ___forwarding___ + 1019
4   CoreFoundation                      0x051010ee _CF_forwarding_prep_0 + 14
5   iRehberBeta                         0x00025ac1 -[AppDelegate application:didFinishLaunchingWithOptions:] + 193
6   UIKit                               0x0347d14f -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
7   UIKit                               0x0347daa1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1810
8   UIKit                               0x03482667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
9   UIKit                               0x03496f92 -[UIApplication handleEvent:withNewEvent:] + 3517
10  UIKit                               0x03497555 -[UIApplication sendEvent:] + 85
11  UIKit                               0x03484250 _UIApplicationHandleEvent + 683
12  GraphicsServices                    0x05c03f02 _PurpleEventCallback + 776
13  GraphicsServices                    0x05c03a0d PurpleEventCallback + 46
14  CoreFoundation                      0x0508cca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
15  CoreFoundation                      0x0508c9db __CFRunLoopDoSource1 + 523
16  CoreFoundation                      0x050b768c __CFRunLoopRun + 2156
17  CoreFoundation                      0x050b69d3 CFRunLoopRunSpecific + 467
18  CoreFoundation                      0x050b67eb CFRunLoopRunInMode + 123
19  UIKit                               0x03481d9c -[UIApplication _run] + 840
20  UIKit                               0x03483f9b UIApplicationMain + 1225
21  iRehberBeta                         0x00026bfd main + 141
22  libdyld.dylib                       0x04ce6701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Solution

  • I solved the problem by setting the UIViewController as RootViewController to UINavigationController and then pass onto other ViewControllers from it. Now it works fine.

    [self performSegueWithIdentifier:@"login_success" sender:self];
    

    and handle changes to destinationViewController in;

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
    ContentViewController *destViewController = segue.destinationViewController;
        destViewController.labelString = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
    }