自訂視圖:視圖的封裝過程.

來源:互聯網
上載者:User

標籤:uitextfield   uilabel   uibutton   ui   delegate   

@interface LTView : UIView//1.是否採用安全模式- (void)setSecureEntry:(BOOL)secureEntry;//2.設定鍵盤的類型- (void)setKeyBoardType:(UIKeyboardType)keyBoardType;//3.設定textField代理- (void)setDelegate:(id<UITextFieldDelegate>)delegate;//4.擷取輸入框中輸入的文字- (NSString *)text;//自訂初始化方法,來處理LTView之間的差異性//1.LTView的frame, 2.UILabel上顯示的文字 . 3.UITextField佔位文字. 4.預設顯示的文字.- (id)initWithFrame:(CGRect)frame labelText:(NSString *)labelText placeholder:(NSString *)placeholder textFieldText:(NSString *)textFieldText;@end

LTView.h中的介面檔案.

@interface LTView (){    UILabel *_desLabel; //左邊的label    UITextField *_textField; //右邊的textField}@end@implementation LTView- (id)initWithFrame:(CGRect)frame labelText:(NSString *)labelText placeholder:(NSString *)placeholder textFieldText:(NSString *)textFieldText{    self = [self initWithFrame:frame];    if (self) {        //initialization code here..        _desLabel.text = labelText;        _textField.placeholder = placeholder;        _textField.text = textFieldText;    }    return self;}/** *  當系統提供的控制項滿足不了我們的需求時,我們就需要組合系統控制項,封裝成自己的控制項.    自訂視圖的步驟:    1.建立類 繼承自UIView.    2.封裝系統控制項.(內部完成群組控制項的建立過程)    3.使用自訂控制項建立對象.    封裝的過程:封裝內部實現,提供外界訪問的介面.先封裝公用部分,然後處理差異. * */- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        //LTView內部封裝了 左邊一個UILabel,右邊一個UITextField.        // Initialization code        //初始化時完成內部封裝控制項的建立以及添加操作.        CGFloat width = frame.size.width;        CGFloat height = frame.size.height;        //UILabel        _desLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0.3 * width, height)];        _desLabel.font = [UIFont systemFontOfSize:15];        _desLabel.textAlignment = NSTextAlignmentRight;        [self addSubview:_desLabel];        [_desLabel release];        //UITextField        _textField = [[UITextField alloc] initWithFrame:CGRectMake(0.4 * width, 0, 0.6 * width, height)];        _textField.borderStyle = UITextBorderStyleRoundedRect;        UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];        [btn setTitle:@"完成" forState:UIControlStateNormal];        [btn addTarget:self action:@selector(done:) forControlEvents:UIControlEventTouchUpInside];        btn.frame = CGRectMake(320 - 60, 0, 40, 40);        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 40)];        [view addSubview:btn];        view.backgroundColor = [UIColor whiteColor];        _textField.inputAccessoryView = view;        [view release];        _textField.autocorrectionType = UITextAutocorrectionTypeNo;        [self addSubview:_textField];        [_textField release];    }    return self;}- (void)done:(UIButton *)btn{    [_textField resignFirstResponder];}//1.是否採用安全模式- (void)setSecureEntry:(BOOL)secureEntry{    _textField.secureTextEntry = secureEntry;}//2.設定鍵盤的類型- (void)setKeyBoardType:(UIKeyboardType)keyBoardType{    _textField.keyboardType = keyBoardType;}//3.設定textField代理- (void)setDelegate:(id<UITextFieldDelegate>)delegate{    _textField.delegate = delegate;}//4.擷取輸入框中輸入的文字- (NSString *)text{    return _textField.text;}/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end

LTView.m中中的實現.

通過以上兩步,可以封裝左邊一個UILable,和右邊一個UITextField視圖.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.