我有两个通过segue链接的视图控制器。一个视图控制器(ViewController2
)中有多个视图,这些视图响应第一个视图控制器(pageIndex
)中的ViewController1
。因此,我要实现的目标是,如果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个视图均设置为隐藏
答案 0 :(得分:1)
将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
}
}
}