iOS編程-------UITextField UIButton UITextFieldDelegate

//
//  AppDelegate.h
//  UI02_UITextField\UIButton\UITextFieldDelegate
//
//  Created by t on 15/9/1.
//  Copyright (c) 2015年 . All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITextFieldDelegate>

@property (strong, nonatomic) UIWindow *window;

@end


//
//  AppDelegate.m
//  UI02_UITextField\UIButton\UITextFieldDelegate
//
//  Created by t on 15/9/1.
//  Copyright (c) 2015年 . All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()
#pragma 把我們要使用的控件定義爲實例變量.
@property (nonatomic, retain)UITextField *userNameTF;
@property (nonatomic, retain)UITextField *keyTF;
@end

@implementation AppDelegate
#pragma 重寫dealloc方法
- (void)dealloc{
    [_keyTF release];
    [_userNameTF release];
    [_window release];
    [super dealloc];
}

#pragma 實現UITextFieldDelegate裏面的鍵盤迴收方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    [textField resignFirstResponder];
    //取消第一響應者
    //正在和用戶進行交互的稱爲響應者,同一時間內只能有一個響應者.

    //點擊return按鈕 讓當前輸入框取消第一響應者,讓對應的另外一個輸入框成爲第一響應者.
//    if (textField.tag == 101) {
//        [textField resignFirstResponder];
//        //取出來 密碼輸入框
//        UITextField *pwdTextField = (UITextField *)[self.window viewWithTag:102];  //注意返回值類型
//        //讓密碼輸入框 成爲2第一個響應者
//        [pwdTextField becomeFirstResponder];
//}

//        if ([textField isEqual:_userNameTF]) {
//            [textField resignFirstResponder];
//            UITextField *nextTextField = [self.window ];
// }

    return YES;
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

#pragma UI02 主講內容:UITextField,UIButton,UITextFieldDelegate
#pragma 一.UITextField
// 創建文本框
//    1.開闢空間,並且初始化
//    UITextField *userNameTF = [[UITextField alloc] initWithFrame:(CGRectMake(100, 50, 200, 30))];

//    2.設置相關屬性,外觀屬性,輸入屬性,顯示屬性
//    (1)顯示屬性
//    userNameTF.text = @"用戶名";
//    userNameTF.textColor = [UIColor blueColor];
//    userNameTF.font = [UIFont systemFontOfSize:15];

//    userNameTF.placeholder = @"請輸入用戶名";//設置佔位符
//    userNameTF.textColor = [UIColor redColor];
////    (2)輸入屬性
//    userNameTF.secureTextEntry = YES;//設置爲密文輸入,默認爲NO
//    userNameTF.keyboardType = UIKeyboardTypeNumberPad;//設置鍵盤樣式:數字鍵盤
////    (3)外觀屬性
//    userNameTF.borderStyle = UITextBorderStyleRoundedRect;//設置邊框樣式,圓角樣式
//    userNameTF.clearButtonMode = UITextFieldViewModeAlways;//清除樣式,永久顯示
////
////    3.添加到父視圖上
//    [self.window addSubview:userNameTF];
//
//    4.釋放
//    [userNameTF release];

#pragma 練習 (1).輸入用戶名
    UITextField *userNameTF = [[UITextField alloc] initWithFrame:(CGRectMake(50, 50, 200, 30))];
    userNameTF.placeholder = @"請輸入用戶名";
    userNameTF.textColor = [UIColor blueColor];
    userNameTF.borderStyle = UITextBorderStyleRoundedRect;
    userNameTF.clearButtonMode = UITextFieldViewModeAlways;
//    [self.window addSubview:userNameTF];
//    [userNameTF release];

    userNameTF.delegate = self;
    userNameTF.tag = 101;
    [self.window addSubview:userNameTF];
    [userNameTF release];

#pragma (2).輸入密碼
    UITextField *keyTF = [[UITextField alloc] initWithFrame:(CGRectMake(50, 90, 200, 30))];
    keyTF.placeholder = @"請輸入密碼";
    keyTF.textColor = [UIColor blackColor];
    keyTF.secureTextEntry = YES;
    keyTF.keyboardType = UIKeyboardTypeNumberPad;
    keyTF.borderStyle = UITextBorderStyleRoundedRect;
    keyTF.clearButtonMode = UITextFieldViewModeAlways;
