2016-05-19 32 views
0

我有一個UITextField,它在UISearchController中,當顯示視圖控制器時需要成爲FirstResponder。UISearchController中的UITextField BecomeFirstResponder

在ViewDidLoad上構建了UISearchController。

在ViewDidAppear我從UISearchController得到的UITextField,並告訴它BecomeFirstResponder

public override void ViewDidLoad(bool animated) 
    { 
     base.ViewDidLoad(); 
     BuildSearchController(); 

     SearchController.SearchBar.BecomeFirstResponder(); 
    } 

private void BuildSearchController() 
    { 
     UIFont MyFont = UIFont.FromName("Helvetica", 20f); 

     SearchController = new UISearchController(ResultsTableController) 
     { 
      WeakDelegate = this, 
      DimsBackgroundDuringPresentation = false, 
      WeakSearchResultsUpdater = this, 
     }; 

     SearchController.SearchBar.ScopeButtonTitles = new string[5] { "Account", "Name", "Service Address", "Phone", "SSN" }; 
     UITextField SearchField = (UITextField)SearchController.SearchBar.Subviews[0].Subviews[2]; 

     SearchField.Font = MyFont; 
     SearchController.SearchBar.ShowsCancelButton = false; 
     SearchController.SearchBar.SetScopeBarButtonTitle(new UITextAttributes() { Font = MyFont }, UIControlState.Normal); 
     SearchController.SearchBar.SizeToFit(); 
     SearchController.SearchBar.SelectedScopeButtonIndex = 1; 
     SearchController.SearchBar.SetShowsCancelButton(false, false); 

     UISegmentedControl TempSegControl = SearchController.SearchBar.Subviews[0].Subviews[0].Subviews[1] as UISegmentedControl; 
     TempSegControl.BackgroundColor = UIColor.FromRGBA(0, 122, 255, 255); 
     TempSegControl.TintColor = UIColor.White; 
     TempSegControl.Layer.BorderWidth = 1; 
     TempSegControl.Layer.CornerRadius = 6; 
     TempSegControl.Layer.BorderColor = UIColor.White.CGColor; 
     SearchController.SearchBar.BarTintColor = UIColor.FromRGBA(232, 232, 232, 255); 


     SearchController.HidesNavigationBarDuringPresentation = false; 

     SearchField.BecomeFirstResponder(); 
     TableView.TableHeaderView = SearchController.SearchBar; 

     SearchController.SearchBar.WeakDelegate = this; 
    } 

public override void ViewDidAppear(bool animated) 
    { 
     UITextField SearchBox = SearchController.SearchBar.Subviews[0].Subviews[2] as UITextField; 

     if (SearchBox.CanBecomeFirstResponder) 
     { 
      SearchBox.BecomeFirstResponder(); 
     } 
     base.ViewWillAppear(animated); 
    } 

的UITextField永遠不會成爲第一個響應者,因爲它是不能夠成爲第一個響應者。爲什麼UITextField不能成爲第一個轉型者?

回答

0

ViewDidLoad在視圖控制器已將其視圖層次結構加載到內存中但未呈現給用戶之後調用。 您無法在該階段與對象進行UI交互。

UISearchController有一個委託方法,當它自動出現時被調用。用它來調出鍵盤。

optional func didPresentSearchController(searchController: UISearchController){ 
     searchController.searchBar.becomeFirstResponder(); 
    } 
+0

我將該調用移至ViewDidAppear方法。我的UISearchController中的UITextField仍然沒有焦點。當顯示控制器時,我希望用戶能夠在無需觸摸UITextField內部的情況下開始輸入 –

+0

感謝您的工作。這是它的C#版本。 [Export(「didPresentSearchController:」)] public virtual void DidPresentSearchController(UISearchController searchController) { SearchController.SearchBar.Subviews [0] .Subviews [2] .BecomeFirstResponder(); } –

相關問題