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];
相关新闻>>
- 发表评论
-
- 最新评论 更多>>