大家好,我在 swift 中使用了一个 View Controller 和一个自定义 View
View Controller
import UIKit
class ViewController: UIViewController {
var genderView : GenderOptionView!
override func viewDidLoad() {
super.viewDidLoad()
loadGenderCustomView()
}
func loadGenderCustomView() {
genderView = GenderOptionView(frame: CGRectMake(0,0,rectIs.size.width * 0.95, rectIs.size.height * 0.355)) { (ScreeenType) -> () in
if(ScreeenType == 0)
{
print("Show Gender");
//self.ShowLoginScreen()
}
};
self.view.addSubview(genderView)
}
}
自定义 View
import UIKit
class GenderOptionView: BaseView {
//static let sharedGenderOptionViewInstance = GenderOptionView()
@IBOutlet weak var maleImgView: UIImageView!
@IBOutlet weak var maleBtn: UIButton!
@IBOutlet weak var maleLbl: UILabel!
internal init(frame: CGRect, OnActions:(ScreeenType:NSInteger) -> ()){
super.init(frame: frame)
let myView = NSBundle.mainBundle().loadNibNamed("GenderOptionView", owner: self, options: nil)[0] as! UIView
self.frame = CGRectMake(frame.origin.x + 6,315,self.bounds.width,myView.bounds.height)
myView.frame = frame
//myView.backgroundColor = UIColor.greenColor()
//self.backgroundColor = UIColor.yellowColor()
OnActionsRef = OnActions;
self.addSubview(myView)
self.maleBtn.alpha = 0.0// I am getting here.. bad excess and in console fatal error: unexpectedly found nil while unwrapping an Optional value
}
}
我在最后一行出现错误.. bad excess and in console fatal error: unexpectedly found nil while unwrapping an Optional value 在 View Controller 中加载该 View 后
基础 View 是
import UIKit
class BaseView: UIView {
var OnActionsRef : (ScreeenType:NSInteger) -> () = {_ in };
}
最佳答案
使用
myview.maleBtn.alpha = 0.0
因为你没有在 self 中加载 nib,所以你在 myview 中加载
关于ios - iOS swift 中的自定义 View 导出在 View Controller 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745716/