【发布时间】:2019-09-14 07:20:23
【问题描述】:
我有一个 TabViewController 有三个选项卡,这导致三个 ViewControllers。
我可以很好地使用 TVC 的实际标签按钮。但是,我还想使用 UIButton 转到其中一个选项卡。我在一个 ViewController 中有一个按钮,我想转到另一个,但不使用 Tab 按钮。
我已经取得了一些部分成功。部分我的意思是 VC 出现但 TabBar 不存在!
UIButton所在视图控制器的代码如下:
import UIKit
class HomeView: UIViewController {
@IBOutlet var startBtn: UIButton! //Define the start button as a variable
@IBOutlet var homeText: UILabel! //Define the text on the home screen
override func viewDidLoad() {
super.viewDidLoad()
setBackground(); //Set background image
setStartBtn();
setHomeText();
}
func setBackground() {
// Apply the insets…
let backgroundImage = UIImageView(frame: UIScreen.main.bounds)
backgroundImage.image = UIImage(named: "Background.png")
backgroundImage.contentMode = UIView.ContentMode.scaleAspectFill
self.view.insertSubview(backgroundImage, at: 0)
}
func setStartBtn() {
let startBtnText = NSLocalizedString("Start Test", comment: "")
startBtn.addTarget(self, action: #selector(self.clickStart(_:)), for: .touchUpInside) //Add a target and action of the start buton
startBtn.setTitle(startBtnText, for: [])
}
func setHomeText() {
let homeTextStr = NSLocalizedString("Home Text", comment: "")
homeText.text = homeTextStr
}
@objc func clickStart(_ sender: UIButton) {
}
}
解决方案:
点击开始功能看起来像我需要的(感谢下面接受的答案)。
@objc func clickStart(_ sender: UIButton) {
self.tabBarController?.selectedIndex = 1 // 1 is the second tab
}
这让我可以从视图中的 UIButton 移动到第二个标签栏项
【问题讨论】:
标签: ios swift uibutton uitabbarcontroller segue