[Cocoa]_[NSScrollView]_[界面切换]

    总结通过一个类来管理各个界面之间的切换,这样程序用到哪些界面,之间的逻辑关系都会比较清晰,我们有两种方法。

    我的例子是创建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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值