在iOS开发中,时常会用到按钮,通过按钮的点击来完成界面的跳转等功能。具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
写在前面
在iOS开发中,时常会用到按钮,通过按钮的点击来完成界面的跳转等功能。按钮事件的实现方式有多种,其中较为常用的是目标-动作对模式。但这种方式使得view与controller之间的耦合程度较高,不推荐使用;
另一种方式是代理方式,按钮的事件在view中绑定,controller作为view的代理实现代理方法。
目标-动作对实现方式
具体来说,假设我们有一个包含一个Button的veiw,view将Button放在头文件中,以便外部访问。然后controller将view作为自己的view,在viewcontroller中实现按钮的点击事件。文字描述起来好像不够直观,直接上代码
1、MyView.h
包含一个可被外部访问的按钮的view
@interface MyView : UIView
@property (strong, nonatomic) UIButton *myBtn;
@end
2、MyView.m
#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
3、MyViewController.h
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController
@end
4、MyViewController.m
添加MyView作为自身view
#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.");
}
5、 AppDelegate.m
#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;
}
6、运行结果
界面:
#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
2、MyView.m
按钮被封装在.m文件中,同时在.m文件中提供一个本地方法,在本地方法中调用代理的代理方法
#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
3、MyViewController.h
同上(目标-动作对实现方式)
4、MyViewController.m
#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.");
}
5、AppDelegate
同上(目标-动作对实现方式)
6、运行结果
界面同上
日志:
7、小结
从日志可以看出,使controller成为view的代理,实现按钮的代理方法,与按钮相关的方法的执行顺序为:view中按钮的动作方法->controller提供的按钮代理方法。
事实上,在代理模式中,有三个角色存在:
- 协议:一般是方法列表,规定了代理双方行为,在本例中 就是协议;
- 代理:遵循一定的协议的类,需要实现协议中的必须方法,完成委托方的功能,本例中MyViewController就是代理;
- 委托:拥有自己的代理,指定代理去完成功能,本例中的MyView就是委托。
代理模式用大白话说就是:委托方让代理方代替自己执行一定的动作。
总结
iOS中,类不能多继承,但协议是可以多继承的。协议并不提供具体实现。协议一般是一系列方法的集合,(也可以有属性,但这不是协议的主要使用场景),这有点像Java中的接口,继承接口的类负责提供接口中方法的具体实现。
代理模式在iOS开发中使用的地方有很多,代理模式能够实现view和controller之间的解耦。拿本文中的例子来说,controller虽然可以操作view中按钮点击后的操作,但由于按钮是作为view的私有属性声明在view的实现文件中的,因此controller并不知道view中有按钮这个属性的存在,因此无法从view外部去更改按钮的各属性,这就是view和controller之间解耦的体现。此外,由于按钮事件是在view中绑定的,而不是在controller中绑定的,因此使用该view的类只需要实现相应的代理方法就可以定制按钮点击后的事件了,这也更加方便了view的复用,体现了view与controller解耦合的优势。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程学习网。
本文标题为:详解iOS中按钮点击事件处理方式


- 作为iOS开发,这道面试题你能答出来,说明你基础很OK! 2023-09-14
- iOS 对当前webView进行截屏的方法 2023-03-01
- Android实现轮询的三种方式 2023-02-17
- Android实现监听音量的变化 2023-03-30
- SurfaceView播放视频发送弹幕并实现滚动歌词 2023-01-02
- 最好用的ios数据恢复软件:PhoneRescue for Mac 2023-09-14
- Flutter实现底部和顶部导航栏 2022-08-31
- Android MaterialButton使用实例详解(告别shape、selector) 2023-06-16
- 详解flutter engine 那些没被释放的东西 2022-12-04
- Android studio实现动态背景页面 2023-05-23