詳解iOS中Button按鈕的狀態(tài)和點(diǎn)擊事件
一、按鈕的狀態(tài)
1.UIControlStateNormal
1> 除開UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情況,都是normal狀態(tài)
2> 這種狀態(tài)下的按鈕【可以】接收點(diǎn)擊事件
2.UIControlStateHighlighted
1> 【當(dāng)按住按鈕不松開】或者【highlighted = YES】時(shí)就能達(dá)到這種狀態(tài)
2> 這種狀態(tài)下的按鈕【可以】接收點(diǎn)擊事件
3.UIControlStateDisabled
1> 【button.enabled = NO】時(shí)就能達(dá)到這種狀態(tài)
2> 這種狀態(tài)下的按鈕【無法】接收點(diǎn)擊事件
4.UIControlStateSelected
1> 【button.selected = YES】時(shí)就能達(dá)到這種狀態(tài)
2> 這種狀態(tài)下的按鈕【可以】接收點(diǎn)擊事件
二、讓按鈕無法點(diǎn)擊的2種方法
1> button.enabled = NO;
*【會(huì)】進(jìn)入U(xiǎn)IControlStateDisabled狀態(tài)
2> button.userInteractionEnabled = NO;
*【不會(huì)】進(jìn)入U(xiǎn)IControlStateDisabled狀態(tài),繼續(xù)保持當(dāng)前狀態(tài)
三、iOS中按鈕點(diǎn)擊事件處理方式
在iOS開發(fā)中,時(shí)常會(huì)用到按鈕,通過按鈕的點(diǎn)擊來完成界面的跳轉(zhuǎn)等功能。按鈕事件的實(shí)現(xiàn)方式有多種,其中
較為常用的是目標(biāo)-動(dòng)作對(duì)模式。但這種方式使得view與controller之間的耦合程度較高,不推薦使用;
另一種方式是代理方式,按鈕的事件在view中綁定,controller作為view的代理實(shí)現(xiàn)代理方法。
目標(biāo)-動(dòng)作對(duì)實(shí)現(xiàn)方式
具體來說,假設(shè)我們有一個(gè)包含一個(gè)Button的veiw,view將Button放在頭文件中,以便外部訪問。然后controller將view作為自己的view,在viewcontroller中實(shí)現(xiàn)按鈕的點(diǎn)擊事件。
文字描述起來好像不夠直觀,直接上代碼
1、MyView.h
包含一個(gè)可被外部訪問的按鈕的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中設(shè)置按鈕的目標(biāo)-動(dòng)作,其中目標(biāo)是self,也就是控制器自身,動(dòng)作是用目標(biāo)提供的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、運(yùn)行結(jié)果
界面:

輸出:

總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望能對(duì)大家的學(xué)習(xí)或者工作帶來一定的幫助,如果有疑問大家可以留言交流。
- 關(guān)于iOS導(dǎo)航欄返回按鈕問題的解決方法
- IOS UITableViewCell詳解及按鈕點(diǎn)擊事件處理實(shí)例
- iOS開發(fā)中UISwitch按鈕的使用方法簡介
- 詳解iOS應(yīng)用中自定義UIBarButtonItem導(dǎo)航按鈕的創(chuàng)建方法
- 詳解iOS-按鈕單選與多選邏輯處理
- iOS應(yīng)用開發(fā)中導(dǎo)航欄按鈕UIBarButtonItem的添加教程
- iOS App中UITableView左滑出現(xiàn)刪除按鈕及其cell的重用
- 學(xué)習(xí)iOS開關(guān)按鈕UISwitch控件
- iOS 防止按鈕多次點(diǎn)擊造成多次響應(yīng)的方法
- iOS實(shí)現(xiàn)全局懸浮按鈕
相關(guān)文章
淺談強(qiáng)大易用支持URL Rewrite的iOS路由庫FFRouter
FRouter 是 iOS 中一個(gè)強(qiáng)大且易用的 URL 路由庫,支持 URL Rewrite,基于匹配查找 URL,效率高。非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-10-10
iOS省市二級(jí)聯(lián)動(dòng)的數(shù)據(jù)組織PHP版
這篇文章主要為大家詳細(xì)介紹了iOS開發(fā)之"省市"二級(jí)聯(lián)動(dòng)的數(shù)據(jù)組織PHP版,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
ios NSNotificationCenter通知的簡單使用
這篇文章主要介紹了ios NSNotificationCenter通知的簡單使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-06-06
iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例
這篇文章主要介紹了iOS中獲取系統(tǒng)相冊(cè)中的圖片實(shí)例,具有一定的參考價(jià)值沒有需要的朋友可以了解一下。2016-11-11
iOS自定義View實(shí)現(xiàn)卡片滑動(dòng)
這篇文章主要為大家詳細(xì)介紹了ios自定義View實(shí)現(xiàn)卡片滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-02-02
iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸
這篇文章主要介紹了iOS應(yīng)用開發(fā)中使用Auto Layout來適配不同屏幕尺寸的方法,根據(jù)Xcode IDE下的實(shí)際調(diào)試步驟講解其用法,需要的朋友可以參考下2016-03-03

