Programmatically Select a TabBar Tab

Has anybody worked out how to programmatically select a TabBar tab? In my TabBar app, my first tab is a “Dashboard” with a summary of the key items requiring attention on the other tabs. If a user taps one of the Dashboard items, I want to switch tabs to one of the other tabs. I can see that Views have a ParentTabBar but this only has an AddTab method and a handle. (FYI I have an outstanding case for this <https://xojo.com/issue/36718> but I was hoping there might be a workaround for now).

I have this code at application level

Sub goTabPage(idx as integer,doReset as Boolean=False)
  if idx<0 then Return
  Declare Function NSClassFromString lib "Foundation"(cls as CFStringRef) as Ptr
  Declare function sharedApplication_ lib "UIKit" selector "sharedApplication"(cls_ptr as Ptr) as Ptr
  dim shAppPtr as Ptr=sharedApplication_(NSClassFromString("UIApplication"))
  
  Declare function keyWindow_ lib "UIkit" selector "keyWindow"(app_ptr as Ptr) as Ptr
  dim keyWinPtr as Ptr=keyWindow_(shAppPtr)
  
  Declare Function rootWiewController_ lib "UIKit" selector "rootViewController"(winPtr as Ptr) as Ptr
  dim rootWiewControllerPtr as Ptr=rootWiewController_(keyWinPtr)
  
  Declare Function isMemberOfClass_ lib "foundation" selector "isMemberOfClass:"(oPtr as Ptr,cPtr as Ptr) as Boolean
  dim a as ptr=NSClassFromString("UITabBarController")
  if isMemberOfClass_(rootWiewControllerPtr,NSClassFromString("UITabBarController")) then
    Declare sub setSelectedIndex lib "UIKIT" selector "setSelectedIndex:"(tbPtr as Ptr,page as UInteger)
    setSelectedIndex(rootWiewControllerPtr,idx)
    
    if doReset then
      Declare Function selectedViewController_ lib "UIKIT" selector "selectedViewController"(oPtr as ptr) as Ptr
      dim navPtr as Ptr=selectedViewController_(rootWiewControllerPtr)
      Declare Sub popToRoot lib "UIKIT" selector "popToRootViewControllerAnimated:"(oPtr as Ptr,animated as Boolean)
      popToRoot(navPtr,true)
    end if
  end if
End Sub

and it works :wink:

[quote=157150:@Antonio Rinaldi]I have this code at application level

Sub goTabPage(idx as integer,doReset as Boolean=False)
  if idx<0 then Return
  Declare Function NSClassFromString lib "Foundation"(cls as CFStringRef) as Ptr
  Declare function sharedApplication_ lib "UIKit" selector "sharedApplication"(cls_ptr as Ptr) as Ptr
  dim shAppPtr as Ptr=sharedApplication_(NSClassFromString("UIApplication"))
  
  Declare function keyWindow_ lib "UIkit" selector "keyWindow"(app_ptr as Ptr) as Ptr
  dim keyWinPtr as Ptr=keyWindow_(shAppPtr)
  
  Declare Function rootWiewController_ lib "UIKit" selector "rootViewController"(winPtr as Ptr) as Ptr
  dim rootWiewControllerPtr as Ptr=rootWiewController_(keyWinPtr)
  
  Declare Function isMemberOfClass_ lib "foundation" selector "isMemberOfClass:"(oPtr as Ptr,cPtr as Ptr) as Boolean
  dim a as ptr=NSClassFromString("UITabBarController")
  if isMemberOfClass_(rootWiewControllerPtr,NSClassFromString("UITabBarController")) then
    Declare sub setSelectedIndex lib "UIKIT" selector "setSelectedIndex:"(tbPtr as Ptr,page as UInteger)
    setSelectedIndex(rootWiewControllerPtr,idx)
    
    if doReset then
      Declare Function selectedViewController_ lib "UIKIT" selector "selectedViewController"(oPtr as ptr) as Ptr
      dim navPtr as Ptr=selectedViewController_(rootWiewControllerPtr)
      Declare Sub popToRoot lib "UIKIT" selector "popToRootViewControllerAnimated:"(oPtr as Ptr,animated as Boolean)
      popToRoot(navPtr,true)
    end if
  end if
End Sub

and it works ;)[/quote]

Antonio, you genius !

It works fine in a module as well, and will now save me some rather cumbersome pushto :slight_smile:

Thank you so much.

I have added a comment to <https://xojo.com/issue/36718> to refer to this post. I am going to add that to the wrapper, but I think it really ought to be part of Xojo. PushTo simply does not cut it when tabs are concerned.

Yes, thank you very much Antonio. :slight_smile:

That code works perfectly. I created my first iOS game app last night and had that exact problem, jumping to a different tab View after pressing a button on a different View — now solved! Thank you.