从 Swift 中的 UIButton 转到 ViewController
Segue to a ViewController from a UIButton in Swift
我有一个 TabViewController,它有三个 Tab,它们指向三个 ViewController。
我可以使用 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) {
}
}
解决方案:
Click Start 功能看起来像我需要的(感谢下面接受的答案)。
@objc func clickStart(_ sender: UIButton) {
self.tabBarController?.selectedIndex = 1 // 1 is the second tab
}
这让我可以从视图中的 UIButton 移动到第二个标签栏项目
您需要将每个 vc 嵌入到 navigationController 内的选项卡中,然后使用它导航到嵌套的 vc
@objc func clickStart(_ sender: UIButton) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") // set id for the destination vc
self.navigationController?.pushViewController(vc,animated:true)
}
然后导航到选项卡内的另一个 vc
self.tabBarController?.selectedIndex = 1 // 1 is the second tab
我有一个 TabViewController,它有三个 Tab,它们指向三个 ViewController。
我可以使用 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) {
}
}
解决方案:
Click Start 功能看起来像我需要的(感谢下面接受的答案)。
@objc func clickStart(_ sender: UIButton) {
self.tabBarController?.selectedIndex = 1 // 1 is the second tab
}
这让我可以从视图中的 UIButton 移动到第二个标签栏项目
您需要将每个 vc 嵌入到 navigationController 内的选项卡中,然后使用它导航到嵌套的 vc
@objc func clickStart(_ sender: UIButton) {
let vc = self.storyboard!.instantiateViewController(withIdentifier: "VCID") // set id for the destination vc
self.navigationController?.pushViewController(vc,animated:true)
}
然后导航到选项卡内的另一个 vc
self.tabBarController?.selectedIndex = 1 // 1 is the second tab