UI 03 UIButton 和 UITextField

可以將UIButton 與 UITextField 結合起來使用, 產生如下圖的效果.
最開始運行時的狀態
點擊button後密碼顯示

// 新建一個Button
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(100, 300, 50, 50);
    button.backgroundColor = [UIColor cyanColor];
    [self.window addSubview:button];

    button.layer.borderWidth = 1;
    [button setTitle:@"顯示" forState:UIControlStateNormal ];
    [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    // 新建一個UITextField的屬性.
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(70, 100, 120, 50)];
    self.textField.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.textField];
    [self.textField release];

    self.textField.textColor = [UIColor cyanColor];
    self.textField.layer.borderWidth = 1;
    self.textField.layer.cornerRadius = 10;
    self.textField.clearButtonMode = UITextFieldViewModeAlways;
    self.textField.placeholder = @"請輸入密碼";
    self.textField.secureTextEntry = YES;
    self.textField.keyboardType = UIKeyboardTypeNumberPad;
    self.textField.tag = 1000;

    // 他是UIControl的子類,可以添加addTag 方法
    [self.textField addTarget:self action:@selector(chageValue:) forControlEvents:UIControlEventEditingChanged];

    //創建一個按鈕,用來切換狀態
    UIButton *newButton = [UIButton buttonWithType:UIButtonTypeCustom];
    newButton.frame = CGRectMake(60, 180, 40, 40);
    newButton.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:newButton];
    self.isSelect = NO;
    [newButton setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    [newButton addTarget:self action:@selector(changePic:) forControlEvents:UIControlEventTouchUpInside];

    // 創建一個label來顯示
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(110, 180, 150, 40)];
    label.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:label];
    [label release];

    label.text = @"是否顯示密碼";
    label.alpha = 0.5;

 //創建一個label
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(210, 110, 150, 30)];
    self.label.backgroundColor = [UIColor whiteColor];
    [self.window addSubview:self.label];
    [_label release];
    self.label.alpha = 0.5;
    self.label.text = @"passWord lenth";

對應的方法:

- (void)click:(UIButton *)button{
//    UITextField *t = (UITextField *)[self.window viewWithTag:1000];
    NSLog(@"%@",self.textField.text);

}

- (void)changePic:(UIButton *)button{
    if (self.isSelect) {
        [button setImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];
    }else{
         [button setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
    }
    self.isSelect = !self.isSelect;
    self.textField.secureTextEntry = !self.textField.secureTextEntry;
}


- (void)chageValue:(UITextField *)textField{
    NSLog(@"%@",textField.text);
    if (textField.text.length >5) {
        self.label.text = @"密碼長度適合";
    }else{
        self.label.text = @"密碼長度太短";
    }

}

從圖中還可以看到一個mybutton 字樣的button.
那個是新建的繼承於UIButton的MyButton 類.

  // MyButton類型的對象.
    MyButton *meButton = [MyButton buttonWithType:UIButtonTypeSystem];
    meButton.frame = CGRectMake(200, 250, 100, 100);
    meButton.buttonName = @"宋某人";
    [self.window addSubview:meButton];
    [meButton setTitle:@"myButton" forState:UIControlStateNormal];
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章