国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

詳解iOS中Button按鈕的狀態和點擊事件

2020-07-26 03:12:30
字體:
來源:轉載
供稿:網友

一、按鈕的狀態

1.UIControlStateNormal

    1> 除開UIControlStateHighlightedUIControlStateDisabledUIControlStateSelected以外的其他情況,都是normal狀態

    2> 這種狀態下的按鈕【可以】接收點擊事件

2.UIControlStateHighlighted

    1> 【當按住按鈕不松開】或者【highlighted = YES】時就能達到這種狀態

    2> 這種狀態下的按鈕【可以】接收點擊事件

3.UIControlStateDisabled

    1> 【button.enabled = NO】時就能達到這種狀態

    2> 這種狀態下的按鈕【無法】接收點擊事件

4.UIControlStateSelected

    1> 【button.selected = YES】時就能達到這種狀態

    2> 這種狀態下的按鈕【可以】接收點擊事件

二、讓按鈕無法點擊的2種方法

     1> button.enabled = NO;

     *【會】進入UIControlStateDisabled狀態

     2> button.userInteractionEnabled = NO;

     *【不會】進入UIControlStateDisabled狀態,繼續保持當前狀態

三、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、運行結果

界面:

輸出:

總結

以上就是這篇文章的全部內容了,希望能對大家的學習或者工作帶來一定的幫助,如果有疑問大家可以留言交流。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 军事| 仁化县| 乌拉特后旗| 寻甸| 商水县| 化德县| 濉溪县| 集安市| 富川| 昭觉县| 增城市| 黄陵县| 行唐县| 盐津县| 开远市| 榕江县| 西藏| 边坝县| 桦南县| 牡丹江市| 泸溪县| 紫金县| 衡水市| 太和县| 大兴区| 宜良县| 车致| 广州市| 桃园县| 黄冈市| 临朐县| 名山县| 阳信县| 榆林市| 长白| 黄陵县| 宜州市| 濮阳县| 石林| 来凤县| 图木舒克市|