多个视图的切换

多个视图的切换

STEP1

新建-》Cocoa Application 

(选中:Create Document based Application 和 Use Core Data)

需要做的事情:

增加一个box控件,设置最后一个选项为checked

    //[红色注意]

    //在容器控件box里面有一个属性: Autoresizing : Autoresizes Subviews (须选中)

    //则在启动的时候,会在控制台里出现异常BUG

    [selfdisplayViewController:[viewControllersobjectAtIndex:2]];


在初始化里面增加对每个视图的增加,例如增加了三个视图:

- (id)init

{

   self = [superinit];

   if (self)

    {

        // Add your subclass-specific initialization here.

        

        //增加三个视图,调用的时候,也要按照这个顺序012调用++++

       viewControllers = [[NSMutableArrayalloc] init];

EDUCATIONViewController *vc;

        vc = [[View1alloc] init];

[vc setManagedObjectContext:[selfmanagedObjectContext]];

[viewControllersaddObject:vc];//Index = 0

        

        vc = [[View2alloc] init];

[vc setManagedObjectContext:[selfmanagedObjectContext]];

[viewControllersaddObject:vc];//Index = 1

        

        vc = [[View3alloc] init];

[vc setManagedObjectContext:[selfmanagedObjectContext]];

[viewControllersaddObject:vc];//Index = 2

       //----

    }

    return self;

}


需要一个通用显示视图的函数

// 显示View的函数,通用调用,不需要修改

- (void)displayViewController:(EDUCATIONViewController *)vc

{

// Try to end editing

NSWindow *w = [m_Control_Box1window];

BOOL ended = [wmakeFirstResponder:w];

if (!ended) {

NSBeep();

return;

}

// Put the view in the box

NSView *v = [vcview];

// Compute the new window frame

   NSSize currentSize = [[m_Control_Box1contentView] frame].size;

   NSSize newSize = [v frame].size;

float deltaWidth = newSize.width - currentSize.width;

   float deltaHeight = newSize.height - currentSize.height;

   NSRect windowFrame = [w frame];

    windowFrame.size.height += deltaHeight;

    windowFrame.origin.y -= deltaHeight;

    windowFrame.size.width += deltaWidth;

    // Clear the box for resizing

    [m_Control_Box1setContentView:nil];

    [wsetFrame:windowFrame

       display:YES

       animate:YES];

[m_Control_Box1setContentView:v];

// Put the view controller in the responder chain

[v setNextResponder:vc];

[vc setNextResponder:m_Control_Box1];

}


然后在BT里面调用,例如显示第一个视图

    EDUCATIONViewController *vc = [viewControllersobjectAtIndex:0];

   [selfdisplayViewController:vc];



STEP2

Add files:继承 NSViewController的类,不要选择with xib [这个类主要起到桥梁作用,所有的视图都要继承这个新类,里面只有一个属性]

M

//动增加这个属性

@synthesize managedObjectContext;



H

//动增加这个属性

@property (strong)NSManagedObjectContext *managedObjectContext;


STEP3

新建立视图类,继承上面建立的继承 NSViewController的类

不同的视图调用不同的xib界面文件,所以需要手动初始化函数

- (id)init

{

    // View_1.xib

self = [superinitWithNibName:@"View_1"

 bundle:nil];

if (self) {

[selfsetTitle:@"View One"];

}

returnself;

}

[非常重要!!!,否则无法显示来自主界面信息]

- (void)loadView

{

    [super loadView];

    [self OnBT1:nil];

   // m_Lable1.stringValue=g_Name;

}



以下是完整代码参考:

主界面的M,h文件

//
//  EDUCATIONDocument.h
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import <Cocoa/Cocoa.h>

//继承
@class EDUCATIONViewController;

//以下继承的NSPersistentDocument必须在新建的时候选择 Use Cocoa Data
@interface EDUCATIONDocument : NSPersistentDocument
{
    //变量放在这里,就不用在对应的m里面再一次申明了。
    NSMutableArray *viewControllers;
}

//以下是一个box控件
@property (assign) IBOutlet NSBox *m_Control_Box1; 

//显示其他View
- (void)displayViewController:(EDUCATIONViewController *)vc;

@end


H

//
//  EDUCATIONDocument.m
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "EDUCATIONDocument.h"

#import "View1.h"
#import "View2.h"
#import "View3.h"

@implementation EDUCATIONDocument

@synthesize m_Control_Box1;

