最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解iOS中Button按鈕的狀態(tài)和點擊事件

 更新時間:2016年09月23日 10:58:31   投稿:daisy  
這篇文章先是給大家介紹iOS中Button按鈕的狀態(tài),而后又詳細(xì)介紹了iOS中按鈕點擊事件處理方式,本文介紹的很詳細(xì),有需要的朋友們可以參考借鑒,下面來一起看看吧。

一、按鈕的狀態(tài)

1.UIControlStateNormal

    1> 除開UIControlStateHighlighted、UIControlStateDisabled、UIControlStateSelected以外的其他情況,都是normal狀態(tài)

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

2.UIControlStateHighlighted

    1> 【當(dāng)按住按鈕不松開】或者【highlighted = YES】時就能達(dá)到這種狀態(tài)

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

3.UIControlStateDisabled

    1> 【button.enabled = NO】時就能達(dá)到這種狀態(tài)

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

4.UIControlStateSelected

    1> 【button.selected = YES】時就能達(dá)到這種狀態(tài)

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

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

     1> button.enabled = NO;

     *【會】進(jìn)入UIControlStateDisabled狀態(tài)

     2> button.userInteractionEnabled = NO;

     *【不會】進(jìn)入UIControlStateDisabled狀態(tài),繼續(xù)保持當(dāng)前狀態(tài)

三、iOS中按鈕點擊事件處理方式

在iOS開發(fā)中,時常會用到按鈕,通過按鈕的點擊來完成界面的跳轉(zhuǎn)等功能。按鈕事件的實現(xiàn)方式有多種,其中

較為常用的是目標(biāo)-動作對模式。但這種方式使得view與controller之間的耦合程度較高,不推薦使用;

另一種方式是代理方式,按鈕的事件在view中綁定,controller作為view的代理實現(xiàn)代理方法。

目標(biāo)-動作對實現(xiàn)方式

具體來說,假設(shè)我們有一個包含一個Button的veiw,view將Button放在頭文件中,以便外部訪問。然后controller將view作為自己的view,在viewcontroller中實現(xiàn)按鈕的點擊事件。

文字描述起來好像不夠直觀,直接上代碼

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中設(shè)置按鈕的目標(biāo)-動作,其中目標(biāo)是self,也就是控制器自身,動作是用目標(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、運行結(jié)果

界面:

輸出:

總結(jié)

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

相關(guān)文章

最新評論

临泉县| 湖南省| 南华县| 松阳县| 武安市| 郸城县| 龙里县| 峨山| 新余市| 正定县| 盘锦市| 东兰县| 田林县| 余庆县| 图木舒克市| 冷水江市| 武隆县| 离岛区| 天等县| 商丘市| 福清市| 凤台县| 沙湾县| 西昌市| 余姚市| 平山县| 沁阳市| 彭水| 筠连县| 安阳市| 乌鲁木齐县| 芮城县| 津南区| 丹棱县| 安平县| 汨罗市| 桃园县| 濮阳县| 鄂温| 深水埗区| 潮安县|