【问题标题】:Instantiate view with XIB使用 XIB 实例化视图
【发布时间】:2014-12-17 11:20:39
【问题描述】:

我有一个按照指南 (How do I create a custom iOS view class and instantiate multiple copies of it (in IB)?) 创建的 xib,但我有一个问题:

如何从代码中实例化 if?

那么我应该在 viewDidLoad 中写什么而不是

self.myView = [[MyView alloc] initWithFrame:self.view.bounds];

我知道如何用故事板来实例化它,但我不知道如何从代码中做到这一点。谢谢!

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您必须添加-loadNibNamed 方法,如下所示:

    将以下代码添加到 Your_View init 方法中:

    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"Your_nib_name" owner:self options:nil];
    UIView *mainView = [subviewArray objectAtIndex:0];
    [self addSubview:mainView];
    

    在这里参考这两个问题:

    Adding a custom subview (created in a xib) to a view controller's view - What am I doing wrong

    iOS: Custom view with xib

    编辑:

    在您的ViewController.m 文件中

    #import CustomView.h   <--- //import your_customView.h file
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        CustomView *customView = [[CustomView alloc]init];
        [self.view addSubview:customView];
    }
    

    【讨论】:

    • 是的,这很清楚。但是我应该在 ViewController 中做什么才能从 nib 添加视图?
    • 什么是[CustomView customView]
    • customView 将是您在其中初始化视图的CustomView 类的方法。即您在其中加载了视图的 nib 文件。如果您在-init 中编写该代码,那么只需编写[[CustomView alloc]init];
    【解决方案2】:

    这是我使用的 Swift 4 扩展:

    public extension UIView {
        // Load the view for this class from a XIB file
        public func viewFromNibForClass(index : Int = 0) -> UIView {
            let bundle = Bundle(for: type(of: self))
            let nib = UINib(nibName: String(describing: type(of: self)), bundle: bundle)
            return nib.instantiate(withOwner: self, options: nil)[index] as! UIView
        }
    
        // Load the view for this class from a XIB file and add it
        public func initViewFromNib() {
            let view = viewFromNibForClass()
            addSubview(view)
            //view.frame = bounds  // No Autolayout
            view.constrainToFillSuperview()  // Autolayout helper
        }
    }
    

    像这样使用它:

    override init(frame: CGRect) {
        super.init(frame: frame)
        initViewFromNib()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        initViewFromNib()
    }
    

    【讨论】:

      【解决方案3】:

      斯威夫特 4

      extension UIView {
          private class func _makeFromNib<T: UIView>() -> T {
              let nibName = NSStringFromClass(T.self).components(separatedBy: ".").last ?? ""
              let bundle = Bundle(for: T.self)
              let nib = UINib(nibName: nibName, bundle: bundle)
              let view = nib.instantiate(withOwner: T.self, options: nil)[0]
              return view as! T
          }
      
          class func makeFromNib() -> Self {
              return _makeFromNib()
          }
      }
      

      使用

      let myView = MyView.makeFromNib()
      let profileView = ProfileView.makeFromNib()
      
      

      【讨论】:

        猜你喜欢
        • 2015-03-24
        • 2014-10-24
        • 1970-01-01
        • 2018-02-15
        • 1970-01-01
        • 2012-02-27
        • 2013-02-01
        • 1970-01-01
        • 2019-11-12
        相关资源
        最近更新 更多