0

Good evening,

I currently have two UIViewControllers. My appDelegate looks like this

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    struct CGRect rect = [[UIScreen mainScreen] bounds];
    rect.origin.x = rect.origin.y = 0.0f;

    _viewController = [[sandboxViewController alloc] init];

    UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:_viewController];

    _window = [[UIWindow alloc] initWithFrame:rect];

    [_window makeKeyAndVisible];
    [_window addSubview:nc.view];

    return YES;
}

The viewController looks like this:

    - (void)loadView {
        self.view = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 480.0f)];
        self.view.backgroundColor = [UIColor whiteColor];
        self.navigationItem.title = @"Master View";
    }

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
        [infoButton addTarget:self action:@selector(switchView:) forControlEvents:UIControlEventTouchUpInside];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
    }

    - (void)switchView:(id)obj {
        if(![self navigationController])
            NSLog(@"navigationController IS NIL!!!");
        if(!_secondView) 
            _secondView = [[secondViewController alloc] init];
        [self.navigationController pushViewController:_secondView animated:YES];
}

By clicking on the info button, that was added to the right side on the navigation bar, I want to switch to the secondView. This, however, is not happening because navigationController logs as nil ! What am I missing here?

Any help is truly appreciated!

1 Answer 1

1

You don't have to create the window, it should already exist.

//_window = [[UIWindow alloc] initWithFrame:rect]; //remove this line    
[self.window makeKeyAndVisible]; //use the ivar
[self.window addSubview:nc.view];
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the hint! I changed the code but it's not working! It says expected rootViewCintoller at the end of application launch
ah you need to set rootViewController on the window
self.window.rootViewController = nc;
Railway, thx!! That did the trick! However I had to add self.window alloc/initWithFrame to make it to work! What exactly is the difference between using self.window and _window?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.