如何在视图显示时自动显示键盘,而不在iOS,目标C中执行任何操作

时间:2016-06-14 12:30:20

标签: ios objective-c uisearchbar uikeyboard

在我的iOS应用程序中有两个视图。在第一个视图中有一个按钮。当用户按下按钮时,它会导航到带有搜索栏的第二个视图控制器。在该视图控制器中,用户必须点击搜索器才能获得键盘。我想要的是点击搜索器,当视图出现时自动弹出键盘对用户没有做任何事情。

我尝试了以下,

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self.fromportSearchcontroller setActive:YES];
    [self.fromportSearchcontroller.searchBar becomeFirstResponder];
}

但这没有用。希望你的帮助。

注意: 这就是我创建uisearchcontroller

的方法
- (void)settinguptheSearchBarforfromairports
{
    self.fromportSearchcontroller = [[UISearchController alloc] initWithSearchResultsController:nil];
    self.fromportSearchcontroller.searchResultsUpdater = self;
    self.fromportSearchcontroller.searchBar.delegate = self;
    self.definesPresentationContext = YES;
    self.fromaiportsTableView.tableHeaderView = self.fromportSearchcontroller.searchBar;
    self.fromportSearchcontroller.searchBar.barTintColor = [UIColor lightGrayColor];
    self.fromportSearchcontroller.dimsBackgroundDuringPresentation = NO;
    //self.fromportSearchcontroller.active = YES;
    //self.fromportSearchcontroller.searchBar.text = @"type city or airport";
    [self.fromportSearchcontroller.searchBar sizeToFit];


}

我在' ViewDidLoad'中调用了此方法。方法

  • 搜索栏已激活,但键盘不会自动显示(弹出)。

1 个答案:

答案 0 :(得分:0)

而不是UISearchController尝试UISearchBar。它对我来说很好。

-(void)viewDidAppear:(BOOL)animated
    {
       [self.searchbar becomeFirstResponder];
    }

确保在调用becomeFirstResponder之前已分配self.searchbar

- (void)settinguptheSearchBarforfromairports
{
        self.searchbar = [[UISearchBar alloc]init];
        self.searchbar.backgroundColor = [UIColor whiteColor];
        self.searchbar.tintColor = [UIColor whiteColor];
        self.searchbar.placeholder = @"Search";
        self.searchbar.delegate=self;
        self.searchbar.barTintColor = [UIColor whiteColor];
        self.searchbar.layer.borderWidth = 0;
        [self.searchbar setTranslucent:YES];
        [self.view addSubview:self.searchbar];
}

希望这有帮助。