[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
{
    [mainDelegate performSelectorOnMainThread:@selector(SwitchToView1) withObject:nil waitUntilDone:NO];
}


@end

view3.h

#import <Cocoa/Cocoa.h>

@interface view3 : NSViewController
{
    id mainDelegate;
}

-(void) sethandle:(id) handle;
-(IBAction)SwitchToView2:(id)sender;
-(IBAction)SwitchToView1:(id)sender;

@end

view3.m

#import "view3.h"

@interface view3 ()

@end

@implementation view3

- (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)SwitchToView1:(id)sender
{
    [mainDelegate performSelectorOnMainThread:@selector(SwitchToView1) withObject:nil waitUntilDone:NO];
}

@end

AppDelegate.h

#import <Cocoa/Cocoa.h>
@class view1;
@class view2;
@class view3;


@interface AppDelegate : NSObject <NSApplicationDelegate>
{
    IBOutlet NSScrollView *scrollView;
    
    view1 *scrollView1;
    view2 *scrollView2;
    view3 *scrollView3;
}

-(void) SwitchToView1;
-(void) SwitchToView2;
-(void) SwitchToView3;

@property (assign) IBOutlet NSWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "view1.h"
#import "view2.h"
#import "view3.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    // Insert code here to initialize your application
}

-(void)awakeFromNib
{
    scrollView1 = [[view1 alloc] initWithNibName:@"view1" bundle:nil];
    scrollView2 = [[view2 alloc] initWithNibName:@"view2" bundle:nil];
    scrollView3 = [[view3 alloc] initWithNibName:@"view3" bundle:nil];
    
    [scrollView1 sethandle:self];
    [scrollView2 sethandle:self];
    [scrollView3 sethandle:self];
    
    [scrollView setDocumentView:scrollView1.view];
}

-(void) SwitchToView1
{
    [scrollView setDocumentView:scrollView1.view];
}

-(void) SwitchToView2
{
    [scrollView setDocumentView:scrollView2.view];
}

-(void) SwitchToView3
{
    [scrollView setDocumentView:scrollView3.view];
}

@end
效果:

view1-view2:

[Cocoa]_[NSScrollView]_[界面切换]_第1张图片[Cocoa]_[NSScrollView]_[界面切换]_第2张图片


代码例子下载链接:http://download.csdn.net/detail/yepeng2014/9149981


方法二:基于Document-Base Application

1.创建工程,勾选 Create Document-Base Application

[Cocoa]_[NSScrollView]_[界面切换]_第3张图片

2.自定义类:ManagingViewController

ManagingViewController.h

#import <Cocoa/Cocoa.h>

@interface ManagingViewController : NSViewController
{
    NSManagedObjectContext *manageObjectContext;
}

@property (retain) NSManagedObjectContext *manageObjectContext;

@end

ManagingViewController.m

#import "ManagingViewController.h"

@interface ManagingViewController ()

@end

@implementation ManagingViewController

@synthesize manageObjectContext;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
}

@end

3.创建两个继承ManagingViewController并带xib的类: DepartmentViewController 和  EmployeeViewController,两个类中不添加代码,只是区别下xib的view

DepartmentViewController的view:

[Cocoa]_[NSScrollView]_[界面切换]_第4张图片

EmployeeViewController的view:

[Cocoa]_[NSScrollView]_[界面切换]_第5张图片

4.Document类:

Document.h

#import <Cocoa/Cocoa.h>

@interface Document : NSPersistentDocument
{
    IBOutlet NSBox *box;
    IBOutlet NSPopUpButton *popUp;
    NSMutableArray *viewControllers;
}

- (IBAction)DepartmentViewController:(id)sender;
- (IBAction)EmployeeViewController:(id)sender;

@end

Document.m

#import "Document.h"
#import "DepartmentViewController.h"
#import "EmployeeViewController.h"

@interface Document ()

@end

@implementation Document

- (instancetype)init {
    self = [super init];
    if (self) {
        // Add your subclass-specific initialization here.
        viewControllers = [[NSMutableArray alloc] init];
        
        ManagingViewController *vc;
        
        vc = [[DepartmentViewController alloc] init];
        [vc setManageObjectContext:[self managedObjectContext]];
        [viewControllers addObject:vc];
        
        vc = [[EmployeeViewController alloc] init];
        [vc setManageObjectContext:[self managedObjectContext]];
        [viewControllers addObject:vc];
    }
    return self;
}

- (IBAction)DepartmentViewController:(id)sender
{
    NSView *v = [[viewControllers objectAtIndex:0] view];
    [box setContentView:v];
}

- (IBAction)EmployeeViewController:(id)sender
{
    NSView *v = [[viewControllers objectAtIndex:1] view];
    [box setContentView:v];
}

- (void)windowControllerDidLoadNib:(NSWindowController *)aController {
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.
}

+ (BOOL)autosavesInPlace {
    return YES;
}

- (NSString *)windowNibName {
    // Override returning the nib file name of the document
    // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead.
    return @"Document";
}

- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
    // Insert code here to write your document to data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning nil.
    // You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead.
    [NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
    return nil;
}

- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
    // Insert code here to read your document from the given data of the specified type. If outError != NULL, ensure that you create and set an appropriate error when returning NO.
    // You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead.
    // If you override either of these, you should also override -isEntireFileLoaded to return NO if the contents are lazily loaded.
    [NSException raise:@"UnimplementedMethod" format:@"%@ is unimplemented", NSStringFromSelector(_cmd)];
    return YES;
}
xib文件:

[Cocoa]_[NSScrollView]_[界面切换]_第6张图片

效果:

[Cocoa]_[NSScrollView]_[界面切换]_第7张图片

[Cocoa]_[NSScrollView]_[界面切换]_第8张图片

代码例子下载:http://download.csdn.net/detail/yepeng2014/9211931

你可能感兴趣的:([Cocoa]_[NSScrollView]_[界面切换])