【问题标题】:UITextField is not working programmatically?UITextField 不能以编程方式工作?
【发布时间】:2015-03-30 12:24:28
【问题描述】:

我以编程方式创建了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];

【问题讨论】:

  • 它没有工作..这是什么问题?是您在尝试编辑时看不到文本字段吗?
  • 它工作的兄弟,但是当我尝试点击我的文本字段时,它不起作用我的键盘没有加载我想我留下了一些东西,我的文本字段是不可编辑的
  • 从模拟器中删除应用程序并清理构建然后再次执行如果问题仍然存在检查您的硬件键盘
  • 我试过了,还是不行
  • 尝试将子视图放在前面,它可能在某个视图下 [self.view bringSubviewToFront:cycle];

标签: ios objective-c iphone uitextfield


【解决方案1】:
'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 

【讨论】:

  • 这与 OP 的代码有何不同,为什么您认为这更好?很好的答案伴随着代码示例,并为未来的读者提供了解释。
  • #Stonz2 好问题,:-) 哦,对不起,我没有检查上面的答案,两者都是一样的。这是我的错误。但我解释说我们正在使用委托选项,所以必须添加文本字段委托我们的视图控制器,使用委托是在 xcode 中声明目标的最佳方式之一。所以我认为这是您的问题的答案之一。我希望您能理解。我们有很多方法可以在 xcode 中创建文本字段。无需声明委托我们还创建文本字段。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-23
  • 2011-10-22
  • 2015-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多