为什么self.navigationController在故事板应用程序中为零?

时间:2014-09-27 00:07:29

标签: ios objective-c uiviewcontroller storyboard

我有一个基于iOS故事板的应用程序,它使用navigationController来控制视图过渡。应用代表的didFinishLaunchingWithOptions是:

- (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;

}

在初始视图中,由初始导航控制器推动,我推送下一个视图,如:

 [[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];

虽然已成功推送下一个视图,但self.navigationControllernil且导航栏未显示。

//
//  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

我试图用故事板发布图像,但我不允许这样做。但是,导航控制器是RootView。 ViewUsaruio声明是:

//
//  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

3 个答案:

答案 0 :(得分:0)

您还需要解释故事板的结构以及ViewControllerUsuario视图控制器的标题。你在哪里定义了故事板中的导航控制器?

如果您想将导航控制器作为故事板的根视图控制器,则必须在故事板中定义一个。一个不是免费创建的。请注意,如果您创建一个以任何其他视图控制器作为根的故事板,那么您可以选择&#34;嵌入&#34;从编辑菜单。这会将根视图控制器更改为导航控制器,并使前一个视图控制器成为导航控制器的第一个子控件。

答案 1 :(得分:0)

很多时候,你的app在iOS模拟器中表现得很奇怪只是XCode很奇怪。

完全退出XCode,然后再次运行您的应用程序。

如果仍然存在,则说明如何设置故事板有问题。很可能您的视图控制器未嵌入故事板中的导航控制器中。

我喜欢this tutorial因为它很容易遵循示例和许多屏幕截图。

答案 2 :(得分:-3)

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

[self navigationController]