
参考:
http://blog.sina.com.cn/s/blog_4431c7610100vgds.HTML
http://blog.163.com/zhe8stianshi@126/blog/static/17176291920117233111605/
http://blog.csdn.net/riveram/article/details/7263322
1)采用代理模式 子vIEwcontroller设计 代理协议,定义协议接口,父vIEwcontroller 实现协议接口,实现子vIEwcontroller 退出时将相关数据更新到父视图。
2)采用ios的消息机制 父vIEwcontroller注册消息 子vIEwcontroller 发送消息,触发父vIEwcontroller的消息处理。
3)采用database做为数据中间的存储媒介,子vIEwcontroller将状态数据存入DB,父vIEwcontroller从DB获取数据更新vIEw。
4)采用ios的NSDefault 存储
5)通过AppDelegate 中定义全局变量实现中间数据的存储。
UIVIEwController和UIVIEwController之间的交互
1 单向交互。
2 双向交互,共享数据
就是使用MVC模式(UIVIEwController本身就是MVC中的C)。这个时候,shareData就不应该再是一个普通的数据,而应该封装成model,当model中的数据发生了改变的时候,会发出通知告诉C(和/或V)。这样的话,两个controller之间就比较松散了。引入dataModel后,伪代码如下:
[dataModel
3 双向交互,逆向消息传递
4 双向交互,大量逆向消息传递
最后还要补充说明一些内容。
iphone 使用委托(delegate)在不同的窗口之间传递数据
在IOS里两个UIVIEw窗口之间传递参数方法有很多,比如
1.使用SharedApplication,定义一个变量来传递.
2.使用文件,或者NSUserdefault来传递
3.通过一个单例的class来传递
4.通过Delegate来传递。
前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIVIEw里传递数据 。
比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。
窗口1
窗口2
窗口2的结果传递给窗口1
1.首先定义个一委托UIVIEwPassValueDelegate用来传递值
- ( voID )passValue:(Nsstring * )value;
@end
这个protocol 就是用来传递值
2.在窗口1的头文件里,声明delegate
#import < UIKit / UIKit.h >#import " UIVIEwPassValueDelegate.h "
@interface DelegateSampleVIEwController : UIVIEwController < UIVIEwPassValueDelegate >
{
UITextFIEld * _value;
}
@property(nonatomic, retain) IBOutlet UITextFIEld * value;
- (IBAction)buttonClick:(ID)sender;
@end
并实现这个委托
- ( voID )passValue:(Nsstring * )value
{
self.value.text = value;
NSLog( @" the get value is %@ " , value);
}
button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。
{
ValueinputVIEw * valueVIEw = [[ValueinputVIEw alloc] initWithNibname: @" ValueinputVIEw " bundle:[NSBundle mainBundle]];
valueVIEw. delegate = self; // 把两个VC关联起来!
[self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentModalVIEwController:valueVIEw animated:YES];
}
第二个窗口的实现
.h 头文件
#import < UIKit / UIKit.h >
#import " UIVIEwPassValueDelegate.h "
@interface ValueinputVIEw : UIVIEwController {
NSObject < UIVIEwPassValueDelegate > * delegate ;
UITextFIEld * _value;
}
@property(nonatomic, retain)IBOutlet UITextFIEld * value;
@property(nonatomic, retain) NSObject < UIVIEwPassValueDelegate > * delegate ;
- (IBAction)buttonClick:(ID)sender;
@end
.m实现文件
#import " ValueinputVIEw.h "
@implementation ValueinputVIEw
@synthesize delegate ;
@synthesize value = _value;
- ( voID )dealloc {
[self.value release];
[super dealloc];
}
- (IBAction)buttonClick:(ID)sender
{
[ delegate passValue:self.value.text]; // 具体的实现在第一个VC里。
NSLog( @" self.value.text is%@ " , self.value.text);
[self dismissModalVIEwControllerAnimated:YES];
}
- ( 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.
}
- ( voID )vIEwDIDUnload {
[super vIEwDIDUnload];
// Release any retained subvIEws of the main vIEw.
// e.g. self.myOutlet = nil;
}
/*
// Only overrIDe drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (voID)drawRect:(CGRect)rect {
// Drawing code.
}
*/
@end
废话就不说了,直接上步骤和代码:
1 创建一个基于Navigation-based Application的iphone工程,为什么要创建基于Navigation-based Application的工程呢,因为这样系统就会自动将Navigation视图加到我们的窗口视图中,这样我们就不用自己手动去加,并且可以用
[self.navigationControllerpushVIEwController:othervIEwanimated:YES]去跳转页面。当设置每个页面的标题后会在页面左上角自动生成后退导航按钮,多方便呀,当然需要在建立后将xib文件里面的tablevIEw去掉加入vIEw。
2 新建一个页面,我这里是OtherVIEw,让系统自动生成.h,.xib文件。
3 第一个页面上面添加一个文本筐和一个按钮,点击按钮后调转到第二个页面并将文本筐里面的数据传入第二个页面。第二个页面用一个label来显示传过来的数据。
4 代码:
首先是自动生成的协议里面的代码,注意看navigationController:
MynavAppDelegate.h代码:
[HTML] view plain copy #import <UIKit/UIKit.h> @interface MynavAppDelegate : NSObject UIApplicationDelegate> { } @property (nonatomic, retain) IBOutlet UIWindow *window; @end
MynavAppDelegate.m代码:
[cpp] copy #import "MynavAppDelegate.h" @implementation MynavAppDelegate @synthesize window=_window; @synthesize navigationController=_navigationController; - (BOol)application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window.rootVIEwController = self.navigationController; [self.window makeKeyAndVisible]; return YES; - (voID)applicationWillResignActive:(UIApplication *)application { } - (voID)applicationDIDEnterBackground:(UIApplication *)application voID)applicationWillEnterForeground:(UIApplication *)application voID)applicationDIDBecomeActive:(UIApplication *)application voID)applicationWillTerminate:(UIApplication *)application voID)dealloc [_window release]; [_navigationController release]; [super dealloc]; 第一个页面的代码:
RootVIEwController.h
copy #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface RootVIEwController : UIVIEwController { IBOutlet UITextFIEld * message;//需要传出的数据 @property(nonatomic,retain) UITextFIEld * message; -(IBAction) send;//按钮点击方法 RootVIEwController.m
copy #import "RootVIEwController.h" #import "OtherVIEw.h" @implementation RootVIEwController @synthesize message; -(IBAction) send{ OtherVIEw *othervIEw = [[OtherVIEw alloc] initWithNibname:@"OtherVIEw" bundle:nil]; othervIEw.mystring= message.text; [self.navigationController pushVIEwController:othervIEw animated:YES]; [othervIEw release]; voID)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { UItouch *touch = [[event alltouches] anyObject]; if (touch.tapCount >= 1) { [message resignFirstResponder]; } voID)vIEwDIDLoad self.Title = @"第一个页面"; [super vIEwDIDLoad]; voID)dIDReceiveMemoryWarning // Releases the vIEw if it doesn't have a supervIEw. [super dIDReceiveMemoryWarning]; // Relinquish ownership any cached data, etc that aren't in use. voID)vIEwDIDUnload [super vIEwDIDUnload]; // Relinquish ownership of anything that can be recreated in vIEwDIDLoad or on demand. // For example: self.myOutlet = nil; [message release]; [super dealloc]; @end
第二个页面代码:
OtherVIEw.h
copy @interface OtherVIEw : UIVIEwController { IBOutlet UILabel * mylabel;//用来显示传入的数据 Nsstring * mystring;//数据传入用到的属性 @property(nonatomic,retain) UILabel * mylabel; @end OtherVIEw.m
copy #import "OtherVIEw.h" @implementation OtherVIEw @synthesize mylabel,mystring; - (ID)initWithNibname:(Nsstring *)nibnameOrNil bundle:(NSBundle *)nibBundleOrNil self = [super initWithNibname:nibnameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; voID)dealloc [mylabel release]; [mystring release]; // Release any cached data,248); line-height:17.27272605895996px"> #pragma mark - VIEw lifecycle voID)vIEwDIDLoad [super vIEwDIDLoad]; self.Title = @"第二个页面"; self.mylabel.text=mystring; // mylabel.text = mystring; // Do any additional setup after loading the vIEw from its nib. // 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); 总结
以上是内存溢出为你收集整理的iOS的View之间的数据传递全部内容,希望文章能够帮你解决iOS的View之间的数据传递所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)