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 ...
随机推荐
- iOS 开发的9个超有用小技巧
http://www.jianshu.com/p/221507eb8590 1.如何快速的查看一段代码的执行时间. 1 2 #define TICK NSDate *startTime = [NS ...
- iPhone各种尺寸
iPhone 6 Plus 736x414 points 2208x1242 pixels 3x scale 1920x1080 physical pixels ...
- HDU 5775 Bubble Sort (线段树)
Bubble Sort 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5775 Description P is a permutation of t ...
- TTL电平、CMOS电平、RS232电平的区别
工作中遇到一个关于电平选择的问题,居然给忘记RS232电平的定义了,当时无法反应上来,回来之后查找资料才了解两者之间的区别,视乎两年多的时间,之前非常熟悉的一些常识也开始淡忘,这个可不是一个好的现象. ...
- [5] Zygote
Android设备中的两大进程,如下图 1,由init进程创建的Daemon进程 2,由 Zygote进程创建的应用程序进程 什么是Zygote? zygote是“受精卵”的意思.在Android里, ...
- CPU与内存(经典问答)
原文:http://www.cnblogs.com/xkfz007/archive/2012/10/08/2715163.html 下面是网上看到的一些关于内存和CPU方面的一些很不错的文章. 整理如 ...
- springMVC部署
一.导入springMVC所需要的jar包 下载地址:http://repo.spring.io/release/org/springframework/spring/ 二.springM ...
- [前端JS学习笔记]JavaScript 数组
一.JavaScript数组的奇葩 大多数语言会要求数组的元素是同个类型, 但是JavaScript允许数组元素为多种类型. var arr = ["羽毛球", 666, {&qu ...
- 处理Oracle中杀不掉的锁
一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...
- 深入了解 Dojo 的服务器推送技术
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...