2014-02-28 48 views
0

我创建了一个UITextField编程,(我不使用故事板),并使用此代码添加它作为一个subviewViewController不能使编程方式创建的UITextField编辑(IOS)

@interface ViewController() 
@property (nonatomic, strong) UITextField *searchLocationBar; 
@end 

...

@synthesize searchLocationBar; 

...

self.searchLocationBar = [[UITextField alloc] init]; 
self.searchLocationBar.frame = CGRectMake(0.0f, 0.0f, 320.0f, 40.0f); 
self.searchLocationBar.delegate = self; 
self.searchLocationBar.borderStyle = UITextBorderStyleRoundedRect; 
self.searchLocationBar.placeholder = @"a temporary placeholder"; 
self.searchLocationBar.userInteractionEnabled = YES; 
self.searchLocationBar.clearButtonMode = UITextFieldViewModeAlways; 
[self.view addSubview:self.searchLocationBar]; 

但是,我不能恩特任何文字 - 没有任何反应,当我点击textfield。它不会与其他视图重叠。

我检查UITextfield not editable-iphone但没有效果

我是新手,完全确定,我只是错过了什么 - 请指点。 谢谢!

编辑: 一两件事:我有一个谷歌地图GMSMapView中分配给self.view为 self.view = mapView_;如Google API文档中所写。 经过一些测试后,我发现使用这个声明,所有的控件都可以很好地工作,但不是textfields。我不想将地图视图移动到任何子视图,因为我需要重写很多东西。

有人可以添加任何建议吗?

+0

self.searchLocationBar.font = [UIFont fontWithName:@ “黑体,粗体” 大小:25]。核实 ? – Rushabh

+1

你是否从'textFieldShouldBeginEditing:'textfield委托方法返回'NO'? – Amar

+0

评论此行self.searchLocationBar.delegate = self; 并检查它的工作是否 – Rushabh

回答

1

你忘了补充:

[self.view bringSubviewToFront:self.searchLocationBar]; 
1

在Xcode 5中,您的代码应该可以工作。您可以检查您的Xcode版本。可能是代码与Xcode版本有关的问题。您可以按照以下方式尝试。

UITextField *lastName = [[[UITextField alloc] initWithFrame:CGRectMake(10, 100, 300, 30)]; 
[self.view addSubview:lastName]; 

lastName.placeholder = @"Enter your last name here"; //for place holder 
lastName.textAlignment = UITextAlignmentLeft;   //for text Alignment 
lastName.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:14.0]; // text font 
lastName.adjustsFontSizeToFitWidth = YES;  //adjust the font size to fit width. 

lastName.textColor = [UIColor greenColor];    //text color 
lastName.keyboardType = UIKeyboardTypeAlphabet;  //keyboard type of ur choice 
lastName.returnKeyType = UIReturnKeyDone;    //returnKey type for keyboard 
lastName.clearButtonMode = UITextFieldViewModeWhileEditing;//for clear button on right side 

lastName.delegate = self;    //use this when ur using Delegate methods of UITextField 

还有很多其他属性可用,但这些都是少数,我们使用它frequently.ifü想知道更多的属性,以及如何使用它们请参考以下链接。 您也可以为UITextField制作属性。无论哪种方式都应该在Xcode中正常工作。

http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextField_Class/Reference/UITextField.html

相关问题