因此,我尝试使用uiview实现登录/注册段控件,该控件包含两个用两个viewcontroller初始化的子视图(带有xib文件)。我从https://www.youtube.com/watch?v=e5nxg5NzLks视频中得到了这个想法,并将所有元素都放在了xib界面上。但是,即使我设置正确,该按钮也无法使用。
class RegisterLoginVC: UIViewController {
@IBOutlet weak var segmentedControl: BetterSegmentedControl!
@IBOutlet weak var container: UIView!
var subviews : [UIView]!
override func viewDidLoad() {
super.viewDidLoad()
print("this is main page superview")
//change the value of segmented control
segmentedControl.segments = LabelSegment.segments(withTitles: ["Login", "Sign Up"],
normalFont: UIFont(name: "Avenir", size: 20.0)!,
normalTextColor: Utilities.hexStringToUIColor(hex: "FEC341"),
selectedFont: UIFont(name: "Avenir-Heavy", size: 20.0)!,
selectedTextColor: .white)
//initialize login and signup subview
subviews = [UIView]()
subviews.append(LoginVC().view)
subviews.append(SignUpVC().view)
// add subviews to container
for v in subviews{
container.addSubview(v)
//add subview constraints to be framed inside container
v.translatesAutoresizingMaskIntoConstraints = false
v.leftAnchor.constraint(equalTo: container.leftAnchor).isActive = true
v.topAnchor.constraint(equalTo: container.topAnchor).isActive = true
v.rightAnchor.constraint(equalTo: container.rightAnchor).isActive = true
v.bottomAnchor.constraint(equalTo: container.bottomAnchor).isActive = true
}
//set login subview as default
container.bringSubviewToFront(subviews[0])
}
// when the segmented control was selected
@IBAction func segmentedCtrlChanged(_ sender: BetterSegmentedControl) {
self.container.bringSubviewToFront(subviews![Int(sender.index)])
}
}
上面是我的主页视图控制器,下面是我的登录子视图控制器
import UIKit
class LoginVC: UIViewController {
@IBOutlet weak var forgetPasswordLink: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
print("omg!")
// Do any additional setup after loading the view.
}
@IBAction func forgetPasswordTapped(_ sender: UIButton) {
print("normal button works")
}
}
我还附加了登录子视图xib和文件所有者屏幕截图
答案 0 :(得分:0)
我是发布问题的人,我以某种方式找到了一个讨厌的丑陋解决方案,但至少它是受视频作者的下一个视频启发的。
所以我实际上是从主视图控制器访问登录视图控制器实例,并访问其按钮出口,并且当我切换控件时,我在该按钮实例上添加了一个目标,并且可以正常工作。
但是这很麻烦,因为所有子视图的控制器工作都将在主视图控制器中处理(而不是它们自己的vc)。希望这能给别人一些见识,以帮助我找到错误并帮助我找到更好的解决方案。
谢谢