[Cocoa]_[NSScrollView]_[界面切换]
来源:互联网 发布:土佐之梦 知轩藏书 编辑:程序博客网 时间:2024/05/17 08:13
总结通过一个类来管理各个界面之间的切换,这样程序用到哪些界面,之间的逻辑关系都会比较清晰,我们有两种方法。
我的例子是创建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:
代码例子下载链接:http://download.csdn.net/detail/yepeng2014/9149981
方法二:基于Document-Base Application
1.创建工程,勾选 Create Document-Base Application
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:
EmployeeViewController的view:
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文件:
效果:
代码例子下载:http://download.csdn.net/detail/yepeng2014/9211931
0 0
- [Cocoa]_[NSScrollView]_[界面切换]
- [Cocoa]_[界面切换]
- [Cocoa]_[初级]_[自定义的界面上绘制图片]
- [Cocoa]_[初级]_[Cocoa开发应用实现多语言之间自由切换]
- [Cocoa]_[初级]_[Cocoa入门级界面开发之界面初级程序编辑的步骤和实现方法]
- [Cocoa]_[简单画图]
- [Cocoa]_[Plug-in]
- [Cocoa]_[初级]_[NSTimer在Cocoa中的应用,主要以多张图片切换的播放和停止]
- Cocoa Programming 手动界面---NSTextView (in NSScrollView)
- [Cocoa]_[初级]_[自定义NSTableHeaderView]
- [Cocoa]_[初级]_[NSTableView添加背景图片]
- [Cocoa]_[初级]_[NSThread的使用]
- [Cocoa]_[NSOutlineView]_[多根显示]
- [Cocoa]_[NSTableView]_[基本使用]
- [Cocoa]_[NSTableView]_[添加复选框]
- [Cocoa]_[NSNotificationCenter]_[基本使用]
- [Cocoa]_[初级]_[如何复制NSView]
- [Cocoa]_[初级]_[文件拖放操作]
- HasMap中添加List<String>遍历输出结果不对
- Spring+Struts2+Hibernate三大框架整合常见问题解析
- linux 用rpm安装GCC过程
- IVideoWindow视频窗口接口
- 阅读笔记4
- [Cocoa]_[NSScrollView]_[界面切换]
- TopCoder SRM 669 DIV 1
- iOS 自定义tabBar
- load和get的区别分析
- Linux下查看文件内容的命令
- weka基础:weka.core
- Ajax与HTML5 history pushState/replaceState实例
- 命令消息--ID_APP_EXIT整理
- 网络操作与AFNetworking