osx在两个nsviews之间切换 [英] osx switch between two nsviews
问题描述
我想在按下按钮时在两个 NSView
控件之间切换。基本上,我有.xib文件包含 NSWindow
控制。窗口包含两个子视图和几个按钮。我在对象列表中拖动了 NSViewController
,在xib中又拖动了 NSView
。
NSViewController
引用了 NSWindow
的视图,并且视图是浮动在xib文件中。
NSWindow
中的nsview1和nsview2之间切换?这是正确的方法吗?
为可交换视图所在的占位符定义一个NSView插口,以及一个属性,用于保持对当前使用的视图控制器的引用。
@property(assign)IBOutlet NSView * mainView;
@property(strong)NSViewController * currentViewController;
我使用一个通用的方法进行视图交换(使用autolayout使视图占据整个占位符视图) 。
- (void)setMainViewTo:(NSViewController *)controller
{
//删除现有子视图b $ b while([[self.mainView subviews] count]> 0)
{
[self.mainView.subviews [0] removeFromSuperview];
}
NSView * view = [controller view];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view];
NSDictionary * viewsDictionary = NSDictionaryOfVariableBindings(view);
[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@V:| [view] |
options:0
metrics:nil
views:viewsDictionary]];
[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@H:| [view] |
options:0
metrics:nil
views:viewsDictionary]];
self.currentViewController = controller;
}
现在您可以定义IBOutlets来实例化和交换视图控制器
- (IBAction)showView1:(id)sender
{
View1Controller * controller = [[View1Controller alloc] init] ;
[self setMainViewTo:controller];
}
- (IBAction)showView2:(id)sender
{
View2Controller * controller = [[View2Controller alloc] init];
[self setMainViewTo:controller];
}
I would like to switch between two NSView
controls upon pressing the button. Basically, i have .xib file which contains NSWindow
control. Window contains two subviews and few buttons. I have dragged NSViewController
in object list and one more NSView
in xib.
NSViewController
has reference to view from NSWindow
and view that is floating in xib file.
Question is, how can i switch between nsview1 and nsview2 in NSWindow
upon button press? Is this the right way to do it ?
Define a NSView outlet for the placeholder of where the swappable view is as well as a property for keeping a reference to the current view controller in use.
@property (assign) IBOutlet NSView* mainView;
@property (strong) NSViewController* currentViewController;
I use a generic method for the view swapping (using autolayout to make view take up entire placeholder view).
-(void)setMainViewTo:(NSViewController *)controller
{
//Remove existing subviews
while ([[self.mainView subviews] count] > 0)
{
[self.mainView.subviews[0] removeFromSuperview];
}
NSView * view = [controller view];
[view setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.mainView addSubview:view];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(view);
[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[view]|"
options:0
metrics:nil
views:viewsDictionary]];
[self.mainView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:nil
views:viewsDictionary]];
self.currentViewController = controller;
}
Now you can define IBOutlets to instantiate and swap your view controllers
-(IBAction)showView1:(id)sender
{
View1Controller * controller = [[View1Controller alloc]init];
[self setMainViewTo:controller];
}
-(IBAction)showView2:(id)sender
{
View2Controller * controller = [[View2Controller alloc]init];
[self setMainViewTo:controller];
}
这篇关于osx在两个nsviews之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!