`
stephen830
  • 浏览: 2966968 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

如何从当前窗口界面切换到另一个窗口界面

 
阅读更多

 

如何从当前窗口界面切换到另一个窗口界面

 

在程序中,经常会在不同的窗口界面中互相切换。本文,将介绍如何进行窗口界面的切换。

 

下面采用的示例以文章《在程序代码中设定控件调用的方法 》代码为基础。

 

操作处理过程:

(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  文件:

 

 

#import "NewViewController.h"

@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 "FirstViewController.h"

#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]按钮,即显示前一窗口。

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics