我有一个ViewController所有它的布局是以编程方式完成的,因为它用于谷歌地图。
我将按钮,图层和其他视图放在地图上作为子视图,一切都很棒。
目标是单击一个按钮,然后将UITextField添加为子视图,仅当用户单击其中时,将显示滑架以及键盘,将其设置为firstFirstResponder
当前状态UITextField出现但它没有进入编辑模式,我尝试添加tapGesture并且它也不起作用
这是添加UITextField
的代码- (IBAction) searchBtn_Click:(id)sender
{
[self.view addSubview:self.searchTextField];
}
-(UITextField*) searchTextField
{
if(_searchTextField == nil)
{
//set the location where to place the textField as well as its size.
CGFloat yAxis = self.yAxis - TEXT_FIELD_HEIGHT - TEXT_FIELD_PADDING_Y;
CGFloat textFieldWidth = (ROUND_BUTTON_SIZE + ROUND_BUTTON_PADDING_X)*(NUMBER_OF_MENU_BTN-2) +ROUND_BUTTON_SIZE;
//set all parameters to give it a interface builder look & feel
_searchTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.screenMarginXAxis+5, yAxis, textFieldWidth, TEXT_FIELD_HEIGHT)];
_searchTextField.borderStyle = UITextBorderStyleRoundedRect;
_searchTextField.font = [UIFont systemFontOfSize:15];
_searchTextField.placeholder = @"enter address or location";
_searchTextField.autocorrectionType = UITextAutocorrectionTypeNo;
_searchTextField.keyboardType = UIKeyboardTypeDefault;
_searchTextField.returnKeyType = UIReturnKeyDone;
_searchTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
_searchTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_searchTextField.delegate = self;
}
return _searchTextField;
}
这是我为tapGesture尝试的代码,它是在searchBtn_Click:
方法中添加子视图后添加的
//set the tap gesture to identify when the user enters edit mode
tapSearchTextField = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(enterSearchEditMode)];
[tapSearchTextField setNumberOfTouchesRequired:1];
[tapSearchTextField setNumberOfTapsRequired:1];
[tapSearchTextField setDelegate:self];
[self.searchTextField addGestureRecognizer:tapSearchTextField];
-(void) enterSearchEditMode
{
[self.searchTextField becomeFirstResponder];
}
答案 0 :(得分:0)
我觉得“[self.searchTextField]
”在您尝试向其添加手势识别器时为NULL。在那里设置断点和/或NSLog'输出将证明这一点。
您是指在视图控制器之外的任何位置使用“searchTextField
”属性吗?
如果没有,请删除“@property
”位并坚持使用ivar“_searchTextField
”(例如将“[self.searchTextField addGestureRecognizer:tapSearchTextField];
”更改为“[_searchTextField addGestureRecognizer: tapeSearchTextField];
” ”