如何从当前窗口界面切换到另一个窗口界面
在程序中,经常会在不同的窗口界面中互相切换。本文,将介绍如何进行窗口界面的切换。
下面采用的示例以文章《在程序代码中设定控件调用的方法 》代码为基础。
操作处理过程:
(1)新建一个窗口视图;
(2)调用打开新建的窗口视图;
第1步:新建一个窗口视图
鼠标选中项目的classes目录,鼠标右键选中菜单 [Add > New File...].
在界面上选择[iOS > Cocoa Touch Class > UIViewController subclass ],并且选中 With XIB for user interface.
新建的视图取名为 NewViewController .
打开文件 NewViewController.xib ,在界面上增加一个按钮(标题设为 back)。
修改 NewViewController.h 文件:
#import <UIKit/UIKit.h>
@interface NewViewController : UIViewController {
}
-(IBAction)backButtonClicked:(id)sender;
@end
修改 NewViewController.m 文件:
@implementation NewViewController
-(IBAction)backButtonClicked:(id)sender{
[self.view removeFromSuperview];
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
然后,在 NewViewController.xib 中将按钮控件的点击事件绑定方法backButtonClicked。
第2步:调用打开新建的窗口视图;
修改 FirstViewController.m 文件:
#import "NewViewController.h"
@implementation FirstViewController
NewViewController *newViewController;
@synthesize label,button;
-(IBAction)myButtonClicked:(id)sender{
newViewController=[[NewViewController alloc] initWithNibName:@"NewViewController" bundle:nil];
[self.view addSubview:newViewController.view];
/*
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"my alert"
message:@"button is clicked"
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
[alert release];
*/
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
//create the label
CGRect frame=CGRectMake(50, 30, 200, 45);
label=[[UILabel alloc] initWithFrame:frame];
label.text=@"This is a label";
//create the button
frame=CGRectMake(50, 100, 200, 45);
button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=frame;
[button setTitle:@"OK" forState:UIControlStateNormal];
[button addTarget:self
action:@selector(myButtonClicked:)
forControlEvents:UIControlEventTouchUpInside];
//add the label and button into current view.
[self.view addSubview:label];
[self.view addSubview:button];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations.
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[newViewController release];
[label release];
[button release];
[super dealloc];
}
@end
执行后,点击界面上的[OK]按钮,即显示新建的窗口视图;再点击新窗口视图的[back]按钮,即显示前一窗口。
Binder系统组件
由第一章知道,Binder系统由3部分组成,client,server,binder驱动。
其中server由两部分组成:server和Service Manager,因为Service Manager是一种特殊的server,所以也将其划分到了server中,Service Manager是一个守护进程,用来管理Server,并向Client提供查询Server接口的能力。
1.Client、Server和Service Manager实现在用户空间中,Binder驱动程序实现在内核空间中
2. Binder驱动程序和Service Manager在Android平台中已经实现,开发者只需要在用户空间实现自己的Client和Server
3. Binder驱动程序提供设备文件/dev/binder与用户空间交互,Client、Server和Service Manager通过open和ioctl文件操作函数与Binder驱动程序进行通信
4. Client和Server之间的进程间通信通过Binder驱动程序间接实现
组件之间的关系如图:
参考:http://www.linuxidc.com/Linux/2011-07/39269.htm
下面将给出一些具体的理解链接,先供大家查看学习:
1. Service Manager是如何成为一个守护进程的?即Service Manager是如何告知Binder驱动程序它是Binder机制的上下文管理者。
http://www.linuxidc.com/Linux/2011-07/39272.htm
2. Server和Client是如何获得Service Manager接口的?即defaultServiceManager接口是如何实现的。
http://www.linuxidc.com/Linux/2011-07/39273.htm
3. Server是如何把自己的服务启动起来的?Service Manager在Server启动的过程中是如何为Server提供服务的?即IServiceManager::addService接口是如何实现的。http://www.linuxidc.com/Linux/2011-07/39274.htm
4 Service Manager是如何为Client提供服务的?即IServiceManager::getService接口是如何实现的。
http://www.linuxidc.com/Linux/2011-07/39321.htm
5. Client和Server是如何间接通过Binder驱动程序进行进程间通信的? http://www.linuxidc.com/Linux/2011-07/39620.htm
视图不满屏的处理
如果遇到视屏不满屏的情况,可以视图显示代码部分,增加下面的代码:
myview.view.frame=CGRectMake(0, 0, 320, 480);