UITextField无法以编程方式工作?

时间:2015-03-30 12:24:29

标签: ios objective-c iphone uitextfield

我以编程方式创建UITextField一切正常,但我的UITextField未编辑,即当我尝试点击UITextField时,我的示例代码无效,

CGRect frame = CGRectMake(16, 281, 288, 47);
cycle  = [[UITextField alloc] initWithFrame:frame];
cycle.placeholder = @"Cycle Number";
cycle.textColor = [UIColor blackColor];
cycle.font = [UIFont systemFontOfSize:17.0];
cycle.borderStyle = UITextBorderStyleLine;
cycle.autocorrectionType = UITextAutocorrectionTypeYes;
cycle.keyboardType = UIKeyboardTypeDefault;
cycle.clearButtonMode = UITextFieldViewModeWhileEditing;
cycle.delegate = self;
cycle.text = @"";
[self.view addSubview:cycle];

1 个答案:

答案 0 :(得分:0)

'UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 300, 40)];   textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
textField.delegate = self;
[self.view addSubview:textField];'

Below delegate methods must use

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
   [self endEditing];
   [textField resignFirstResponder];
   return YES;

}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
        [self beginEditing:textField];
}

 - (void)textFieldDidEndEditing:(UITextField *)textField {


     // [self endEditing];
}



 I hope you it is useful for u