ios @IBDesignable 崩溃代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31265906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@IBDesignable crashing agent
提问by jbehrens94
When I write my own UIButton
-extended class and make it @IBDesignable
, I receive two errors in Interface Builder, namely:
当我编写自己的UIButton
-extended class 并 make it 时@IBDesignable
,我在 Interface Builder 中收到两个错误,即:
- Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent crashed because the fd closed
- Main.storyboard: error: IB Designables: Failed to render instance of RandjeUIButton: The agent crashed
- Main.storyboard: error: IB Designables: Failed to update auto layout status: The agent crashed because the fd closed
- Main.storyboard:错误:IB Designables:无法呈现 RandjeUIButton 的实例:代理崩溃
Here is my code:
这是我的代码:
import UIKit
@IBDesignable
class RandjeUIButton: UIButton {
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.backgroundColor = UIColor.blackColor()
}
}
I am working in Xcode 7 beta 2 on OS X 10.11 beta 2. (Running in VM)
我在 OS X 10.11 beta 2 上使用 Xcode 7 beta 2。(在 VM 中运行)
回答by mathielo
Xcode's Interface Builder requires that you implement bothor neitherinitializers for @IBDesignable
classes to render properly in IB.
Xcode 的 Interface Builder 要求您为类实现两个或两个初始化器,@IBDesignable
以便在 IB 中正确呈现。
If you implement required init(coder aDecoder: NSCoder)
you'll need to override init(frame: CGRect)
as well, otherwise "the agent will crash" as seen in the errors thrown by Xcode.
如果您实现,required init(coder aDecoder: NSCoder)
您还需要覆盖init(frame: CGRect)
,否则“代理将崩溃”,如 Xcode 抛出的错误所示。
To do so add the following code to your class:
为此,将以下代码添加到您的类中:
override init(frame: CGRect) {
super.init(frame: frame)
}
回答by Azure Yu
I've met the same problem and solved it this way:
我遇到了同样的问题并通过以下方式解决了它:
- The Problem:
- 问题:
error: IB Designables: Failed to update auto layout status: The agent crashed
错误:IB Designables:无法更新自动布局状态:代理崩溃
- Find the debug button in the inspection file and click it.
- 在检查文件中找到调试按钮并单击它。
Then the Xcode will tell you where the prolem is. In my case I drop the
IBDesignable
before the class.Then I clean and rebuild it, the error disappeared
然后Xcode会告诉你问题在哪里。就我而言,我
IBDesignable
在课前放弃了。然后我清理并重建它,错误消失了
回答by voidref
There are a myriad of problems that can cause this. Fire up Console, and look for the crash report IBDesignablesCocoaTouch...
有无数的问题会导致这种情况。启动控制台,并查找崩溃报告IBDesignablesCocoaTouch...
I just sorted out a problem with a 3rd party designable which had issues with the valueForKey
semantics.
我刚刚解决了一个与valueForKey
语义有关的第 3 方设计的问题。
回答by Glenn Barnett
In Xcode 7.3, none of the above solutions worked for me, but the accepted answer to this question did: Failed to render instance of IB Designables
在 Xcode 7.3 中,上述解决方案都不适用于我,但对这个问题的公认答案是:无法呈现 IB Designables 的实例
- Clear Xcode derived data for the project. They are in ~/Library/Developer/Xcode/DerivedData
- Clean your current build by pressing ??K
- Build your project
- In storyboard go to Editor menu and do Refresh All Views; wait for build to be completed and errors should be gone
- 清除项目的 Xcode 派生数据。它们在 ~/Library/Developer/Xcode/DerivedData
- 按 ??K 清理您当前的构建
- 构建你的项目
- 在故事板中,转到编辑器菜单并刷新所有视图;等待构建完成,错误应该消失
I did not need to do the 4th step to solve the problem (and get my PaintCode-drawRect'd UIView to paint in the storyboard), included here just in case.
我不需要执行第 4 步来解决问题(并让我的 PaintCode-drawRect 的 UIView 在情节提要中绘画),这里包含以防万一。
Credit to @Mojtaba and @WeZZard
归功于@Mojtaba 和@WeZZard
回答by StuFF mc
For me, it was not using #if !TARGET_INTERFACE_BUILDER
that got me. Basically I had some code that would result in accessing a path in my Bundle...
对我来说,它没有#if !TARGET_INTERFACE_BUILDER
得到我的使用。基本上我有一些代码会导致访问我的捆绑包中的路径......
Bundle.main.path(forResource: "Foo", ofType: "plist")
The problem is that when running in IB (and not in your app), Bundle.main
isn't your app...
问题是,当在 IB 中运行(而不是在您的应用程序中)时,Bundle.main
您的应用程序不是...
(lldb) po Bundle.main
NSBundle </Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Overlays> (loaded)
So the answer to this is simply to review your @IBDesignable UIView
code carefully and use #if !TARGET_INTERFACE_BUILDER
for anything (in my case calls to CoreLocation
for example) that doesn't make sense when running at design time.
因此,对此的答案只是@IBDesignable UIView
仔细检查您的代码并将#if !TARGET_INTERFACE_BUILDER
其CoreLocation
用于在设计时运行时没有意义的任何内容(例如在我的情况下调用)。
How I debugged and found this
我如何调试并找到这个
This is what you see when using the Editor -> Debug Selected View
while selecting your @IBDesignable UIView
in your storyboard:
这是您在使用Editor -> Debug Selected View
while@IBDesignable UIView
在您的故事板中选择您时看到的内容:
You'll then crash at the right location
然后你会在正确的位置坠毁
In my case, as you can see initXXXX
was doing an assert
, which was crashing at design time, because it was looking for a value in file in my Bundle?—?which is Xcode, at design time.
在我的例子中,正如你所看到的initXXXX
那样assert
,它在设计时崩溃了,因为它在我的 Bundle 中寻找文件中的值?-?这是 Xcode,在设计时。
回答by Adrien Yvon
I found something really important when using an UIImage in any class marked @IBDesignable. The classic UIImage init would crash the agent:
在任何标记为 @IBDesignable 的类中使用 UIImage 时,我发现了一些非常重要的东西。经典的 UIImage init 会使代理崩溃:
let myImage = UIImage(named: String) // crash the agent
The solution is to use this init method of UIImage:
解决方法是使用UIImage的这个init方法:
let myImage = UIImage(named: String, in: Bundle, compatibleWith: UITraitCollection)
Working code example:
工作代码示例:
let appBundle = Bundle(for: type(of: self))
let myImage = UIImage(named: "myImage", in: bundle, compatibleWith: self.traitCollection))
Where self is your class with the @IBDesignable keyword. Xcode 9.4, Swift 4.1
其中 self 是带有 @IBDesignable 关键字的类。Xcode 9.4,斯威夫特 4.1
回答by escapedcanadian
XCode 10, Swift 4.2
XCode 10,斯威夫特 4.2
I found an answer here
我在这里找到了答案
Solution was simple - changing the way the bundle was resolved in the required init?(coder aDecoder: NSCoder)
method of my custom view class.
解决方案很简单 - 在required init?(coder aDecoder: NSCoder)
我的自定义视图类的方法中更改包的解析方式。
Bundle.main.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
needed to be changed to
需要改为
let bundle = Bundle(for: TestView.self)
bundle.loadNibNamed(String(describing: TestView.self), owner: self, options: nil)
回答by Charmi
Just re-open a code on another Xcode setup System. In my case it Worked.
只需在另一个 Xcode 设置系统上重新打开一个代码。就我而言,它有效。
回答by Sam
回答by vikram jeet singh
This is how I solves this problem:
这是我解决这个问题的方法:
- Make sure the outlets are properly initialized
- @IBInspectable properties would be properly initialized
- in xib file, click on File's owner placeholder, Go to Identity inspector, make sure it wont have any default values
- 确保插座已正确初始化
- @IBInspectable 属性将被正确初始化
- 在 xib 文件中,单击文件的所有者占位符,转到身份检查器,确保它没有任何默认值
import UIKit
@IBDesignable class TestView: UIView {
@IBOutlet weak var label: UILabel!
@IBInspectable var textLabel1: String? {
get {
return label.text
}
set {
label.text = newValue
}
}
// MARK: Setup
var view1: UIView!
var nibName: String = "TestView"
override init(frame: CGRect) {
super.init(frame: frame)
xibSetup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
xibSetup()
}
private func xibSetup() {
view1 = loadViewFromNib()
view1?.frame = self.bounds
view1?.autoresizingMask = [.FlexibleWidth, .FlexibleHeight]
if view1 != nil {
addSubview(view1!)
//textLabel1 = "ok"
}
}
private func loadViewFromNib() -> UIView? {
let bundle = NSBundle(forClass: self.dynamicType)
let nib = UINib(nibName: nibName, bundle: bundle)
for object in nib.instantiateWithOwner(self, options: nil) {
if let view: UIView = object as? UIView {
return view
}
}
return nil
}
}
Usage:
用法: