ios - iOS swift 中的自定义 View 导出在 View Controller 中不起作用

标签 ios swift custom-view

大家好,我在 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/

相关文章:

ios - iOS9 中 [_UIFlowLayoutSection updateEstimatedSizeForSection] 中的 UICollectionView 崩溃 - Xcode7

ios - ios 5 中带有 Storyboard 的 Table ViewController

ios - 在 1-Section Collection View 中混合匹配 Collection View 单元格

ios - 如何快速取出字符串的一部分

iOS SWIFT - NSJSONSerialization 后的 JSON 对象为 nil

Android:基于布局的自定义 View :怎么样?

objective-c - IOS保留在后端做什么

iOS MVC 实现

android - 什么时候必须扩充自定义 View 的布局?

ios - 如何为 Alamofire 请求全局设置基本 url?