iOS 下实现代理设计模式
2013-09-26 08:53
459 查看
我们实现一个简单的代理设计模式,在一个试图控制器中点击按钮去对另一个试图控制器的控件进行操作。
本文代码例子:http://pan.baidu.com/s/1f1CkK
看代码吧
CustomViewViewController.h 要自己在XIB上拖一个Button啊
CustomViewViewController.m
ViewController.h
ViewController.m
本文代码例子:http://pan.baidu.com/s/1f1CkK
看代码吧
CustomViewViewController.h 要自己在XIB上拖一个Button啊
// // CustomViewViewController.h // CustomDelegate // // Created by 杜甲 on 13-9-26. // Copyright (c) 2013年 杜甲. All rights reserved. // #import <UIKit/UIKit.h> @protocol ChangeTextViewDelegate <NSObject> -(void)textEntered:(NSString*)text; @end @interface CustomViewViewController : UIViewController @property (assign,nonatomic) id<ChangeTextViewDelegate>delegate; -(IBAction)exchangeData; @end
CustomViewViewController.m
// // CustomViewViewController.m // CustomDelegate // // Created by 杜甲 on 13-9-26. // Copyright (c) 2013年 杜甲. All rights reserved. // #import "CustomViewViewController.h" @interface CustomViewViewController () @end @implementation CustomViewViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } -(IBAction)exchangeData { [_delegate textEntered:@"ffff"]; [self dismissViewControllerAnimated:YES completion:^{}]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
ViewController.h
// // ViewController.h // CustomDelegate // // Created by 杜甲 on 13-9-26. // Copyright (c) 2013年 杜甲. All rights reserved. // #import <UIKit/UIKit.h> #import "CustomViewViewController.h" @interface ViewController : UIViewController<ChangeTextViewDelegate> @property (retain, nonatomic) IBOutlet UILabel *myTest; @property (retain, nonatomic) CustomViewViewController* CTVC ; -(IBAction)changeText:(id)sender; @end
ViewController.m
// // ViewController.m // CustomDelegate // // Created by 杜甲 on 13-9-26. // Copyright (c) 2013年 杜甲. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.CTVC = [[CustomViewViewController alloc] init]; self.CTVC.delegate = self; } -(IBAction)changeText:(id)sender { [self presentViewController:self.CTVC animated:YES completion:^{ }]; } -(void)textEntered:(NSString *)text { self.myTest.text = text; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_myTest release]; [super dealloc]; } @end
相关文章推荐
- iOS中的触摸事件(TouchView) - (代理响应) - (实现touch的按钮化)(target/action设计模式,代理设计模式)重点
- iOS 设计模式-Block实现代理的逻辑
- iOS设计模式-Block实现代理的逻辑
- iOS 设计模式-Block实现代理的逻辑
- iOS_代理设计模式demo
- 你真的了解iOS代理设计模式吗?
- iOS设计模式学习 (二) OC协议之接口继承和代理设计
- 实例讲解如何在iOS应用开发中使用设计模式中的代理模式
- IOS设计模式------代理
- 设计模式C++实现(8)——代理模式
- IOS之Objective-C学习 代理设计模式
- GOF23设计模式之动态代理模式实现之经典
- 设计模式C++实现(8)——代理模式
- 你真的了解iOS代理设计模式吗?
- iOS 设计模式之代理模式(Proxy)
- 设计模式C++实现(8)——代理模式
- 玩转iOS开发:《iOS设计模式 — 代理模式》
- 数据库连接池代理实现(代理设计模式)-续数据库连接池
- 黑马程序员—IOS加强视频—利用协议实现代理模式
- [iOS]使用NSProxy实现代理模式