controller.dismiss(animated: false, completion: nil)
但是,正如您从下面的图像中看到的,我的主选项卡栏项没有突出显示,我创建了一个方法,试图突出显示我的主选项卡,但我得到了一个nil异常。首先让我向你展示我的自定义功能所有的标签栏项目点击这里
class HomeProfile: NSObject {
var mycontroller = ControllerEnum()
func TabBarLogic(_ tabBar: UITabBar, didSelect item: UITabBarItem, streamsModel: Int, controller: UIViewController) {
if tabBar.items?.index(of: item) == 0 {
tabBar.tintColor = UIColor(hexString: "#004d99")
if TabBarCounter == 0 {
// To Refresh
let nextViewController = controller.storyboard?.instantiateViewController(withIdentifier: "HomeC") as! HomeC
controller.present(nextViewController, animated:false, completion: nil)
} else {
// keeps TableView position but does not highlight Tab
controller.dismiss(animated: false, completion: nil)
let Home = HomeC()
Home.HighlightTabBar() // Nil exception here
TabBarCounter = 0
}
}
if tabBar.items?.index(of: item)! == 1 {
// Search Tab Item
tabBar.tintColor = UIColor(hexString: "#004d99")
let nextViewController = controller.storyboard?.instantiateViewController(withIdentifier: "LocalSearchC") as! LocalSearchC
controller.present(nextViewController, animated:false, completion:nil)
TabBarCounter = 1
}
if tabBar.items?.index(of: item)! == 2 {
// Post
}
if tabBar.items?.index(of: item)! == 3 {
// Notification
}
if tabBar.items?.index(of: item)! == 4 {
// Menu
}
}
}
此功能与facebook类似,如果您在主页上再次单击“主页”选项卡,则它将刷新“表格”视图;但是,如果您在另一个选项卡上并返回“主页”选项卡,它将保持您的位置;在
标签计数器
检查一下。
class HomeC: UIViewController,UITableViewDataSource,UITableViewDelegate, UITabBarDelegate{
@IBOutlet weak var TabBar: UITabBar!
override func viewDidLoad() {
super.viewDidLoad()
TabBar.delegate = self
TabBar.selectedItem = TabBar.items![0]
TabBar.tintColor = UIColor(hexString: "#004d99")
}
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
homeProfile.TabBarLogic(tabBar, didSelect: item, streamsModel: TabCounter,controller: self)
}
func HighlightTabBar() {
TabBar.delegate = self
TabBar.selectedItem = TabBar.items![0] // this fails
TabBar.tintColor = UIColor(hexString: "#004d99")
}
}
我知道它失败的原因是HighlightTabBar()中的TabBar在调用它时没有对TabBar的引用。我怎样才能使它起作用呢。我一直在看这个
How to programmatically change UITabBar selected index after Dismiss?