ios中的delegate的理解运用
来源:互联网 发布:三国杀2015珍藏版淘宝 编辑:程序博客网 时间:2024/05/18 12:34
delegete---委托,在ios中View之间,已经ViewController之间数据传递等,都要用到这个概念。
现在虽然已经能运用了,可是最初刚开始接触这个概念的时候,很是头疼。
多亏一篇介绍得非常易懂的文章,才算渐渐开始理解,希望能给正在学习delegate的朋友一点帮助。
翻译的通俗化,也加了些自己的理解,有不准确的地方,还忘谅解。
例子:父画面的背景颜色,根据子画面的颜色选择来进行更改。
父画面ModalViewController 子画面ColorSetViewController
在ios中如孩子对父亲不能直接说的话,通过书信方式来传递。这个书信就是delegate.
子画面(孩子这边)
1:孩子首先确定要用书信 ,那么先在自己的头文件中宣布 (@protocol ColorSelectDelegate;)
2:孩子开始准备自己的书信名称。(id<ColorSelectDelegate> delegate;)
3:孩子准备自己的书信内容,根据需要有什么要传递的。(@protocol ColorSelectDelegate<NSobject>-(void)selectColor:(UIColor*)inColor; //这个delegate method,要选择颜色@end)
4:前面都是准备工作在.h中定义, 孩子在适当的时候,把书信发出去在.m中执行([self.delegate selectColor:requestColor];)
父画面(父亲这边)
1:父亲这边要等着孩子的书信。(@interface ModalViewController : UIViewController <ColorSelectDelegate>)
2:父亲和孩子联系,也需要给个适当的时机。让孩子把信交给父亲。这个就是在父画面调用子画面的时候。 子画面的ColorSetViewController.delegate = self;//这个self其实就是父亲
3:父亲接到孩子的书信后,知道该做什么来对应。(-(void)selectColor:(UIColor*)inColor; )//看到孩子书信后做些什么。
下面贴部分代码@protocol ColorSelectDelegate;@interface ColorSetViewController : UIViewController { id<ColorSelectDelegate> delegate; IBOutlet UIButton *redButton; IBOutlet UIButton *greenButton; IBOutlet UIButton *whiteButton; IBOutlet UIButton *cancelButton;}@property (nonatomic, assign) id<ColorSelectDelegate> delegate;@property (nonatomic, retain) IBOutlet UIButton *redButton;@property (nonatomic, retain) IBOutlet UIButton *greenButton;@property (nonatomic, retain) IBOutlet UIButton *whiteButton;@property (nonatomic, retain) IBOutlet UIButton *cancelButton;//*********************************************************// COLOR BUTTON- (IBAction)colorSelectButtonAction:(UIButton *)sender;@end//////////////////////////////////////////////////////////////#pragma mark Protocol ColorSelectDelegate//////////////////////////////////////////////////////////////@protocol ColorSelectDelegate-(void)selectColor:(UIColor*)inColor; @end
.m
#import "ColorSetViewController.h"#import "ModalViewController.h"@implementation ColorSetViewController@synthesize delegate;@synthesize redButton;@synthesize greenButton;@synthesize whiteButton;@synthesize cancelButton;- (void)viewDidUnload { [super viewDidUnload]; self.redButton = nil; self.greenButton = nil; self.whiteButton = nil; self.cancelButton = nil; self.delegate = nil;}// COLOR BUTTON- (IBAction)colorSelectButtonAction:(UIButton *)sender { UIColor* requestColor = nil; // 选择颜色 switch (sender.tag) { case kRED: requestColor = [UIColor redColor]; break; case kGREEN: requestColor = [UIColor greenColor]; break; case kWHITE: requestColor = [UIColor whiteColor]; break; case kCANCEL: requestColor = nil; break; default: break; } // 把信发出去 [self.delegate selectColor:requestColor];}@end
父画面
@interface ModalViewController : UIViewController <ColorSelectDelegate>{ IBOutlet UIBarButtonItem *colorButton;}@property (nonatomic, retain) IBOutlet UIBarButtonItem *colorButton;//*********************************************************// [COLOR] 与孩子通话的时机- (IBAction)ButtonAction:(UIBarButtonItem *)sender;@end
#import "ModalViewController.h"#import "ColorSetViewController.h"@implementation ModalViewController@synthesize colorButton;- (void)viewDidLoad { [super viewDidLoad];}- (void)viewDidUnload { self.colorButton = nil;}//*********************************************************// COLOR BUTTON- (IBAction)ButtonAction:(UIBarButtonItem *)sender { //孩子通话 ColorSetViewController *colorSetView = [[ColorSetViewController alloc] initWithNibName:@"ColorSetViewController" bundle:nil]; colorSetView.colorSelectdelegate_ = self; [self presentModalViewController:colorSetView animated:YES];}// 收到孩子的信,做些什么。-(void)selectColor:(UIColor*)inColor { // nil 什么都没有说。 if (inColor != nil) self.view.backgroundColor = inColor; // 改变背景颜色 [self dismissModalViewControllerAnimated:YES]; }@end
根据这个流程练习一下,能渐渐明白delegate的运用了。
之后再看些专业的解释,能更深入的掌握。
- ios中的delegate的理解运用
- 【iOS初级】delegate的理解
- iOS KVO的运用理解
- 关于delegate 与 protocol 的理解 iOS
- 关于iOS Delegate的个人理解
- 对iOS 中自定义delegate的理解
- 关于IOS delegate回调的理解
- IOS -- Delegate委托 的 使用与理解
- 关于delegate 与 protocol 的理解 iOS
- iOS页面之间传值-----delegate的运用
- iphone开发中的delegate的理解一
- 关于objectiveC中的delegate的理解
- IOS - 理解委托 - delegate
- iOS中的delegate的用法和规范
- iOS中的delegate的用法和规范
- iOS中的delegate的用法和规范
- iOS中的delegate的用法和规范
- iOS中的delegate的用法和规范
- 两个有序数组,A[k]和B[k]长度都为k。求前k个最小的(a[i]+b[j])
- Struts1与Struts2原理 区别 详解 汇总
- 编译原理之文法二
- 配置httpd - linux
- hdu2603
- ios中的delegate的理解运用
- hdu 1215(七夕节)
- 高手之Convert C++ To C#
- String与Stringbuilder的联系
- sizeof()用法汇总
- Unix网络编程 chapter习题代码
- 程序员面试宝典之数据结构基础----④单链表排序(读后)
- 纯dom动画框架可以满足常用的需求 包含四种常用动画样式(匀速,减速,橡皮筋,反弹)
- GDI+ 自学4