简体   繁体   中英

UINavigationController not showing up when running build

EDIT: Working, see code below.

Working on an application, where right now I have the AppDelegate class, and a custom UINavigationController class. The code i have right now is pretty simple, and I feel like I've setup the XIB correctly. The build succeeds with no errors. But when the app launches, the navigationcontroller isnt displayed. I do not see the nav bar nor the table view. All I see is a blank screen. Here's the bulk of my code:

//
//  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];
}

Any ideas? Here's a screenshot of the XIB ,

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

Let me know if you need more info.

You need to check some points:

  • Is the code in application:didFinishLaunchingWithOptions: executed when you run the application? Add a NSLog(@"app launched") in this code or set a breakpoint in it to check it. If it is not called, your FTPPhotosAppDelegate object is probably not set as the delegate of your UIApplication singleton in the XIB (this connection should be done by default if you created your app using a standard template, but it's worth checking)
  • Is the navigationController IBOutlet of your AppDelegate properly connected to your actual UINavigationController in the XIB ? Check (using an NSLog or a breakpoint) that it is not nil .
  • Same for the view IBOutlet of the UINavigationController object in your XIB : check that it is properly connected and not nil .

Instead of a root View controller, make it a UINavigationController *navigationController .

then in the applicationDidFinishLaunching , say

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

EDIT: some example code i have in an app of mine

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

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