簡體   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