Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。使用parent <->dismiss

来源:互联网 发布:程序员的电脑桌 编辑:程序博客网 时间:2024/05/29 07:37

所有的视图都继承自 UIViewController,都使用 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;来初始化视图,所以一般初始化的数据或者变量都是在这个方法中。

首先建一个single view 工程,然后再添加一个view。

结构如图:

单击单开 cidpViewController.xib 文件,在视图上面拖放一个UIbutton 和一个 UILable,并且在cidpViewController.h文件中声明一个变量和一个方法,然后将UIbutton 和对应的方法连线, UILable和对应的变量连线,(这里要注意的是,必须将UIbutton的Touch up inside 和对应的方法连接 )。

SecondViewController.xib同上:

关键代码如下:

#pragma mark 支持试图切换的方法#pragma mark --(IBAction)btnPreClick:(id)sender{    //实例化一个SecondViewController 对象    SecondViewController* second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];    //控制试图加载的样式,就是一些效果,有四种可以选择    second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;    //让实例化的试图对象加载在当前试图之上,    [self presentModalViewController:second animated:YES];    //页面传值。    second.lblNext.text = lblPre.text;    //释放开辟的空间,前提是将 arc关闭。    [second release];    }#pragma mark 试图消失的方法:#pragma mark --(IBAction)btnBackClick:(id)sender{    //让当前试图消失    [self dismissModalViewControllerAnimated:YES];}

整个代码如下:

////  cidpViewController.h#import <UIKit/UIKit.h>#import "SecondViewController.h"@interface cidpViewController : UIViewController{        IBOutlet UILabel* lblPre;}@property (nonatomic,retain) UILabel* lblPre;-(IBAction)btnPreClick:(id)sender;@end////  cidpViewController.m#import "cidpViewController.h"@implementation cidpViewController@synthesize lblPre;- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Release any cached data, images, etc that aren't in use.}#pragma mark - View lifecycle- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}- (void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];}- (void)viewDidAppear:(BOOL)animated{    [super viewDidAppear:animated];}- (void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];}- (void)viewDidDisappear:(BOOL)animated{    [super viewDidDisappear:animated];}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);}#pragma mark 支持试图切换的方法#pragma mark --(IBAction)btnPreClick:(id)sender{    //实例化一个SecondViewController 对象    SecondViewController* second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];    //控制试图加载的样式,就是一些效果,有四种可以选择    second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;    //让实例化的试图对象加载在当前试图之上,    [self presentModalViewController:second animated:YES];    //页面传值。    second.lblNext.text = lblPre.text;    //释放开辟的空间,前提是将 arc关闭。    [second release];    }@end////  SecondViewController.h#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController{    IBOutlet UILabel* lblNext;    }@property (nonatomic ,retain) UILabel* lblNext;-(IBAction)btnBackClick:(id)sender;@end////  SecondViewController.m#import "SecondViewController.h"@implementation SecondViewController@synthesize lblNext;- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization    }    return self;}- (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.}#pragma mark - View lifecycle- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view from its nib.}- (void)viewDidUnload{    [super viewDidUnload];    // Release any retained subviews of the main view.    // e.g. self.myOutlet = nil;}- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{    // Return YES for supported orientations    return (interfaceOrientation == UIInterfaceOrientationPortrait);}#pragma mark 试图消失的方法:#pragma mark --(IBAction)btnBackClick:(id)sender{    //让当前试图消失    [self dismissModalViewControllerAnimated:YES];}@end