所有的视图都继承自 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)之间的切换,以及视图之间传值。的更多相关文章

  1. Objective-C ,ios,iphone开发基础:使用GDataXML解析XML文档,(libxml/tree.h not found 错误解决方案)

    使用GDataXML解析XML文档 在IOS平台上进行XML文档的解析有很多种方法,在SDK里面有自带的解析方法,但是大多情况下都倾向于用第三方的库,原因是解析效率更高.使用上更方便 这里主要介绍一下 ...

  2. Objective-C ,ios,iphone开发基础:几个常用类-NSNumber

    2013-08-21 在Objective-C,包括int double float 等等再内的基础数据类型都不是一个类,所以就不能给它们发送消息,也就是说不能调用方法,那怎么办呢 ?Objectiv ...

  3. [置顶] Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  4. Objective-C ,ios,iphone开发基础:UIAlertView使用详解

    UIAlertView使用详解 Ios中为我们提供了一个用来弹出提示框的类 UIAlertView,他类似于javascript中的alert 和c#中的MessageBox(); UIAlertVi ...

  5. Objective-C ,ios,iphone开发基础:快速实现一个简单的图片查看器

    新建一个single view 工程: 关闭ARC , 在.xib视图文件上拖放一个UIImageView  两个UIButton ,一个UISlider ,布局如图. 并为他们连线, UIImage ...

  6. Objective-C ,ios,iphone开发基础:JSON解析(使用苹果官方提供的JSON库:NSJSONSerialization)

    json和xml的普及个人觉得是为了简化阅读难度,以及减轻网络负荷,json和xml 数据格式在格式化以后都是一种树状结构,可以树藤摸瓜的得到你想要的任何果子. 而不格式化的时候json和xml 又是 ...

  7. Objective-C ,ios,iphone开发基础:http网络编程

    - (IBAction)loadData:(id)sender { NSURL* url = [NSURL URLWithString:@"http://162.105.65.251:808 ...

  8. Objective-C ,ios,iphone开发基础:3分钟教你做一个iphone手机浏览器

    第一步:新建一个Single View工程: 第二步:新建好工程,关闭arc. 第三步:拖放一个Text Field 一个UIButton 和一个 UIWebView . Text Field 的ti ...

  9. Objective-C ,ios,iphone开发基础:使用第三方库FMDB连接sqlite3 数据库,实现简单的登录

    第一步:下载第三方库,点击 连接 下载, 第二部:准备数据库:按照连接&中博客的步骤实现数据库, 数据库的设计大致如下表: id        username             pas ...

  10. Objective-C ,ios,iphone开发基础:ios数据库(The SQLite Database),使用终端进行简单的数据库操作

    SQLite  是一个轻量级的免费关系数据库.SQLite最初的设计目标是用于嵌入式系统,它占用资源非常少,在嵌入式设备中,只需要几百K的内存就够了,可以在(http://www.sqlite.org ...

随机推荐

  1. 自动化运维工具ansible-如何设置客户端多python版本问题

    问题:在使用ansible进行管理客户主机时,发现客户主机安装了多个版本的python,并且默认版本为3.0 shell>>cat list 192.168.2.9 shell>&g ...

  2. 学习使用Markdown标记语言

    学习如何使用Markdown进行文本编辑 使用教程   大家若是经常逛Github,就知道其中有一个文件叫做README.MD.我一开始也不知道这个.MD是什么意思,后来我自己写了一次,就知道了这一种 ...

  3. HDU 4884 TIANKENG’s rice shop (模拟)

    TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...

  4. oracle的substr和replace

    //我个人做的是更新表中某个字段下的所有内容带有中文括号的信息变为英文括号,具体做法如下 update 表名 set 列名 =replace(要修改的字段名,要替换掉的内容,要替换上去的新内容) su ...

  5. 转载yield关键字理解

    实现IEnumerable接口及理解yield关键字   [摘要]本文介绍实现IEnumerable接口及理解yield关键字,并讨论IEnumerable接口如何使得foreach语句可以使用. 本 ...

  6. Educational Codeforces Round 15 (A - E)

    比赛链接:http://codeforces.com/contest/702 A. Maximum Increase A题求连续最长上升自序列. [暴力题] for一遍,前后比较就行了. #inclu ...

  7. Base64.java 工具类

    import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOExceptio ...

  8. Cocos2d-x学习笔记(10)(CCMenu菜单)

    1.CCMenu创建方式 CCMenu* menu = CCMenu::create(cocos2d::CCMenuItem* item,--)參数为CCMenuItem菜单项的对象可变參数列表 2. ...

  9. WPF基础到企业应用系列7——深入剖析依赖属性(WPF/Silverlight核心)

    一. 摘要 首先圣殿骑士非常高兴这个系列能得到大家的关注和支持.这个系列从七月份開始到如今才第七篇,上一篇公布是在8月2日,掐指一算有二十多天没有继续更新了,最主要原因一来是想把它写好,二来是由于近期 ...

  10. Perl多进程

    perl作为一种解释性的语言,非常受广大系统管理员的欢迎,优点么就不多说了,坏处也有不少,比如对线程的支持,就一直不咋地,所以大多数情况下,我们都须要多个进程,来帮助我们完毕工作,闲话少说,上代码. ...