Objective-C ,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。
所有的视图都继承自 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)之间的切换,以及视图之间传值。的更多相关文章
- Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)
使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...
- Objective-C ,ios,iphone开发基础:几个常用类-NSNumber
2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...
- [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:UIAlertView使用详解
UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...
- Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器
新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView 两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...
- Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)
json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...
- Objective-C ,ios,iphone开发基础:http网络编程
- (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...
- Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器
第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...
- Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录
第一步:下载第三方库,点击 连接 下载, 第二部:准备数据库:按照连接&中博客的步骤实现数据库, 数据库的设计大致如下表: id username pas ...
- Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作
SQLite 是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...
随机推荐
- tomcat server获取用户的请求地址
当用户 与 tomcat之间 用 nginx做跳转时, HttpServletRequest 中的 getRemoteHost()方法获取到的只是nginx的地址,而不能拿到用户真正的请求地址 解决方 ...
- RestTemplate中文乱码问题
使用RestTemplate传输带有图片的表单时,需要对表单中的中文参数进行URL编码, eg :URLDecoder.decode(name); // 使用默认的解码 ...
- ViewPager使用笔记
1.ViewPager.setCurrentItem(position),即使已设置动画,但是没有动画效果 原因:因为ViewPager滑动之前的时间间隔太短,可以通过反射,去修改ViewPager自 ...
- HDU 1142 A Walk Through the Forest (求最短路条数)
A Walk Through the Forest 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1142 Description Jimmy exp ...
- STL学习系列五:Queue容器
Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...
- BestCoder Round #71 (div.2) (hdu 5621)
KK's Point Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 1828 Picture (线段树+扫描线)(周长并)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1828 给你n个矩形,让你求出总的周长. 类似面积并,面积并是扫描一次,周长并是扫描了两次,x轴一次,y ...
- 剑指OFFER之变态跳台阶(九度OJ1389)
题目描述: 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 输入: 输入可能包含多个测试样例,对于每个测试案例, 输入包括一个整数n(1 ...
- Windows 环境下于虚拟环境安装源码安装 cx_oracle
安装前提条件: (1).安装 instantclient-basic-nt (2).安装 instantclient-sdk-nt (3).安装 Microsoft Visual C++ Compil ...
- 哥也能写KMP了——实现strstr()
经过上次去面试,面试官要求实现strstr(),当场就蒙了.这个题目是模式匹配问题,<算法导论>里列出了几种字符串匹配算法: 朴素算法 | Rabin-Karp | 有限自动机算法 | ...