iOS 委托模式(5)
来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:次
p>
p>下面还是贴代码,应该都是很容易看懂的。
p>
p>MyView.h
p>
p>
p>
p>[cpp]
p>#import <UIKit/UIKit.h>
p>
p>@protocol MyDelegate <NSObject>
p>
p>-(void)print:(NSString*)viewName;
p>
p>@end
p>
p>@interface MyView : UIView
p>
p>@property(nonatomic,assign)id<MyDelegate> mydelegate;
p>
p>@end
p>
p>#import <UIKit/UIKit.h>
p>
p>@protocol MyDelegate <NSObject>
p>
p>-(void)print:(NSString*)viewName;
p>
p>@end
p>
p>@interface MyView : UIView
p>
p>@property(nonatomic,assign)id<MyDelegate> mydelegate;
p>
p>@end
p>MyView.m
p>
p>
p>
p>[cpp]
p>#import "MyView.h"
p>
p>@implementation MyView
p>
p>
p>@synthesize mydelegate = _mydelegate;
p>
p>- (id)initWithFrame:(CGRect)frame
p>{
p> self = [super initWithFrame:frame];
p> if (self) {
p>
p> //代码创建一个button
p> UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
p> [button setTitle:@"Button" forState:UIControlStateNormal];
p> [button setFrame:CGRectMake(10, 10, 100, 50)];
p> [button setTintColor:[UIColor blueColor]];
p>
p> //Target-Action模式 为button指定事件处理对象target为self,事件处理方法为buttonPressed
p> [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
p> [self addSubview:button];
p>
p> }
p> return self;
p>}
p>//事件处理的响应方法
p>-(void)buttonPressed{
p>
p> [_mydelegate print:@"this is a view"];
p>}
p>
p>@end
p>
p>#import "MyView.h"
p>
p>@implementation MyView
p>
p>
p>@synthesize mydelegate = _mydelegate;
p>
p>- (id)initWithFrame:(CGRect)frame
p>{
p> self = [super initWithFrame:frame];
p> if (self) {
p>
p> //代码创建一个button
p> UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
相关新闻>>
- 发表评论
-
- 最新评论 进入详细评论页>>
今日头条
更多>>您可能感兴趣的文章
- iPhone开发 调用阿asp.net程序的webservice
- iphone开发基础三和c语言混编
- 深入理解iPhone静态库(手把手教你iphone开发
- iOS文件存磁盘的设想
- (iOS-iap防护)验证用户付费收据,拒绝iap
- ios 时间定时器 NSTimer应用demo
- ios 关于UITableView UITableViewCellAccessoryCheckmark 混乱的
- Cocoa Core Competencies 的Object creation 和IOS面试题示例
- iPhone开发 文件的增加删除查询
- iphone-使用TextField及关闭键盘(useing TextField for in
新闻点评
新闻关注排行榜
热门推荐
最新推荐