总结通过一个类来管理各个界面之间的切换,这样程序用到哪些界面,之间的逻辑关系都会比较清晰,我们有两种方法。
我的例子是创建3个带.xib文件的NSViewController,在主类中通过NSScrollView类型的变量来切换不同的界面,比如在主类窗口里面拖动一个ScrollView并绑定到该变量中,我这里主类用默认的AppDelegate,还要把主类的指针传递给各个界面。下面是例子代码:
方法一:传self
view1.h
#import <Cocoa/Cocoa.h>
@interface view1 : NSViewController
{
id mainDelegate;
}
-(void) sethandle:(id) handle;
-(IBAction)SwitchToView2:(id)sender;
-(IBAction)SwitchToView3:(id)sender;
@end
view1.m
#import "View1.h"
#import "AppDelegate.h"
@interface view1 ()
@end
@implementation view1
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void) sethandle:(id) handle
{
mainDelegate = handle;
}
-(IBAction)SwitchToView2:(id)sender
{
[mainDelegate performSelectorOnMainThread:@selector(SwitchToView2) withObject:nil waitUntilDone:NO];
}
-(IBAction)SwitchToView3:(id)sender
{
[mainDelegate performSelectorOnMainThread:@selector(SwitchToView3) withObject:nil waitUntilDone:NO];
}
@end
view2.h
#import <Cocoa/Cocoa.h>
@interface view2 : NSViewController
{
id mainDelegate;
}
-(void) sethandle:(id) handle;
-(IBAction)SwitchToView3:(id)sender;
-(IBAction)SwitchToView1:(id)sender;
@end
view2.m
#import "view2.h"
@interface view2 ()
@end
@implementation view2
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Initialization code here.
}
return self;
}
-(void) sethandle:(id) handle
{
mainDelegate = handle;
}
-(IBAction)SwitchToView3:(id)sender
{
[mainDelegate performSelectorOnMainThread:@selector(SwitchToView3) withObject:nil waitUntilDone:NO];
}
-(IBAction)SwitchToView1:(id)sender
{
[mainDe