//    [self.window addSubview:keyTF];
//    [keyTF release];

    keyTF.delegate = self;
    keyTF.tag = 102;
    [self.window addSubview:keyTF];
    [keyTF release];

#pragma 二. UIButton 按鈕
    /*UIButton 是UIControl 的子類, 主要功能是和用戶進行交互,響應用戶的點擊事件.
    UIButton 一般使用類方法創建,使用系統提供的按鈕樣式,因此不用release */
    //登陸
    //1.創建Button
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //設置Button系統樣式

    //2.設置Button屬性

    loginButton.frame = CGRectMake(40, 210, 30, 20);
//    [loginButton setTitle:@"登陸" forState:(UIControlStateNormal)];//設置Button的標題
    [loginButton setTitle:@"xxx" forState:UIControlStateHighlighted];


    //3.添加點擊事件
    //addTarget : 執行selector 方法的對象
    //self 在哪個類的內部,self就是那個類的一個實例化對象
    //action: 點擊事件觸發的方法
    //forControlEvents: 觸發事件
    [loginButton addTarget:self action:@selector(loginAction:)  forControlEvents:(UIControlEventTouchUpInside)];

//  添加背景圖片  通過UIImage 類去創建一個圖片對象
//    如果圖片爲png格式,不用加後綴,否則需要加後綴
    UIImage *image = [UIImage imageNamed:@"EG$Z4XLKDA01CBH5R1`@]}4"];
//    設置normal狀態下的背景圖
    [loginButton setBackgroundImage:image forState:UIControlStateNormal];

    //4.添加button 到父視圖上
    [self.window addSubview:loginButton];

#pragma 找回密碼
    UIButton *getKeyButton = [UIButton buttonWithType:UIButtonTypeSystem];
    getKeyButton.frame = CGRectMake(100, 200, 80, 44);
    [getKeyButton setTitle:@"找回密碼" forState:UIControlStateNormal];
    [getKeyButton addTarget:self action:@selector(getKeyAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:getKeyButton];

#pragma 註冊
    UIButton *registrationButton = [UIButton buttonWithType:(UIButtonTypeSystem)];
    registrationButton.frame = CGRectMake(180, 210, 30, 20);
    [registrationButton setTitle:@"註冊" forState:UIControlStateNormal];
    [registrationButton addTarget:self action:@selector(registrationAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:registrationButton];
    //添加背景圖片
    UIImage *image2 = [UIImage imageNamed:@"/Users/lanou/Desktop/9ZDCZFDD5H3%5NX36(`][EU.jpg"];
    [registrationButton setBackgroundImage:image2 forState:UIControlStateNormal];

#pragma 三. UITextFieldDelegate協議
    //鍵盤迴收
    //鍵盤迴收 ,主要通過textFieldDelegate 協議裏面的方法textFieldShouldReturn: 來實現
    //當我們點擊虛擬鍵盤return按鈕的時候,textField的代理區執行textFieldShouldReturn:方法實現鍵盤迴收,或者其他操作
    //第一步:delegate協議  //UItextFieldDelegate
    //第二步:delegate 實現協議裏面的方法  - (BOOL)textFieldShouldReturn:(UITextField *)textField;

    UITextField *textField2 = [[UITextField alloc] initWithFrame:(CGRectMake(100, 300, 200, 30))];
    textField2.placeholder = @"回收鍵盤";
    textField2.borderStyle = UITextBorderStyleRoundedRect;
    //第三步 給textField 設置代理
    textField2.delegate = self;
    [self.window addSubview:textField2];
    [textField2 release];


    return YES;

}

//點擊事件觸發的方法,登陸事件
- (void)loginAction:(UIButton *)button{
//    button.hidden = YES;  //隱藏
    [button removeTarget:self action:@selector(loginAction:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"進行安全判定, 然後進入登陸頁面");
}

- (void)getKeyAction:(UIButton *)button{
    [button removeTarget:self action:@selector(getKeyAction:) forControlEvents:UIControlEventTouchUpInside];
    NSLog(@"找回密碼");
}

- (void)registrationAction:(UIButton *)button{
    [button removeTarget:self action:@selector(registrationAction:) forControlEvents:UIControlEventTouchUpOutside];
    NSLog(@"註冊");
}

@end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章