標籤: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