3. iOS 編程 之 UIButton

來源:互聯網
上載者:User

標籤:ios   uibutton   ios開發   ios教程   ui   

前面的配置&建立都搞好了的話,那麼就開始應用開發的第一步:瞭解應用組件。比如說,按鈕,文字框等等……這篇先介紹按鈕。

1、建立按鈕

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    UIViewController *viewController = [UIViewController alloc];    self.window.rootViewController = viewController;        UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    viewController.view = rootView;        /**     *  建立 UIButton     */    //建立按鈕的時候,必須用 buttonWithType: 來建立,想上面的 UIView 或其他的可以用 alloc    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    //設定按鈕的位置、寬高。參數:X軸,Y軸,寬度,高度    button.frame = CGRectMake(100, 100, 100, 30);    //設定按鈕的背景顏色    button.backgroundColor = [UIColor yellowColor];    //必須用這種方式來設定按鈕的文字,同時設定它的狀態。不能用button.titleLabel來設定文字,不顯示    [button setTitle:@"我是按鈕" forState:UIControlStateNormal];    //添加按鈕到 view 控制項顯示    [rootView addSubview:button];             [self.window makeKeyAndVisible];    return YES;}

1)UIButtonType 有如下幾個枚舉值:
UIButtonTypeCustom-----自訂
UIButtonTypeSystem-----標準系統預設
UIButtonTypeDetailDisclosure-----顯示當前列表詳情,顯示 i
UIButtonTypeInfoLight-----顯示簡簡訊息,顯示 i
UIButtonTypeInfoDark-----顯示簡簡訊息,顯示 i
UIButtonTypeContactAdd-----添加資訊,顯示 +

2)UIControlState 有如下幾個枚舉值:
UIControlStateNormal-----預設
UIControlStateHighlighted-----高亮
UIControlStateDisabled-----禁用
UIControlStateSelected-----選中

按鈕的其他屬性,只能是靠去查看文檔瞭解,一些零零碎碎的我就不寫出來了。

運行結果:


2、綁定一個事件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {...    //使用 addTarget:action:forControlEvents: 方法就可以綁定一個事件     [button addTarget:self action:@selector(Click:) forControlEvents:UIControlEventTouchUpInside];...}//綁定要實現的方法 Click:- (void) Click:(id) sender{    NSLog(@"我是一個按鈕");}
運行後點擊按鈕的效果:

1)UIControlEvent 有如下枚舉值:

UIControlEventTouchDown
UIControlEventTouchDownRepeat
UIControlEventTouchDragInside
UIControlEventTouchDragOutside
UIControlEventTouchDragEnter
UIControlEventTouchDragExit 
UIControlEventTouchUpInside
UIControlEventTouchUpOutside
UIControlEventTouchCancel
UIControlEventValueChanged
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd 
UIControlEventEditingDidEndOnExit 
UIControlEventAllTouchEvents 
UIControlEventAllEditingEvents 
UIControlEventApplicationReserved
UIControlEventSystemReserved (application use)
UIControlEventAllEvents   (framework use)

基礎部分,簡單介紹一下就行,以後碰到相關的再接著說明其他的使用方法。

第三章,結束!

本篇內容屬原創,轉載請註明出處,禁止用於商業用途。謝謝!


3. iOS 編程 之 UIButton

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.