IOS开发使用委托delegate在不同窗口之间传递数据
来源:互联网 发布:网络歌曲很少歌曲 编辑:程序博客网 时间:2024/06/14 22:37
IOS开发使用委托delegate在不同窗口之间传递数据
本文转自:http://mobile.51cto.com/iphone-284116.htm
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容。在IOS开发里两个UIView窗口之间传递参数方法有很多,比如
1、使用SharedApplication,定义一个变量来传递.
2、使用文件,或者NSUserdefault来传递
3、通过一个单例的class来传递
4、通过Delegate来传递。
前面3种方法,暂且不说,这次主要学习如何使用通过Delegate的方法来在不同的UIView里传递数据
比如: 在窗口1中打开窗口2,然后在窗口2中填入一个数字,这个数字又回传给窗口1。
1、首先定义个一委托UIViewPassValueDelegate用来传递值
1 @protocol UIViewPassValueDelegate
2 // 必选方法
3 - (void)passValue:(NSString *)value;
4 // 可选方法
@optional
5 -(void)omethod;
6 @end
这个protocol 就是用来传递值
2、在窗口1的头文件里,遵守delegate协议,并实现delegate方法,
//a.h
7 #import <UIKit/UIKit.h>
8 #import "UIViewPassValueDelegate.h"
9
10 @interface DelegateSampleViewController : UIViewController <UIViewPassValueDelegate>
11 {
12 UITextField *_value;
13 }
14 @property(nonatomic, retain) IBOutlet UITextField *value;
15 - (IBAction)buttonClick:(id)sender;
16 @end
在a.m中实现这个委托的必选方法
17 - (void)passValue:(NSString *)value
18 {
19 self.value.text = value;
20 }
21 -(void)optionalMethod{
22 }
button的Click方法,打开窗口2,并将窗口2的delegate实现方法指向窗口1。
23 - (IBAction)buttonClick:(id)sender
24 {
25 ValueInputView *valueView = [[ValueInputView alloc] initWithNibName:@"ValueInputView" bundle:[NSBundle mainBundle]];
26 valueView.delegate = self; // 将a类中的self传递给b类的delegate
27 [self setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
28 [self presentModalViewController:valueView animated:YES];
29 }
第二个窗口的实现
b.h 文件
30 #import <UIKit/UIKit.h>
31 #import "UIViewPassValueDelegate.h"
32
33 @interface ValueInputView : UIViewController {
34
35 NSObject<UIViewPassValueDelegate> * delegate;
36 UITextField *_value;
37 }
38 @property(nonatomic, retain)IBOutlet UITextField *value;
39 @property(nonatomic, assign) NSObject<UIViewPassValueDelegate> * delegate; //这里用assign而不用retain是为了防止引起循环引用。
40 - (IBAction)buttonClick:(id)sender;
41 @end
b.m文件
42 #import "ValueInputView.h"
43 @implementation ValueInputView
44 @synthesize delegate;
45 @synthesize value = _value;
46 - (void)dealloc {
47 [self.value release];
48 [super dealloc];
49 }
50
51 - (IBAction)buttonClick:(id)sender
52 {
53 [delegate passValue:self.value.text]; // 通过delegate调用代理方法
54 NSLog(@"self.value.text is%@", self.value.text);
55 [self dismissModalViewControllerAnimated:YES];
56
57 }
58 - (void)didReceiveMemoryWarning {
59 // Releases the view if it doesn't have a superview.
60 [super didReceiveMemoryWarning];
61
62 // Release any cached data, images, etc. that aren't in use.
63 }
64
65 - (void)viewDidUnload {
66 [super viewDidUnload];
67 // Release any retained subviews of the main view.
68 // e.g. self.myOutlet = nil;
69 self.delegate = nil;
70 }
71
72
@end
- IOS开发使用委托delegate在不同窗口之间传递数据
- IOS开发使用委托delegate在不同窗口之间传递数据
- 【转】IOS开发使用委托delegate在不同窗口之间传递数据
- IOS开发使用委托delegate在不同窗口之间传递数据
- IOS开发使用委托delegate在不同窗口之间传递数据
- iphone 使用委托(delegate)在不同的窗口之间传递数据
- 使用委托(delegate)在不同的窗口之间传递数据
- iphone 使用委托(delegate)在不同的窗口之间传递数据
- 使用委托(delegate)在不同的窗口之间传递数据
- iphone 使用委托(delegate)在不同的窗口之间传递数据
- iphone 使用委托(delegate)在不同的窗口之间传递数据-转
- iphone 使用委托(delegate)在不同的窗口之间传递数据
- iPhone使用委托在不同的窗口之间传递数据
- iPhone使用委托在不同的窗口之间传递数据
- IOS开发使用委托delegate在…
- C#委托(delegate)简单示例:在两个form之间传递数据
- ios delegate代理,多个/不同/两个view之间传递数据,传递批量数据,多个数据
- MFC不同窗口之间传递数据
- 最大流、最小费用最大流【模板】
- Android 性能优化的一些方法(转)
- POJ 2528 Mayor's posters 线段树(成段更新+离散化)
- ubuntu10.04 安装lamp环境
- V2中已废弃或暂未支持的V1接口
- IOS开发使用委托delegate在不同窗口之间传递数据
- 设计模式——简单工厂
- 基于xulrunner架构下的Observer Mode
- 开始学习了,不能在这样下去了~~
- hdoj 3685 Rotational Painting
- 1114 Piggy-Bank
- 美法官驳回Facebook广告和解协议
- ejb3.0基础
- 学做jQuery中的data()函数