@interface MyView : UIView @property (strong, nonatomic) UIButton *myBtn; @end
#import "MyView.h"
@implementation MyView
//view的初始化方法
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{ //初始化按钮
_myBtn = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 100, 50)];
_myBtn.backgroundColor = [UIColor redColor];
//将按钮添加到自身
[self addSubview:_myBtn];
}
return self;
}
@end
#import <UIKit/UIKit.h> @interface MyViewController : UIViewController @end
#import "MyViewController.h"
#import "MyView.h"
@interface MyViewController ()
@property (strong, nonatomic) MyView *myview;
@end
@implementation MyViewController
- (void)loadView
{
MyView *myView = [[MyView alloc] initWithFrame: [[UIScreen mainScreen] bounds] ];
self.view = myView;
self.myview = myView;
//在controller中设置按钮的目标-动作,其中目标是self,也就是控制器自身,动作是用目标提供的BtnClick:方法,
[self.myview.myBtn addTarget:self
action:@selector(BtnClick:)
forControlEvents:UIControlEventTouchUpInside];
}
//MyView中的按钮的事件
- (void)BtnClick:(UIButton *)btn
{
NSLog(@"Method in controller.");
NSLog(@"Button clicked.");
}
#import "AppDelegate.h"
#import "MyViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [ [UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds ] ];
MyViewController *myVC = [[MyViewController alloc] init];
self.window.rootViewController = myVC;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#import <UIKit/UIKit.h> //自定义的按钮协议,该协议实现了<NSObject>协议,协议的名称自定,不过不要和Apple的协议重名 @protocol myBtnDelegate <NSObject> //协议中的方法,遵循该协议的类提供其具体的实现,协议有@optional和@required两个修饰符,默认情况下是@required - (void) BtnClick:(UIButton *)btn; @end //MyView的接口 @interface MyView : UIView //声明一个属性,这个属性用于指定谁来成为本类的代理,由于不能确定什么类型的对象会成为本类的代理,因此声明为id类型 @property (weak, nonatomic) id<myBtnDelegate> delegate; @end
#import "MyView.h"
@interface MyView ()
//声明在.m中的按钮对外部不可见
@property (strong, nonatomic) UIButton *myBtn;
@end
@implementation MyView
//初始化
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_myBtn = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 100, 50)];
_myBtn.backgroundColor = [UIColor redColor];
//为按钮设置目标-动作,其中目标是self即包含该按钮的view自身,动作是有目标(view)提供的myBtnClick:方法
[_myBtn addTarget:self
action:@selector(myBtnClick:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_myBtn];
}
return self;
}
//view中按钮的事件
- (void)myBtnClick:(UIButton *)btn
{
NSLog(@"Method in view");
//在回调代理方法时,首先判断自身的代理是否实现了代理方法,否则会导致崩溃
//如果自身代理实现了代理方法,在该方法中回调代理实现的具体的代理方法
if ( [self.delegate respondsToSelector:@selector(BtnClick:)] )
{
[self.delegate BtnClick: btn];
}
else
{
NSLog(@"BtnClick: haven't found in delegate.");
}
}
@end
#import "MyViewController.h"
#import "MyView.h"
//声明该controller遵循 <myBtnDelegate>协议,因此需要实现协议中的方法
@interface MyViewController () <myBtnDelegate>
@end
@implementation MyViewController
- (void)loadView
{ //创建MyView类型的myView
MyView *myView = [[MyView alloc] initWithFrame: [[UIScreen mainScreen] bounds] ];
//将myView的代理设置为self,即当前controller自身
myView.delegate = self;
//将controller的view指向myView
self.view = myView;
}
//该方法是代理中的方法,在controller中决定点击myBtn按钮后具体要做的事情,但controller并不能直接获取到myBtn
- (void)BtnClick:(UIButton *)btn
{
NSLog(@"Method in controller.");
NSLog(@"Button clicked.");
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有