简体   繁体   中英

Custom view outlets in iOS swift are not working in view controller

Hi all I have taken a view controller and one custom view in swift

ViewController

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

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

I am getting error in the last line.. bad excess and in console fatal error: unexpectedly found nil while unwrapping an Optional value after loading that view in view controller

And the Base View is

import UIKit
class BaseView: UIView {

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

use

myview.maleBtn.alpha = 0.0

as you are not loading nib in self , you are loading in myview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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