美文网首页
iOS开发初级(一、基础003)

iOS开发初级(一、基础003)

作者: feitry | 来源:发表于2018-07-23 00:35 被阅读0次

    输入框:UITextField和UITextView
    应用场景:登录或文本输入时使用
    代码:

        //UITextField
        UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(30.0, 80.0, 160.0, 30.0)];
        //提示文字
        textField.placeholder = @"请输入数字";
        //输入键盘类型
        textField.keyboardType = UIKeyboardTypeNumberPad;//数字
        //添加到父视图
        [self.view addSubview:textField];
    

    常用遵循协议<UITextFieldDelegate>:

    @protocol UITextFieldDelegate <NSObject>
    
    @optional
    
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;        // return NO to disallow editing.
    - (void)textFieldDidBeginEditing:(UITextField *)textField;           // became first responder
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField;          // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
    - (void)textFieldDidEndEditing:(UITextField *)textField;             // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
    - (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
    
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;   // return NO to not change text
    
    - (BOOL)textFieldShouldClear:(UITextField *)textField;               // called when clear button pressed. return NO to ignore (no notifications)
    - (BOOL)textFieldShouldReturn:(UITextField *)textField;              // called when 'return' key pressed. return NO to ignore.
    
    @end
    

    开发中最常用的:

    -(void)textFieldDidEndEditing:(UITextField *)textField{
        NSString *text = textField.text;
        NSLog(@"编辑框文本:%@",text);
    }
    

    UITextView和UITextField相似,但不同的是:
    UITextView是多行文本输入框,UITextField为单行文本输入框。

    相关文章

      网友评论

          本文标题:iOS开发初级(一、基础003)

          本文链接:https://www.haomeiwen.com/subject/bkwcmftx.html