2017-01-03 62 views

回答

0

在項目中嘗試這個代碼

self.stremail=[self.txtemail.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
self.strpass=[self.txtpass.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 


if ((self.stremail.length==0) || (self.strpass.length == 0)) 
{ 
    if(self.stremail.length==0) 
    { 
    UIColor *color = [UIColor redColor]; 
    _txtemail.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_txtemail.placeholder attributes:@{NSForegroundColorAttributeName: color}]; 
    }else 
    { 
     UIColor *color = [UIColor redColor]; 
     _txtpass.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_txtpass.placeholder attributes:@{NSForegroundColorAttributeName: color}]; 

    } 

} 
1

首次進口<QuartzCore/QuartzCore.h>

-(void)checkTextFieldIsEmpty{ 

    NSArray *myarr = [self.view subviews]; 
    NSString *emptyTextFieldName = [[NSString alloc]init]; 

    for (int i = 0; i < myarr.count; i++) { 
     if ([[myarr objectAtIndex:i] isKindOfClass:[UITextField class]]) { 
     UITextField *tempTextField = (UITextField*)[myarr objectAtIndex:i]; 
      if (tempTextField.text.length >0) { 
       NSLog(@"textfield have some text"); 
      } 
      else{ 
       NSLog(@"textfield is empty"); 
        //Change color here like following way 
        //tempTextField.layer.borderColor = [[UIColor redColor]CGColor]; 
       } 
      }  
     } 
    } 
+1

能告訴我這裏爲什麼你添加< QuartzCore/QuartzCore.h>框架。 –

+0

@HimanshuPatel單擊您的項目名稱 - >構建階段 - >轉到與庫鏈接二進制 - >單擊+ - >添加QuartzCore.framework。然後在你的文件中導入。 –

+0

爲什麼要使用框架? –

0

,最好的辦法是添加在文本字段中的右視圖篩選

-(IBAction)yourButtonAction:(id)sender{ 

    if(yourTextField.text.lenght == 0){ 
      UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)]; 
      imageView.image = [UIImage imageNamed:@"yourImageName.png/jpg"]; 

      self.yourTextField.rightViewMode = UITextFieldViewModeAlways; 
      self.yourTextField.rightView = imageView; 
     } 

    } 

//在:

然後你就可以通過調用子視圖的方法獲得陣列中的所有子視圖TextField的代理方法

- (void)textFieldDidBeginEditing:(UITextField *)textField; 


    if(yourTextField.isEditing == YES){ 
      self.yourTextField.rightViewMode = UITextFieldViewModeNever; 
      } 
    } 
0

您可以使用US2FormValidator庫。只需要將其添加到您的項目中,並將US2ValidatorTextField作爲具有驗證消息和一些屬性的類名稱。

0

,我認爲你應該得到的觀點,即包含textFields 檢查,如果subview是一種UItextField類的所有子視圖,如果它是空的,則改變顏色:

for loop in self.view.subviews 
{ 
    if([loop isKindOf:[UItextField class]] && loop.characters.count==0) { 
     //change the Color of textField 
    } 
} 
相關問題