Hide content view of NSApplication window in order to navigate to main screen

≡放荡痞女 提交于 2020-01-06 21:31:44

问题


Initially I have my own setup on application main window, i.e. an image view(for background) and on it I have a set of buttons. Now when user taps on the buttons, respective lesson is launched, i.e. user is being navigated to the corresponding screen, i.e. I add the view to window's content view....

[self.window.contentView addSubview:self.lessonScreen.view];

Actually I know there is an approach of taking a view(custom view) on window, keeping on adding our views to that(for navigation). Then for navigating to main screen(window), we can simply hide that view. But I am following the approach of adding views through window's content view.

However the views get added fine, but I am not able to navigate to home screen, I tried various ways and the below is one of the method I followed:

[[window contentView] setHidden:YES];

Now what happens is the window becomes blank, the main view also vanishes.

I also tried to enumerate views in windows content view subviews and remove the same, but I am getting a crash and more over the same blank screen appears in this case too :(

I have also gone through questions here and there which didn't solve my problem.

Can some one please suggest me a way to remove content view from main window in order to jump to my main screen as I have main menu button for user navigation.

Any help is greatly appreciated, thanks :)


回答1:


Connect this method to the appropriate button or menu item

- (IBAction)deliteAllSubviews:(id)sender {

    NSArray* views = [[window contentView]subviews];

    for (NSView* view in views) {   
        [view removeFromSuperview];
    }
}



回答2:


My basic foundation was not proper, previously I didn't create my own class to handle main view controller. That's the reason why things went weird.

Just create our main menu view by creating our own NSViewController class, then add the same as our window's content view by adding in - (void)applicationDidFinishLaunching:(NSNotification *)aNotification method of our appDelegate file, i.e.:

self.mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
self.window.contentView = _mainViewController.view;

Now that we have started adding our own views to window's content view, in order to navigate to home screen, we need not hide the content view, instead assign our home view as the window's content view:

-(IBAction)topicButton:(id)sender
{
    [AppDelegate sharedInstance].mainViewController = [[MainViewController alloc]initWithNibName:@"MainViewController" bundle:nil];
    [[AppDelegate sharedInstance].window setContentView:[AppDelegate sharedInstanceofAppDelegate].mainViewController.view];
}

Note: Here [AppDelegate sharedInstance] is the instance of our app delegate class, i.e.:

+(AppDelegate *)sharedInstance
{
    if (!sharedAppDelegate) {
        sharedAppDelegate = (AppDelegate *)[[NSApplication sharedApplication]delegate];
    }
    return sharedAppDelegate;
}

That's it, we can now navigate the user to home screen when ever topic or main menu button's clicked :)

Hope it helps, thanks to one and all for the concern :)



来源:https://stackoverflow.com/questions/24532827/hide-content-view-of-nsapplication-window-in-order-to-navigate-to-main-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!