iOS 啓動基本流程及基礎控件

iOS 基本啓動流程

int main(int argc, char * argv[]) {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

在以上的 main 函數中將功能都跳轉到 UIApplicationMain 中, 而 UIApplicationMain 函數總共實現以下三步:

  1. 創建應用程序對象(UIApplication 對象)
  2. 創建應用程序代理對象, 指定應用程序代理, 應用程序代理用來檢測應用程序的執行狀態.
  3. 創建事件循環(runloop, 死循環), 監測用戶對屏幕的操作, 一旦發現用戶操作程序, 應用程序必須立即作出迴應. 由於用戶操作應用程序的時間未知, 所以需要一直進行監聽, 直到應用程序退出.(在 AppDelegate中進行監聽)
    AppDelegate中函數說明
  4. 在 AppDelegate 中定義視圖控制器 ViewController , 並設置爲根視圖控制器, 並在 ViewController 中對視圖進行佈局以及操作等.
    ViewController 的說明

基礎控件

UILabael

  1. UILabel 是UIView 的子類
  2. UILabel 的使用步驟:
//創建UILabel控件
UILabel *subLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
//配置屬性
subLabel.text = @"subLabel";//設置顯示的文字
//添加到父視圖
[container addSubview:subLabel];
//釋放
[subLabel release];

3 . UILabel 的屬性

  • tag – Label 的標籤, 不同的對象不能有相同的 tag 值, 用戶定義時使用100以上的值.
  • backgroundColor – Label 的背景顏色(UIColor)
  • text – Label 的文本內容(NSString)
  • textColor – Label 的文本顏色(UIColor)
  • textAlignment – Label 的文本對齊方式(NSTextAlignment)
typedef NS_ENUM(NSInteger, NSTextAlignment) {
    NSTextAlignmentLeft      = 0,    // Visually left aligned
#if TARGET_OS_IPHONE
    NSTextAlignmentCenter    = 1,    // Visually centered
    NSTextAlignmentRight     = 2,    // Visually right aligned
#else /* !TARGET_OS_IPHONE */
    NSTextAlignmentRight     = 1,    // Visually right aligned
    NSTextAlignmentCenter    = 2,    // Visually centered
#endif
    NSTextAlignmentJustified = 3,    // Fully-justified. The last line in a paragraph is natural-aligned.
    NSTextAlignmentNatural   = 4,    // Indicates the default alignment for script
} NS_ENUM_AVAILABLE_IOS(6_0);
  • font – Label 的字體類型及大小(UIFont)
  • numberOfLines – Label 顯示文本的行數(NSInteger)
  • lineBreakMode – Label 的行的截取方式(NSLineBreakMode)
  • shadowColor – 設置 Label 的文本陰影顏色(UIColor)
  • shadowOffset – 設置 Label 的文本陰影偏移量, 可以讓文字更有立體感(CGSize), 關於陰影問題可以參考CALayer中的 shadow

UITextField

  1. UITextField 是 UIControl 的子類(UIControl 是 UIView 的子類)
  2. UITextField 使用步驟:
//創建 UITextField 控件
UITextField *subTextField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 25)];
//配置屬性
subTextField.tag = 300;
//添加到父視圖
[containerView addSubview:subTextField];
//釋放
[subTextField release];

3.. UITextField 的常用屬性

  • backgroundColor
  • tag
  • text
  • textColor
  • textAlignment
  • font
  • clearOnBeginEditing – 當輸入框開始編輯時, 清除輸入框中的文字(BOOL)
  • keyboardType – 設置鍵盤類型(UIKeyboardType)
  • returnkeyType – 設置 return 按鈕的樣式(UIReturnKeyType)
  • secureTextEntry – 設置輸入框以密文形式顯示(BOOL), 默認爲 NO
  • borderStyle – 設置輸入框的樣式(UITextBorderStyle)
  • clearButtonMode – 設置輸入框中清除按鈕的模式(UITextFieldViewMode)

UIButton

  1. UIButton 是UIControl 的子類
  2. UIButton 使用步驟:
//創建UIButton 控件
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//配置屬性
button.tag = 200;
//添加到父視圖
[container addSubview:button];

3 . UIButton 的常用屬性

  • tag
  • backgroundColor
  • 設置 Button 上的文本
[button setTitle:@"確定" forState:UIControlStateNormal];//設置文本內容
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];//設置文本顏色
  • 設置圖片
[button setImage:[UIImage imageNamed:@"login_btn_press"] forState:UIControlStateNormal];
  • 添加 Button 響應事件
/**
 *  target : 指定事件的響應對象
 *  action : 指定響應對象要去調用的方法, 處理 Button 的點擊事件
 *  controlEvents : 事件的觸發時機
 *  注意 : handleButtonACtionL: 方法的參數可有可無, 如果有只能有一個, 因爲參數爲調用 addTarget:action:forControlEvents: 方法的對象
 */
[button addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章