使用 Segue,如何在不同的视图控制器中控制视图的可见性?
Using Segue, How do I control the visibility of a view in a different view controller?
我有两个通过 segue 链接的视图控制器。一个视图控制器 (ViewController2
) 中有多个视图响应第一个视图控制器 (ViewController1
) 中的 pageIndex
。所以我想要实现的是 onClick
如果 pageIndex == 0
在 ViewController1
上,其中一个视图应该可见。我已经用 Labels
和 ImageViews
多次这样做了,但是当涉及到使视图可见时,我似乎无法实现它。所以我需要帮助来实现它。
ViewController1:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segueIdentifier(for: segue) == .reveal,
let secondViewController = segue.destination as? ViewController2 {
if pageIndex == 0 {
//secondViewController.viewabc.isHidden = false
}else if pageIndex == 1{
//secondViewController.viewdef.isHidden = false
}else{
//secondViewController.viewghi.isHidden = false
}
secondViewController.transitioningDelegate = self
}
}
@IBAction func handleTap() {
performSegue(withIdentifier: .reveal, sender: self)
}
}
extension ViewController1: SegueHandlerType {
enum SegueIdentifier: String {
case reveal
}
}
ViewController2:
@IBOutlet weak var viewabc: UIView!
@IBOutlet weak var viewdef: UIView!
@IBOutlet weak var viewghi: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
故事板中的所有 3 个视图都设置为隐藏
将 pageIndex 值从 ViewController1 传递到 ViewController2。
class ViewController1: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segueIdentifier(for: segue) == .reveal,
let secondViewController = segue.destination as? ViewController2 {
secondViewController.transitioningDelegate = self
secondViewController.pageIndex = pageIndex
}
}
}
并根据 pageIndex 值更新 ViewController2 视图
class ViewController2: UIViewController {
var pageIndex: Int?
@IBOutlet weak var viewabc: UIView!
@IBOutlet weak var viewdef: UIView!
@IBOutlet weak var viewghi: UIView!
override func viewDidLoad() {
super.viewDidLoad()
if pageIndex == 0 {
self.viewabc.isHidden = false
}else if pageIndex == 1{
self.viewdef.isHidden = false
}else{
self.viewghi.isHidden = false
}
}
}
我有两个通过 segue 链接的视图控制器。一个视图控制器 (ViewController2
) 中有多个视图响应第一个视图控制器 (ViewController1
) 中的 pageIndex
。所以我想要实现的是 onClick
如果 pageIndex == 0
在 ViewController1
上,其中一个视图应该可见。我已经用 Labels
和 ImageViews
多次这样做了,但是当涉及到使视图可见时,我似乎无法实现它。所以我需要帮助来实现它。
ViewController1:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segueIdentifier(for: segue) == .reveal,
let secondViewController = segue.destination as? ViewController2 {
if pageIndex == 0 {
//secondViewController.viewabc.isHidden = false
}else if pageIndex == 1{
//secondViewController.viewdef.isHidden = false
}else{
//secondViewController.viewghi.isHidden = false
}
secondViewController.transitioningDelegate = self
}
}
@IBAction func handleTap() {
performSegue(withIdentifier: .reveal, sender: self)
}
}
extension ViewController1: SegueHandlerType {
enum SegueIdentifier: String {
case reveal
}
}
ViewController2:
@IBOutlet weak var viewabc: UIView!
@IBOutlet weak var viewdef: UIView!
@IBOutlet weak var viewghi: UIView!
override func viewDidLoad() {
super.viewDidLoad()
}
故事板中的所有 3 个视图都设置为隐藏
将 pageIndex 值从 ViewController1 传递到 ViewController2。
class ViewController1: UIViewController {
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segueIdentifier(for: segue) == .reveal,
let secondViewController = segue.destination as? ViewController2 {
secondViewController.transitioningDelegate = self
secondViewController.pageIndex = pageIndex
}
}
}
并根据 pageIndex 值更新 ViewController2 视图
class ViewController2: UIViewController {
var pageIndex: Int?
@IBOutlet weak var viewabc: UIView!
@IBOutlet weak var viewdef: UIView!
@IBOutlet weak var viewghi: UIView!
override func viewDidLoad() {
super.viewDidLoad()
if pageIndex == 0 {
self.viewabc.isHidden = false
}else if pageIndex == 1{
self.viewdef.isHidden = false
}else{
self.viewghi.isHidden = false
}
}
}