【问题标题】:Custom view outlets in iOS swift are not working in view controlleriOS swift中的自定义视图出口在视图控制器中不起作用
【发布时间】:2016-03-02 11:19:03
【问题描述】:

大家好,我在 swift 中使用了一个视图控制器和一个自定义视图

视图控制器

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

我在最后一行出现错误.. 严重过剩和控制台致命错误:在展开可选值时意外发现 nil 在视图控制器中加载该视图后

基础视图是

import UIKit
class BaseView: UIView {

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

【问题讨论】:

  • maleBtn 是否与故事板正确链接?
  • 是的..我做对了

标签: ios swift custom-view


【解决方案1】:

使用

myview.maleBtn.alpha = 0.0

由于您没有在 self 中加载 nib,而是在 myview 中加载

【讨论】:

  • 如果您仍然遇到错误,请告诉我,如果它有效,请批准,以便任何人都可以使用它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 2015-08-11
  • 2013-03-08
相关资源
最近更新 更多