委托模式精讲
#import "SetViewController.h"
@interfaceReadViewController ()<colorDelegate>//ReadViewController这个类要遵循协议
@end
@implementation ReadViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
self.title = @"阅读";
UITextView *text = [[UITextView alloc]initWithFrame:self.view.bounds];
text.userInteractionEnabled = NO;
text.font = [UIFont systemFontOfSize:20];
text.tag = 1;
text.text = @"可不管对方可不可靠的放开保护开关迪佛如果认为是理科高考热死我也hi看认可是公开表示可认定为快高考人数为客户立刻同意和认可可以和客人可给他发可爱我的身高赶快回来你风格和空调管理和你了就会让他理解和内容与讨论哦豁咯快乐不过来后来通过联合厉害不人道了法国和老板了让他聊天如何管理法律的公会聊天聊天人干活了人聊天的好了不让他开会不反抗的效果不好开发过开放不会看人的反馈给好看热得快干活离开过人的可观可包括美国方面还";
[self.view addSubview:text];
UIBarButtonItem *item = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:@selector(edit )];
self.navigationItem.rightBarButtonItem = item;
}
-(void) edit
{
SetViewController *setvc = [[SetViewControlleralloc]init];
setvc.delegate = self;//
[self.navigationControllerpushViewController:setvc animated:YES];
}
-(void) colorchange:(BOOL)sign//委托方法的实现
{
if (sign) {
self.view.backgroundColor = [UIColorgrayColor];
UITextView *te = (UITextView *) [self.view viewWithTag:1];
te.backgroundColor = [UIColor grayColor];
te.textColor = [UIColor whiteColor];
}
else
{
self.view.backgroundColor = [UIColorwhiteColor];
UITextView *te = (UITextView *) [self.view viewWithTag:1];
te.backgroundColor = [UIColor whiteColor];
te.textColor = [UIColor grayColor];
}
}
//////////////////////////////////////////////////////////////////////////////
被委托方
#import <UIKit/UIKit.h>
@protocol colorDelegate <NSObject> //声明一个协议
-(void) colorchange:(BOOL) sign;
@end
@interface SetViewController : UIViewController
@property (nonatomic ,weak) id<colorDelegate> delegate;//声明一个遵循协议的属性
@end
#import "SetViewController.h"
@interfaceSetViewController ()
@end
@implementation SetViewController
- (void)viewDidLoad {
[superviewDidLoad];
self.view.backgroundColor = [UIColorwhiteColor];
UISwitch *s= [[UISwitchalloc]initWithFrame:CGRectMake(80, 90, 50, 10)];
[s addTarget:selfaction:@selector(changcolor:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:s];
}
-(void)changcolor:(UISwitch *) sw
{
if (sw.isOn)
self.view.backgroundColor = [UIColorgrayColor];
else
self.view.backgroundColor = [UIColorwhiteColor];
[_delegate colorchange:sw.isOn];
}