简体   繁体   中英

how to change view controller in cocoa application?

I have two view controller, one is view controller of main.storyboard and the other is NSPageController.

I have one button, after pressing it I want to change view controller from main.storyboard to NSPageController.

In Viewcontroller.m of main.storyboard

- (void)loginButtonPressed:(id)sender {
    NSPageController *pageController = [[NSPageController alloc] initWithNibName:@"CustomPageController" bundle:nil];
}

I have one instance for page controller then how to change view controller from view controller to CustomPageController?

The most easy way to do so is to use a NSTabViewController with a tabless NSTabView. Replace your windows NSViewController with a NSTabViewController and switch the NSTabView style to tabless (in Interface Builder). In the individual NSViewController assigned to the tabs you can use a @IBAction to switch the enclosing NSTabViewController to the other view controller as following.

@IBAction func switchViewController(_ sender: Any) {

        if let tabViewController = parent as? NSTabViewController{

            tabViewController.selectedTabViewItemIndex = 1
        }
    } 

This would have the additional benefit that the transition is animated as well.

Your NSPageController would be one of the TabViewControllers tab items.

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