简体   繁体   中英

Why is self.navigationController nil in a storyboard application?

I have an iOS storyboard-based application which uses the navigationController to control the view transitions. The app delegate's didFinishLaunchingWithOptions is:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardPrincipal"  bundle:[NSBundle mainBundle]];
    self.viewController = [storyboard instantiateInitialViewController];
    // Instantiate a UIWindow object and initialize it with the screen size of the iOS device
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

}

In the initial view, pushed by the initial navigation controller, I push the next view like:

 [[UIStoryboard storyboardWithName:@"StoryboardPrincipal" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewControllerUsuario"];
 self.ViewUsuarioVIP = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerUsuario"];          
 self.navigationController.navigationBar.hidden = NO;
 self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
 [self.navigationController pushViewController:self.ViewUsuarioVIP animated:YES];

Although the next view is successfully pushed, self.navigationController is nil and the navigation bar does not appear.

//
//  ViewController.h


#import <UIKit/UIKit.h>
#import "PageControl.h"
#import "ViewUsuario.h"
#import "SaibaMaisViewController.h"
#import "WebService.h"
#import "ViewCaptainViewController.h"


@interface ViewController : UIViewController <UIScrollViewDelegate, PageControlDelegate, UITextFieldDelegate> {
    IBOutlet UIScrollView *_scrollView;
    IBOutlet PageControl *_pageControl;
    IBOutlet UIImageView *leftArrow;
    IBOutlet UIImageView *rightArrow;

    }
@property (nonatomic, retain) IBOutlet UIScrollView *_scrollView;
@property (nonatomic, retain) IBOutlet UIScrollView *_imageScrollView;
@property (nonatomic, retain) IBOutlet PageControl *_pageControl;
@property (nonatomic, retain) IBOutlet UIImageView *leftArrow;
@property (nonatomic, retain) IBOutlet UIImageView *rightArrow;
@property (nonatomic, retain) ViewUsuario *ViewUsuarioVIP;
@property (nonatomic, retain) ViewCaptainViewController *ViewCaptainVIP;
@property (strong, nonatomic) SaibaMaisViewController *SaibaMaisViewControllerVIP;
@property (strong, nonatomic) WebService *WebServiceVIP;
@property (strong, nonatomic) NSString *firstname;
@property (strong, nonatomic) NSString *id;
@property (strong, nonatomic) NSString *lastname;
@property (strong, nonatomic) NSString *type;
@property (strong, nonatomic) NSString *photo;


@end

I tried to post a image with the storyboard but I am not allowed to do so. But, the Navigation Controller is the RootView. The ViewUsaruio declaration is:

//
//  ViewUsuario.h
//  FlyVIP
//

#import <UIKit/UIKit.h>
#import "NovoVoo.h"
#import "VoosExistentesTableViewController.h"
#import "VooAbertosViewController.h"

@interface ViewUsuario : UIViewController;
@property (nonatomic, retain) NovoVoo *NovoVooVIP;
@property (nonatomic, retain) VoosExistentesTableViewController *VoosExistentesVIP;
@property (nonatomic, retain) VooAbertosViewController *VoosAbertosVIP;
@property (strong, nonatomic) NSString *firstname;
@property (strong, nonatomic) NSString *id;
@property (strong, nonatomic) NSString *lastname;
@property (strong, nonatomic) NSString *type;
@property (strong, nonatomic) NSString *photo;


@end

You also need to explain the structure of your storyboard, and the header of your ViewControllerUsuario view controller. Where have you defined a navigation controller in your storyboard?

If you want to have a navigation controller as the root view controller for your storyboard, you have to define one in the storyboard. One is not created for free. Note that if you create a storyboard with any other view controller as the root then you can select "embed in" from the editor menu. That will change the root view controller to a navigation controller and make the previous view controller the first child of the navigation controller.

Many times, your app acting strange in the iOS simulator is just XCode being weird.

Quit XCode entirely, then run your app again.

If this persists, you have a problem with how you've set the storyboard up. Most likely your view controller is not embedded within a navigation controller in the storyboard.

I like this tutorial as it has easy to follow examples and many screenshots.

我有同样的问题...您可以使用getter方法简单地引用它,即:

[self navigationController]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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