效果圖:
代碼:
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //TextField UITextField *textField=[[UITextField alloc]initWithFrame:CGRectMake(20, 100, 100, 200)]; textField.backgroundColor=[UIColor redColor]; textField.text=@"123"; [self.view addSubview:textField]; NSLog(@"判斷textField裡面是否為空%i",[self textFieldIsNOTEmpt:textField]); } //判斷textField裡面是否有空 -(BOOL)textFieldIsNOTEmpt:(UITextField *)textField{ if ([textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]].length!=0){ return YES; }else { return NO; } }