运行构建时UINavigationController不显示

时间:2011-06-19 01:11:57

标签: iphone objective-c uinavigationcontroller

编辑:工作,请参阅下面的代码。

在应用程序上工作,现在我有AppDelegate类和自定义UINavigationController类。我现在的代码非常简单,我觉得我已正确设置XIB。构建成功,没有错误。但是当应用程序启动时,导航控制器不会显示。我没有看到导航栏和表格视图。我看到的只是一个空白屏幕。这是我的大部分代码:

//
//  FTPPhotosAppDelegate.h
//  FTPPhotos
//
//  Created by Aaron McLeod on 11-05-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "RootViewController.h"

@interface FTPPhotosAppDelegate : NSObject <UIApplicationDelegate> {
    UINavigationController *navigationController;
    RootViewController *rootViewController;
}

@property (nonatomic, retain) UINavigationController *navigationController;
@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) RootViewController *rootViewController;

@end


//
//  FTPPhotosAppDelegate.m
//  FTPPhotos
//
//  Created by Aaron McLeod on 11-05-30.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FTPPhotosAppDelegate.h"

@implementation FTPPhotosAppDelegate


@synthesize window=_window;

    @synthesize navigationController, rootViewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    navigationController = [[UINavigationController alloc] init];

    rootViewController = [[RootViewController alloc] init];
    [self.navigationController pushViewController:rootViewController animated:NO];
    [self.window addSubview:[self.navigationController view]];    
    [self.window makeKeyAndVisible];
    return YES;
}


@interface RootViewController : UITableViewController<UIImagePickerControllerDelegate> {
    NSMutableArray *photos;
    UIImagePickerController *picker;
    UIBarButtonItem *addPhotoButton;
}

@property (nonatomic, retain) NSMutableArray *photos;
@property (nonatomic, retain) UIImagePickerController *picker;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *addPhotoButton;

- (void) loadPhotos;
- (IBAction) addPhoto;
- (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize;
@end


#import "RootViewController.h"

@implementation RootViewController
@synthesize photos, picker, addPhotoButton;

- (void)viewDidLoad
{
    self.photos = [[NSMutableArray alloc] initWithCapacity:50];
    [self loadPhotos];
    [super viewDidLoad];
}

有什么想法吗?这是XIB

的屏幕截图

http://img148.imageshack.us/img148/189/nib.png

如果您需要更多信息,请与我们联系。

2 个答案:

答案 0 :(得分:0)

您需要检查一些要点:

  • 运行应用程序时是否执行了application:didFinishLaunchingWithOptions:中的代码?在此代码中添加NSLog(@"app launched")或在其中设置断点以进行检查。如果未调用,则FTPPhotosAppDelegate对象可能未设置为UIApplicationXIB单例的委托(如果您使用标准模板,但值得检查)
  • 您的AppDelegate的navigationController IBOutlet是否与UINavigationController中的实际XIB正确关联?检查(使用NSLog或断点),它不是nil
  • IBOutletUINavigationController对象的XIB视图相同:检查它是否已正确连接,而不是nil

答案 1 :(得分:0)

而不是根视图控制器,将其设为UINavigationController *navigationController

然后在applicationDidFinishLaunching,说

navigationController = [[UINavigationController alloc]init];
FirstViewController *firstView = [FirstViewController alloc]init];
[self pushViewController:firstView animated:NO];
firstView.title = @"TITLE";
[self addSubview:navigationController.view];

编辑:我在我的应用程序中的一些示例代码

AppDelegate.h

#import <UIKit/UIKit.h>

@interface DragonAge2AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@end

AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
TitleClass *titlePage = [[TitleClass alloc]init];

navigationController = [[UINavigationController alloc]init];
titlePage.title = @"Dragon Age 2";
[navigationController pushViewController:titlePage animated:NO];
[titlePage release];

// Override point for customization after application launch.
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];

return YES;
}