- (id)init
{
    self = [super init];
    if (self)
    {
        // Add your subclass-specific initialization here.
        
        //增加三个视图,调用的时候,也要按照这个顺序012来调用++++
        viewControllers = [[NSMutableArray alloc] init];
		EDUCATIONViewController *vc;
		
        vc = [[View1 alloc] init];
		[vc setManagedObjectContext:[self managedObjectContext]];
		[viewControllers addObject:vc]; //Index = 0
        
        vc = [[View2 alloc] init];
		[vc setManagedObjectContext:[self managedObjectContext]];
		[viewControllers addObject:vc]; //Index = 1
        
        vc = [[View3 alloc] init];
		[vc setManagedObjectContext:[self managedObjectContext]];
		[viewControllers addObject:vc]; //Index = 2
        //----
    }
    return self;
}

- (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 @"EDUCATIONDocument";
}

- (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.

    //启动的时候执行的函数
    //[红色注意]
    //在容器控件box里面有一个属性: Autoresizing : Autoresizes Subviews (必须选中)
    // 否则在启动的时候,会在控制台里出现异常BUG!
    [self displayViewController:[viewControllers objectAtIndex:2]];
}

+ (BOOL)autosavesInPlace
{
    return YES;
}

- (IBAction)OnBT1:(id)sender
{
    // 能在这里调用,使用0,1,2是因素再init里面已经将三个视图增加,并且按照0,1,2这样的顺序增加的。
   	EDUCATIONViewController *vc = [viewControllers objectAtIndex:0];
   	[self displayViewController:vc];

}

- (IBAction)OnBT2:(id)sender
{
    EDUCATIONViewController *vc = [viewControllers objectAtIndex:1];
   	[self displayViewController:vc];
}

- (IBAction)OnBT3:(id)sender
{
    //显示视图3 (将上面2句合并一句)
    [self displayViewController:[viewControllers objectAtIndex:2]];
}

// 显示View的函数,通用调用,不需要修改
- (void)displayViewController:(EDUCATIONViewController *)vc
{
	// Try to end editing
	NSWindow *w = [m_Control_Box1 window];
	BOOL ended = [w makeFirstResponder:w];
	if (!ended) {
		NSBeep();
		return;
	}
	
	// Put the view in the box
	NSView *v = [vc view];
	
	// Compute the new window frame
    NSSize currentSize = [[m_Control_Box1 contentView] frame].size;
    NSSize newSize = [v frame].size;
	
	float deltaWidth = newSize.width - currentSize.width;
    float deltaHeight = newSize.height - currentSize.height;
    NSRect windowFrame = [w frame];
    windowFrame.size.height += deltaHeight;
    windowFrame.origin.y -= deltaHeight;
    windowFrame.size.width += deltaWidth;
    // Clear the box for resizing
    [m_Control_Box1 setContentView:nil];
    [w setFrame:windowFrame
        display:YES
        animate:YES];
	
	[m_Control_Box1 setContentView:v];
	
	// Put the view controller in the responder chain
	[v setNextResponder:vc];
	[vc setNextResponder:m_Control_Box1];
}

@end


中间控制视图M,h文件

M

//
//  EDUCATIONViewController.h
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

// 增加文件:继承NSViewController,不选择with xib
// 本类只是起到中间过渡作用,只增加一个属性,其他视图,必须继承这个类,才可以使用
#import <Cocoa/Cocoa.h>

@interface EDUCATIONViewController : NSViewController

//手动增加这个属性
@property (strong) NSManagedObjectContext *managedObjectContext;

@end


H

//
//  EDUCATIONViewController.m
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "EDUCATIONViewController.h"

@interface EDUCATIONViewController ()

@end

@implementation EDUCATIONViewController

//手动增加这个属性
@synthesize managedObjectContext;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

@end


视图1的M,h文件

//
//  View1.h
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

#import "EDUCATIONViewController.h"

@interface View1 : EDUCATIONViewController

@end



//
//  View1.m
//  Learn_view_switch
//
//  Created by  on 21/7/14.
//  Copyright (c) 2014 EDUCATION. All rights reserved.
//

// 增加文件->选择继承:你新增加的ViewControllewr类,选中with xib (也可以手动另外再增加View)
#import "View1.h"

@interface View1 ()

@end

@implementation View1

// 手动:增加这个函数,调用的时候知道调用的是那一个VIEW
- (id)init
{
    // View1.xib
	self = [super initWithNibName:@"View1"
						   bundle:nil];
	if (self)
    {
		[self setTitle:@""];
	}
	return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

@end


视图2的M,h文件


视图3的M,h文件




  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值