iOS開發(fā)知識(shí)點(diǎn)2——登陸界面

按鈕的實(shí)時(shí)響應(yīng):登陸的時(shí)候,需要當(dāng)用戶有輸入時(shí)按鈕才能響應(yīng)(可點(diǎn)擊),如果有一個(gè)輸入框?yàn)榭?,則按鈕不可響應(yīng)。以前我以為用的是textfield的代理方法直接判斷,后來發(fā)現(xiàn)那樣不好。有另一種方法

// 首先給textfield添加addTarget方法,注意最后的controlEvents是EditingChanged
[self.accountTextField addTarget:self action:@selector(handleTextDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.passwordTextField addTarget:self action:@selector(handleTextDidChange:) forControlEvents:UIControlEventEditingChanged];

// 然后實(shí)現(xiàn)這個(gè)方法
- (void)handleTextDidChange:(id)sender
{
    NSString * accountStr = self.accountTextField.text;
    NSString * passwordStr = self.passwordTextField.text;
    if (accountStr.length > 0 && passwordStr.length > 0) {
        // 登錄可響應(yīng)
        self.loginButton.userInteractionEnabled = YES;
        [self.loginButton setTitleColor:btnEnabledColor forState:UIControlStateNormal];
    } else {
        // 登錄不可響應(yīng)
        self.loginButton.userInteractionEnabled = NO;
        [self.loginButton setTitleColor:btnUnabledColor forState:UIControlStateNormal];
    }
}

輸入框長(zhǎng)度限制:當(dāng)輸入手機(jī)號(hào)或者驗(yàn)證碼時(shí),通常會(huì)有長(zhǎng)度限制,我們想要的是,11位的手機(jī)號(hào)或者6位的驗(yàn)證碼,所以最多只能輸入這么多,當(dāng)超出時(shí),不能再顯示,便于用戶理解。

// 這是通過textField的代理方法實(shí)現(xiàn)的
_smsPhoneTextField.delegate = self;
_smsCirtifyTextField.delegate = self;

#pragma mark - 輸入長(zhǎng)度限制
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if ([string isEqual:@"\n"]) {
        return YES;
    }
    NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField == _smsCirtifyTextField) {
        if (aString.length > 6) {
            aString = [aString substringToIndex:6];
            return NO;
        }
    }
    if (textField == _smsPhoneTextField) {
        if (aString.length > 11) {
            aString = [aString substringToIndex:11];
            return NO;
        }
    }
    return YES;
}

手機(jī)號(hào)的正則表達(dá)式:我收藏了一個(gè)常用的正則表達(dá)式的博客,很不錯(cuò),只需要針對(duì)自己的需求在他的基礎(chǔ)上改一下,就可以使用
常用的正則表達(dá)式

#pragma mark - 手機(jī)號(hào)驗(yàn)證
- (BOOL)validateMobile:(NSString *)mobile
{
    // 手機(jī)號(hào)以13、15、18、17開頭,八個(gè) \d 數(shù)字字符
    NSString * phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$";
    NSPredicate * phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [phoneTest evaluateWithObject:mobile];
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容