iOS delegate, 代理/委托与协议.
之前知知道iOS协议怎么写, 以为真的跟特么java接口一样, 后来发现完全不是.
首先, 说说应用场景, 就是当你要用一个程序类, 或者说逻辑类, 去控制一个storyboard里面的label, 发现如果直接用
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
//由storyboard根据myView的storyBoardID来获取我们要切换的视图
mainView= [mainStoryboard instantiateViewControllerWithIdentifier:@"myView"];
然后去拿这个mainView的label的property, 结果发现这个viewController是可以拿到的, 但是里面的label是nil的.
结果搜索了一下, 发现逻辑类是无法访问视图控制类的(the real problem is no one told me that after I asked so many people, and it's not a fucking unsual issue, correct?).
那么好吧, 这个时候, 据说其中一个解决方案就是这个delegate/protocal这个鬼东西.
首先, 把逻辑类叫做Logic, 视图控制器类就还是ViewController, 在Logic.h里面:
#import <Foundation/Foundation.h>
@protocol LogicDelegate <NSObject>
- (void)DoSomethingOthersCant;
@end
@interface Logic : NSObject
@property (nonatomic, assign) id <LogicDelegate> delegate;
- (void) changeText;
@end
分为两个部分, 一个就是定义协议, 协议名通常是类名+delegate, 在这个例子里面是LogicDelegate, 这部分跟java的接口一个意思, 定义一下抽象方法, 这个例子里面就是DoSomthingOthersCant.
另一个部分就是定义一个property, 也就是谁实现了这个LogicDelegate接口/协议, 就是他了
再有一个方法是给逻辑类使用代理方法的.
那么, Logic.m里面:
#import "Logic.h"
#import "ViewController.h"
@interface Logic ()
{
}
@property (strong,nonatomic) ViewController *viewController;
@end
@implementation Logic
- (void) changeText{
NSLog(@"xxxx");
[self.delegate DoSomethingOthersCant];
}
@end
具体的, changeText就在逻辑需要的时候, 去让代理者, 比如视图控制器, 来doSomething它不能do的.
ok, 到了视图控制器类, 首先要指定这个类, 实现LogicDelegate协议.
#import <UIKit/UIKit.h>
#import "Logic.h"
@interface ViewController : UIViewController<LogicDelegate> //这儿
@property (weak, nonatomic) IBOutlet UILabel *label;
@end
实现类:
#import "ViewController.h"
#import "Logic.h"
@interface ViewController ()
@property Logic *logic;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.logic=[[Logic alloc]init];
self.logic.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)pressed:(id)sender {
[self.logic changeText ];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)DoSomethingOthersCant
{
NSLog(@"dododododo");
self.label.text=@"DDDDDDd";
}
@end
首先在viewDidLoad的时候把viewController传到logic的实例的delegate属性.
self.logic.delegate = self;
然后定义协议里面要求实现的方法, DoSomethingOthersCant, 内容就是将label的值改变.
简单来说, 逻辑类定义协议, 协议里面有抽象方法, UIViewController类, 就做代理, 具体定义抽象类的实现, 同时要再viewDidLoad的时候, 逻辑property的delegate要把self传进去.
多练习吧, 只能这样了.
iOS delegate, 代理/委托与协议.的更多相关文章
- iOS - Delegate 代理
1.Delegate 1.1 协议 协议:是多个类共享的一个方法列表.协议中列出的方法没有相应的实现,计划由其他人来实现.协议中列出的方法,有些是可以选择实现,有些是必须实现. 1>.如果你定义 ...
- ios delegate 代理模式 观察者模式 不同视图间的通信
delegate,在ios中比比皆是,NSURLConnection(网络请求有),tableView, connectionView,等系统自带 的常见代理.甚至,自己写代码的时候,随意间敲打出了p ...
- protocol(协议) 和 delegate(委托)也叫(代理)---辨析
protocol和delegate完全不是一回事. 协议(protocol),(名词)要求.就是使用了这个协议后就要按照这个协议来办事,协议要求实现的方法就一定要实现. 委托(delegate),(动 ...
- [置顶] Objective-C ,ios,iphone开发基础:protocol 协议(委托,代理)的声明
协议是为了弥补Objective-c中类只能单继承的缺陷,在Objective-c2.0之前当一个类遵循一个协议的时候,必须在类中实现协议的所有方法,在Objective-c2.0之后协议中的方法就有 ...
- ios中关于delegate(委托)的使用心得
ios中关于delegate(委托)的使用心得 分类: iOS开发2012-05-15 10:54 34793人阅读 评论(9) 收藏 举报 iosuiviewtimerinterfaceprinti ...
- 【IOS学习】之四、协议,委托,分类粗解
何为协议,何为委托,何为分类(类别)? 委托 即 代理 delegate: 他是 (接口的实现类)类似c中的回调. 把某个对象要做的事情委托给别的对象去做. 那么别的对象就是这个对 ...
- iOS阶段学习第30天笔记( UIViewController—Delegate(代理) )
iOS学习(UI)知识点整理 一.UIViewController的介绍 1)概念:UIViewController 即视图控制器,用来管理和控制页面跳转的一个类 ,iOS里面采用了MVC的体系结构, ...
- [IOS Delegate和协议]
转载请注明出处 http://blog.csdn.net/pony_maggie/article/details/25655443 作者:小马 代理和协议的语法这里不赘述,自己查资料. 这个demo的 ...
- iOS 委托和协议区别和联系
iOS上的协议类似于C#.Java上面的接口,他是从类中抽出来的一系列方法,但方法的实现是在实现这个协议的类中,任何实现这个协议的类都需要实现协议类中的@require方法: 委托是一种设计模式,是一 ...
随机推荐
- LintCode Kth Largest Element
原题链接在这里:http://www.lintcode.com/en/problem/kth-largest-element/# 在LeetCode上也有一道,采用了标准的quickSelect 方法 ...
- Oracle一个实例配置多个监听
要想给一个Oracle实例配置多个监听,首先要定义多个监听器,因为是多个监听,势必会有一些监听端口不是1521. 现在服务端的listener.ora文件中定义如下监听器: LISTENER = (D ...
- php数组遍历
<?php $arr = array('a','b','c','d','e','f'); //for语句只能遍历索引数组 for($i = 0; $i < 6; $i++){ echo $ ...
- AWK高级编程 转载
AWK高级编程 转载 转载自:http://blog.csdn.net/wzhwho/article/details/5513791 1. 程序元素 一个awk 程序是一对以模式(pattern) 与 ...
- php Memcache
<?php $mem = new Memcache();//实例化一个对象 $mem->connect("localhost",11211);//连接memcache服 ...
- ASP.NET IIS设置 Session时间
1.打开IIS需设置的网站主页 2.打开主页IIS--ASP项目,如下图: 3.设置 会话属性---超时 的值,如下图:
- Java基础——数组应用之StringBuilder类和StringBuffer类
接上文:Java基础——数组应用之字符串String类 一.StringBuffer类 StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和 ...
- git 查看、创建、删除 本地,远程 分支
1. 查看远程分支 git branch -rorigin/master 2. 查看本地分支 git branch *master 注:以*开头指明现在所在的本地分支 3. 查看本地分支和远程分支 g ...
- Windows下MongoDB安装与PHP扩展
MongoDB是什么就不再累述了,下面只写MongoDB安装与PHP扩展的方法. 一,安装准备 MongoDB 如果网速慢,可以到MongoDB中文社区的百度网盘下载,密码3gun.(根据你的操作系统 ...
- 使用duplicate target database ... from active database复制数据库
使用duplicate target database ... from active database复制数据库 source db:ora11auxiliary db:dupdb 1.修改监听文件 ...