swift 尝试从iOS中的捆绑包加载自定义视图时出错

ljsrvy3e  于 2023-03-28  发布在  Swift
关注(0)|答案(1)|浏览(130)

我有这个代码的自定义视图。

public override init(frame: CGRect) {
    super.init(frame: frame)
    setUpView()
}

public required init?(coder: NSCoder) {
    super.init(coder: coder)
    setUpView()
}

func setUpView() {
    let name = String(describing: type(of: self))
    let nib = UINib(nibName: name, bundle: .main)
    nib.instantiate(withOwner: self, options: nil)
    fixViewInContainer(view: mainView, containerView: self)
}

我得到了一个类似于Thread 3的错误:此行中的EXC_BREAKPOINT(code=1,subcode=0x1048db98c)
nib.instantiate(withOwner:self,选项:无)
这是XIB文件

错误

有什么想法吗...谢谢

qlvxas9a

qlvxas9a1#

我认为你的观点是错误的...
这里有一个简单的例子...
课程:

class GenericErrorView: UIView {
    
    @IBOutlet var mainView: UIView!
    @IBOutlet var theLabel: UILabel!
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setUpView()
    }
    
    public required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUpView()
    }
    
    func setUpView() {
        let name = String(describing: type(of: self))
        let bundle = Bundle(for: Self.self)
        let nib = UINib(nibName: name, bundle: bundle)
        
        nib.instantiate(withOwner: self, options: nil)
        
        fixViewInContainer(view: mainView, containerView: self)
    }
    
    private func fixViewInContainer(view: UIView, containerView: UIView) {
        containerView.addSubview(view)
        view.frame = self.bounds
        view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    }
    
}

连接如下所示:

以及样本视图控制器:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let gView = GenericErrorView()
        
        gView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(gView)
        
        let g = view.safeAreaLayoutGuide
        NSLayoutConstraint.activate([
            gView.widthAnchor.constraint(equalToConstant: 260.0),
            gView.heightAnchor.constraint(equalTo: gView.widthAnchor, multiplier: 0.75),
            gView.centerXAnchor.constraint(equalTo: g.centerXAnchor),
            gView.centerYAnchor.constraint(equalTo: g.centerYAnchor),
        ])
        
        gView.mainView.backgroundColor = .cyan
        gView.theLabel.text = "Not an Error :)"
        
    }
    
}

下面是xib源代码,您可以详细查看:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_0" orientation="portrait" appearance="light"/>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
        <capability name="Safe area layout guides" minToolsVersion="9.0"/>
        <capability name="System colors in document resources" minToolsVersion="11.0"/>
        <capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="GenericErrorView" customModule="Feb2023" customModuleProvider="target">
            <connections>
                <outlet property="mainView" destination="iN0-l3-epB" id="BNM-lB-59v"/>
                <outlet property="theLabel" destination="zDj-7H-8Sh" id="ob8-nb-5el"/>
            </connections>
        </placeholder>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="iN0-l3-epB">
            <rect key="frame" x="0.0" y="0.0" width="173" height="128"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="zDj-7H-8Sh">
                    <rect key="frame" x="65.5" y="53.5" width="42" height="21"/>
                    <fontDescription key="fontDescription" type="system" pointSize="17"/>
                    <nil key="textColor"/>
                    <nil key="highlightedColor"/>
                </label>
            </subviews>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <color key="backgroundColor" systemColor="systemBackgroundColor"/>
            <constraints>
                <constraint firstItem="zDj-7H-8Sh" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="centerY" id="Icu-KL-0xk"/>
                <constraint firstItem="zDj-7H-8Sh" firstAttribute="centerX" secondItem="iN0-l3-epB" secondAttribute="centerX" id="MeG-E2-0NI"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <point key="canvasLocation" x="224.0625" y="-23.239436619718312"/>
        </view>
    </objects>
    <resources>
        <systemColor name="systemBackgroundColor">
            <color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
        </systemColor>
    </resources>
</document>

当你运行它时,它看起来像这样:

相关问题