繁体   English   中英

iOS swift中的自定义视图出口在视图控制器中不起作用

[英]Custom view outlets in iOS swift are not working in 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)
}
}

自订检视

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
}
}

我在最后一行收到错误..严重超额和控制台致命错误:在视图控制器中加载该视图后,在展开Optional值时意外发现nil

基本视图是

import UIKit
class BaseView: UIView {

var OnActionsRef : (ScreeenType:NSInteger) -> () = {_ in };
}

采用

myview.maleBtn.alpha = 0.0

因为您没有在自己中加载笔尖,所以您正在myview中加载

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM