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
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。使用parent <->dismiss
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换2,使用导航栏控制,以及视图之间传值。
- iOS开发UIViewCOntroller视图控制器之间跳转切换举例,以及视图控制器生命周期的简单说明
- 【ios】 iphone开发之横屏与竖屏在不同视图之间的切换
- 【iOS开发-20】UIViewCOntroller视图控制器之间跳转切换举例,以及视图控制器生命周期的简单说明
- 【iOS开发】UIView之间常用视图之间切换方式
- ios中多视图之间动画切换
- IPHONE开发 VIEW之间的相互切换
- iOS 模态视图,视图之间的切换
- [IOS]iphone开发之横屏与竖屏在不同视图之间的切换
- IOS两个视图之间的传值
- IOS视图之间传值
- iphone视图之间的过度
- iphone视图之间的过度
- 视图控制器之间的切换
- UitableView 之间的视图切换
- mobile - 视图之间的切换
- iphone开发之横屏与竖屏在不同视图之间的切换
- 关于代码 复制
- 小悦2013简介
- 女程QQ群 252276006
- Java好书推荐
- [poj 1113]Wall[凸包]
- Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。使用parent <->dismiss
- Windows基础题目
- html网页抽奖
- 高性能WEB服务器和I/O设计模式
- iOS 计算器 基本功能实现
- Vim 复制粘贴探秘
- UNIX网络编程--套接字可读或可写的条件
- 动态规划求一道 类最长递增公共子序列
- oracle trunc()函数的用法