草庐IT

objective-c - Xib 没有出现在 View 中

coder 2023-09-13 原文

我有一个 Xib 文件,试图用我的 Storyboard设置它。 Xib 文件中的所有内容都很好,但由于某种原因,它没有显示。我从 GitHub 导入了一个文件,该文件设置为我的 Xib,它在 Objective-C 中,我设置了桥接,没有错误。但是当我运行它时,没有任何东西显示它是空白的。我没有在 View Controller 中设置什么吗?一切都以编程方式完成,我只是在 Storyboard 中设置类(class)。

Storyboard截图:

当我推送到 ViewController 时,模拟器会给我什么:

这是我应该看到的:

我正在努力实现的 - https://github.com/jberlana/JBCroppableView

我的 XIB 类(class)

import UIKit

class CropViewXIB: UIView {

    @IBOutlet weak var ImageView: JBCroppableImageView!

    @IBAction func SubAction(_ sender: Any) {
        ImageView.removePoint()
    }

    @IBAction func AddAction(_ sender: Any) {
        ImageView.addPoint()
    }

    @IBAction func UndoAction(_ sender: Any) {
        ImageView.reverseCrop()
    }

    @IBAction func CropAction(_ sender: Any) {
        ImageView.crop()
    }


    override init(frame: CGRect) {
        super.init(frame: frame)
        commomInit()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        commomInit()
    }

    private func commomInit(){
        Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)
        self.addSubview(ImageView)
        ImageView.frame = self.bounds
        ImageView.autoresizingMask = [.flexibleHeight, .flexibleWidth]

    }
}

我的 View Controller

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var cropView: CropViewXIB!

    override func viewDidLoad() {
        super.viewDidLoad()  
    }
}

最佳答案

问题是您实际上没有获得 UINib 对象的父 View 。

Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)

上面的行返回一个 [Any] 在你的情况下你甚至没有使用它返回的 View 。所以想法是从中获取第一个对象并将其转换为 UIView 例如:

 Bundle.main.loadNibNamed("CropViewXIB", owner: self, options: nil)?.first as? UIView

就个人而言,这就是我与 Nib 交互的方式。我创建了一个 UIView 类型的 view 属性,它可以被称为 nib 的父 View ,所有 subview 都被添加到它而不是 self.

像这样:

final class SomeNibView: UIView {
    public var view: UIView!


    private func setup() { // called to the initializer 
        // grab the views from loadNibNamed
        guard let _view = Bundle.main.loadNibNamed("name", owner: self, options: nil)?.first as? UIView else { return }
        // set it to our view property 
        view = _view
        // add this property to the nib subview aka self 
        addSubview(view)

        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }

    private func addMulitpleSubviews() {
        // instead of doing self.addSubview(....) when it comes to add other subviews 
       // you'll do this view.addSubview(....)
   }
}

关于objective-c - Xib 没有出现在 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47973446/

有关objective-c - Xib 没有出现在 View 中的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  3. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  4. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  5. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  8. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  9. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  10. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

随机推荐