MechMK1
MechMK1

Reputation: 3378

load .xib file and display it

I'm trying to display an "About Page" in my application when pressing a button, but when I press the UIButton, the NSLog(@"Button pressed") is called, but the view won't load. Here is my code:

- (IBAction)pushAbout {
    NSLog(@"Button pressed");
    AboutView * aboutView = [[AboutView alloc] initWithNibName:@"AboutView" bundle:nil];
    [self.navigationController pushViewController:aboutView animated:YES];
    [aboutView release];
}

I've seen this code in an example, but it was called in a UITableViewController instead of an ordianry UIView controller.

The class files "AboutView" were created as UIViewController subclass. They were created and left untouched

Upvotes: 0

Views: 2147

Answers (2)

uvesten
uvesten

Reputation: 3355

A guess: Your view is not in a UINavigationController, hence

self.navigationController

is actually nil, and nothing happens.

The solution would be to place the main view in a nav controller, and adding the nav controller's view to your application's main window.

A simple way of doing this:

In your app delegate, change

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

to

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions
(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    UINavigationController* navController = [[UINavigationController alloc] 
        initWithRootViewController:self.viewController];

    self.window.rootViewController = navController;
    [self.window makeKeyAndVisible];
    return YES;
}

Or similar. Note that this is just to get you started, and maybe not the nicest way of doing it. (I assumed you've just created a view-based application in xcode.)

Upvotes: 2

Nick Weaver
Nick Weaver

Reputation: 47231

Yes you don't have a UINavigationController set.

Add

NSLog(@"nav? %@", self.navigationController); 

to your action and you'll see that it dumps (null).

However, the AboutView.xib works fine if you enter this code:

[self presentModalViewController:aboutView animated:YES];

instead of

[self.navigationController pushViewController:aboutView animated:YES];

The view will show up. In you zipped example the AboutView.xib didn't contain a label, so don't wonder if it turns out to be a white page.

You can dismiss the presented modal view by using

[self dismissModalViewControllerAnimated:YES];

in your AboutViewController.

To get your hands on a UINavigationController I suggest creating an app with the Navigation-Based Application template.

Upvotes: 1

Related Questions