0

我有一个 ViewController,它的所有布局都是以编程方式完成的,因为它用于谷歌地图。

我在地图上放置按钮、图层和其他视图作为子视图,一切都很好。

目标是单击一个按钮,然后将 UITextField 添加为子视图,只有当用户单击它时,通过将其设置为 becomeFirstResponder 才会出现马车和键盘

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];
}
4

1 回答 1

0

我有一种感觉,[self.searchTextField]当您尝试向其添加手势识别器时,“”为 NULL。在那里设置断点和/或 NSLog'ing 输出将证明这一点。

您是指searchTextField视图控制器之外的任何地方的“”属性吗?

如果不是,请去掉“ @property”位并坚持使用 ivar“ _searchTextField”(例如将“”之类的行更改[self.searchTextField addGestureRecognizer:tapSearchTextField];为“ [_searchTextField addGestureRecognizer: tapeSearchTextField];”。

于 2013-07-26T04:42:18.223 回答