简体   繁体   English

如何在可可应用中更改视图控制器?

[英]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. 我有两个视图控制器,一个是main.storyboard的视图控制器,另一个是NSPageController。

I have one button, after pressing it I want to change view controller from main.storyboard to NSPageController. 我有一个按钮,按下后我想将视图控制器从main.storyboard更改为NSPageController。

In Viewcontroller.m of main.storyboard 在main.storyboard的Viewcontroller.m中

- (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? 我有一个页面控制器实例,然后如何将视图控制器从视图控制器更改为CustomPageController?

The most easy way to do so is to use a NSTabViewController with a tabless NSTabView. 最简单的方法是将NSTabViewController与表NSTabView一起使用。 Replace your windows NSViewController with a NSTabViewController and switch the NSTabView style to tabless (in Interface Builder). 用NSTabViewController替换Windows NSViewController,并将NSTabView样式切换为表(在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. 在分配给选项卡的单个NSViewController中,可以使用@IBAction将封闭的NSTabViewController切换到另一个视图控制器,如下所示。

@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. 您的NSPageController将是TabViewControllers选项卡项之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM