2014-02-28 121 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

相關問題