SearchField for iOS: how to make firstresponder (setfocus)?

Following up a recent discussion, now I would like to find out how to setFocus on the searchfield, which has been created with the help of iOSKit sample code:

Dim sc As new UISearchController(nil)

//A few options are available here
sc.dimsBackgroundDuringPresentation = False
sc.hidesNavigationBarDuringPresentation = False


Dim scb As UIKit.UISearchBar = sc.searchBar

//A few options are available here
scb.placeholder = "Type something here..."
'scb.showsCancelButton = True

'scb.tintColor = UIColor.White
'scb.barTintColor = UIColor.Red

//Keep a reference to the searchbar
self.searchBar = scb

//Setup the searchfield delegate
scb.SetupDelegate

//Handler for search text changed
AddHandler scb.TextChanged, AddressOf handle_SearchTextChanged
AddHandler scb.CancelButtonPressed, AddressOf handle_SearchCancelled
AddHandler scb.SearchButtonPressed, AddressOf handle_SearchButtonPressed

Declare Function valueForKey_ Lib FoundationLib Selector "valueForKey:" (obj_id As ptr, key As CFStringRef) As ptr

Dim textfield As Ptr = valueForKey_(scb.handle, "searchField")
if textfield <> nil then
  
  Declare Sub setTextColor Lib "UIKit.framework" Selector "setTextColor:" (obj_id As ptr, col As ptr)
  setTextColor(textfield,  UIColor.White)
  
End If



//Now attaching the searchbar to the navigation bar
Declare Function navigationItem Lib "UIKit.framework" Selector "navigationItem" (obj_ref As ptr) As ptr
Dim navItem As ptr = navigationItem(Self.ViewControllerHandle)
Declare Sub setSearchController Lib UIKitLib Selector "setSearchController:" (obj As ptr, ctrl As ptr)
setSearchController(navItem, sc)

//If necessary, set this
Declare Sub hidesSearchBarWhenScrolling Lib "UIKit" Selector "hidesSearchBarWhenScrolling" (obj As ptr, value As Boolean)
hidesSearchBarWhenScrolling(navItem, False)

I tried to add this shot in the dark, but as it is not working as expected I need some help now.

Dim vGotFocus As Boolean
Declare Function becomeFirstResponder Lib "UIKit" Selector "becomeFirstResponder" (obj_id As UInt64) As Boolean
vGotFocus=becomeFirstResponder(UInt64(scb.Handle))

I would like to set focus on the search field at startup of the app. How?

Untested:

Dim vGotFocus As Boolean
Declare Function becomeFirstResponder Lib "UIKit" Selector "becomeFirstResponder" (obj_id As Ptr) As Boolean

Dim textfieldHandle As Ptr = valueForKey_(scb.handle, "searchField")
if textfieldHandle <> nil then
  vGotFocus=becomeFirstResponder(textfieldHandle)
End if

Thanks Jeremie! Your code is not throwing any error but the searchfield isn’t getting the focus either.

Maybe a timing issue? I found this hint to AfterDelay on StackOverflow:

[txtAddNew performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];

Yes it may be a timing issue.
You could try executing the code after 100 milliseconds using Timer.CallLater

I’m not getting this to work, but I think there must be a standard way to set focus on the searchfiled …

You will need to keep a reference to the UISearchController (sc in your original code)

Declare sub setActive lib "UIKit" selector "setActive:" (obj as ptr, value as Boolean)
setActive(searchControllerPtr, True)

Thanks again! I will try this as well!

In the meantime, I got the workaround with the timer functioning:

  1. I store the handle to the searchfield in a property of type Ptr:
Dim textfieldHandle As Ptr = valueForKey_(scb.handle, "searchField")
If textfieldHandle <> Nil Then handleSearchField = textfieldHandle
  1. In the opening event handler I use Timer.CallLater:
Timer.CallLater(500, AddressOf setFocusToSearch)
  1. The code in setFocusToSearch method:
Dim vGotFocus As Boolean
Declare Function becomeFirstResponder Lib "UIKit" Selector "becomeFirstResponder" (obj_id As Ptr) As Boolean

If Self.handleSearchField <> Nil Then
  vGotFocus=becomeFirstResponder(Self.handleSearchField)
End If