Search code examples
iosios5

iOS unable to load views


I am trying to navigate from one view to another on touch of a button using iOS 5.0.

The code I am using

- (IBAction)Actionlogin:(id)sender {
    NSLog(@" login button has been pressed");
       NSLog(@"In init");
        test_details *aSecondPageController=[[test_details alloc]initWithNibName:@"test_details" bundle:nil];
       [self.navigationController pushViewController:aSecondPageController animated:NO];
      }

I have two xib files test_details_iPhone.xib and test_details_iPad.xib

Inside my testdetails.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    NSLog(@"it is coming here in second view");
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {
        // Custom initialization
    }
    NSLog(@"it has been returned ");
    return self;


}

LOGs

2013-04-25 11:06:17.191 app_gototest[3067:207]  login button has been pressed
2013-04-25 11:06:17.194 app_gototest[3067:207] In init
2013-04-25 11:06:17.195 app_gototest[3067:207] it is coming here in second view
2013-04-25 11:06:17.196 app_gototest[3067:207] it has been returned 

The view is not getting loaded onto the view. I suppose I am missing something.

application:didFinishLaunchingWithOptions: in appDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
    } else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
    }
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

** I am trying Jasper Blue's approach**

argc    int 1
argv    char ** 0xbfffed2c
*argv   char *  0xbfffee44

In appDeleagte.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UINavigationController* navigationController; 
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone"     bundle:nil];
            navigationController = [[UINavigationController alloc]         initWithRootViewController:_viewController];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad"     bundle:nil];
            navigationController = [[UINavigationController alloc] initWithRootViewController:_viewController];
        }
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        return YES;
}

Solution

  • You code looks OK, so it must be that self.navigationController is nil. . . Have you set up a navigation controller?

    For your code to work, your current UIViewController needs to be contained within a UINavigationController. . . you can set up the UINavigationController as the root view controller in your application delegate as follows:

    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        UINavigationController* navigationController; 
        // Override point for customization after application launch.
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone"     bundle:nil];
        navigationController = [UINavigationController alloc]         initWithRootViewController:viewController];
        } else {
            self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad"     bundle:nil];
            navigationController = [UINavigationController alloc] initWithRootViewController:viewController];
        }
        self.window.rootViewController = navigationController;
        [self.window makeKeyAndVisible];
        return YES;
    }
    

    NB: You could clean the above code up a little, but you get the picture, which is that you have to set the root view controller to be a navigation controller, like this:

     self.window.rootViewController = navigationController;
    

    You could also make a custom root view controller if you like, but the above will let you achieve what you're trying to do - UINavigationController will be used as the navigation system throughout your app.