属性传值与代理传值
来源:互联网 发布:java用正则表达式 编辑:程序博客网 时间:2024/04/30 13:16
MVC通过控制中心C来完成信息的传递。常用的方式有,属性传值、代理传值、单例传值以及通过通知中心传值。
视图对象负责展示数据,通常它不会存储数据。数据存储在控制器中,视图通常会让控制器成为自己的委托,在显示数据时调用其委托中相应地方法。
为了降低类之间的耦合度,经常使用委托模式来进行传值。委托模式涉及到协议。
如下例子:
协议
#import <Foundation/Foundation.h>
@protocol ModifyTitleDelegate <NSObject>
- (void)modifyTitle:(NSString *)title;
@end
AViewController
#import <UIKit/UIKit.h>
#import "ModifyTitleDelegate.h"
@interface AViewController : UIViewController<ModifyTitleDelegate>
@end
#import "AViewController.h"
#import "BViewController.h"
@interface AViewController ()
@end
@implementation AViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
UIButton *btnTest = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btnTest setTitle:@"PUSH"forState:UIControlStateNormal];
btnTest.frame = CGRectMake(0, 0, 80, 30);
btnTest.center = self.view.center;
[btnTest addTarget:selfaction:@selector(pushToNext)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btnTest];
}
- (void)pushToNext{
BViewController *secondVC = [[BViewControlleralloc] init];
//属性传值
secondVC.title = @"Second";
//设置代理
secondVC.delegate = self;
[self.navigationControllerpushViewController:secondVC animated:YES];
}
- (void)modifyTitle:(NSString *)title
{
self.title = title;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
@end
BViewController
#import <UIKit/UIKit.h>
#import "ModifyTitleDelegate.h"
@interface BViewController : UIViewController
@property (assign,nonatomic) id <ModifyTitleDelegate>delegate;
@property (retain,nonatomic) UITextField *myTextField;
@end
#import "BViewController.h"
@interface BViewController ()
@end
@implementation BViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [superinitWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[superviewDidLoad];
UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:@"Test"forState:UIControlStateNormal];
btn.frame = CGRectMake(120,130, 80, 30);
[btn addTarget:selfaction:@selector(btnPressed)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
self.myTextField = [[UITextFieldalloc] initWithFrame:CGRectMake(120,80, 80, 30)];
self.myTextField.borderStyle = UITextBorderStyleRoundedRect;
[self.viewaddSubview:self.myTextField];
}
- (void)btnPressed
{
//代理传值
[self.delegatemodifyTitle:self.myTextField.text];
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}
- (void)dealloc
{
[_myTextField release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
@end
- 属性传值与代理传值
- 代理传值与属性传值简单描述
- 代理传值和属性传值
- iOS传值一属性传值,代理传值
- Swift3.0 代理传值,属性传值
- 代理 单例 属性 block 四大传值方式
- 传值方式(block, 属性,代理 ,单例)
- iOS传值(属性传值,代理传值,单例传值,通知传值,Block)
- ios 页面之间的传值:属性传值,代理传值,block传值
- iOS 传值方法(属性传值、代理传值、Block、通知、单例)
- IOS学习笔记(页面传值:属性传值,协议代理传值,闭包传值)
- IOS传值---属性传值、代理委托传值(delegate)传值、单例传值、block传值
- iOS开发:界面传值之属性和代理传值
- 传值有四种方法 : 1.属性传值 2.单例传值 3.代理传值 4.block传值
- UI day 8 导航控制器 属性传值 代理传值 单例传值
- iOS个人整理14-界面间传值:属性传值---代理传值---block传值---单例传值
- IOS 页面间传值(属性传值、block传值、代理传值、单例传值、NSNotificaion传值)
- swift基础学习传值[属性传值、代理、block代码块、等]
- 微软公司面试题【1】
- CTAGS解析PowerShell文件
- Android ListView滑动过程中图片显示重复错乱闪烁问题解决
- PHP源码初探之GCC简单介绍(二)
- Orion测试io性能的工具
- 属性传值与代理传值
- ADT安装遇到的问题 cannot perform operator
- 关于QT实现多线程服务器
- ioremap和ioport_map
- 6_双向循环链表
- ADB不是内部或外部命令解决方法
- 一些老的apk项目 使用maven编译不成功
- css_day9---盒子模型的综合案例
- ANSI C