Ios 使用@IBInspectable设置UItextField值

Ios 使用@IBInspectable设置UItextField值,ios,swift,Ios,Swift,我很难理解@IBInspectable。请看下面的代码 @IBDesignable class TagListMyView: UIView { let kCONTENT_XIB_NAME = "TagListMyView" @IBOutlet var contentView: UIView! @IBOutlet weak var nameFieldValue: UITextField! @IBOutlet weak var companyFieldValue:

我很难理解
@IBInspectable
。请看下面的代码

@IBDesignable
class TagListMyView: UIView {

    let kCONTENT_XIB_NAME = "TagListMyView"

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var nameFieldValue: UITextField!
    @IBOutlet weak var companyFieldValue: UITextField!

    @IBInspectable var border:CGFloat = 2.0{
        didSet{
            nameFieldValue.layer.borderWidth = border
        }
    }

    @IBInspectable var nameValue:String = "My dummy text"{
        didSet{
            nameFieldValue.text = nameValue ----> This is not working 
        }
    }

    @IBInspectable var bkgColor:UIColor = UIColor.blue {
        didSet{
            contentView.layer.backgroundColor = bkgColor.cgColor ---> This is working 
        }
    }

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

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

    func commonInit(){
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: kCONTENT_XIB_NAME, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        addSubview(view)
    }
}

第一个屏幕截图

我无法理解为什么我的字段
nameValue
的值没有反映在情节提要上。请看下面的截图。请看情节提要属性检查器

第二个屏幕截图


IBDesignable/IBInspectable仅限于在Xcode Interface Builder中绘制。你甚至不能画阴影,但你可以画渐变,所以这是一点和那。
您可以在运行时使用的文本字段上设置名称或值,但它不会在Xcode界面生成器中更新。

您的代码对我来说似乎工作正常-在
名称值
字段中输入文本将显示在
名称字段值
文本字段中

如果我删除该字段中的文本,它将显示“默认”(浅灰色),但“我的虚拟文本”的默认值不会被填充

要获取要显示的默认值,请在
commonInit()的末尾添加此行:


编辑

不知道为什么它不适合你。。。您的XIB中是否可能存在配置/连接不正确的情况

在新项目中尝试此功能

为您的
TagListMyView
类添加代码

File -> New -> File... Empty User Interface
另存为
TagListMyView

右键单击新的XIB文件并选择
openas。。。源代码

删除其中的所有内容,然后粘贴到以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
        <capability name="Safe area layout guides" minToolsVersion="9.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="TagListMyView" customModule="UsingMyLib" customModuleProvider="target">
            <connections>
                <outlet property="companyFieldValue" destination="kES-3d-prB" id="hm2-Td-Hwh"/>
                <outlet property="contentView" destination="iN0-l3-epB" id="4ze-kf-v9a"/>
                <outlet property="nameFieldValue" destination="rR2-wR-SQc" id="Zce-pm-bo9"/>
            </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="375" height="191"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="rR2-wR-SQc">
                    <rect key="frame" x="16" y="8" width="343" height="30"/>
                    <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <nil key="textColor"/>
                    <fontDescription key="fontDescription" type="system" pointSize="19"/>
                    <textInputTraits key="textInputTraits"/>
                </textField>
                <textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="kES-3d-prB">
                    <rect key="frame" x="16" y="46" width="343" height="30"/>
                    <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <nil key="textColor"/>
                    <fontDescription key="fontDescription" type="system" pointSize="19"/>
                    <textInputTraits key="textInputTraits"/>
                </textField>
            </subviews>
            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            <constraints>
                <constraint firstItem="kES-3d-prB" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="16" id="NCh-WY-orP"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="kES-3d-prB" secondAttribute="trailing" constant="16" id="QSR-aS-Pbc"/>
                <constraint firstItem="rR2-wR-SQc" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" constant="8" id="Qip-9C-I54"/>
                <constraint firstItem="kES-3d-prB" firstAttribute="top" secondItem="rR2-wR-SQc" secondAttribute="bottom" constant="8" id="R96-jo-2bN"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="kES-3d-prB" secondAttribute="bottom" constant="8" id="Rhu-LS-9Bl"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="rR2-wR-SQc" secondAttribute="trailing" constant="16" id="XMy-BD-5em"/>
                <constraint firstItem="rR2-wR-SQc" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="16" id="z6P-St-aSt"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <point key="canvasLocation" x="138.40000000000001" y="-59.820089955022496"/>
        </view>
    </objects>
</document>

嗨,你能给我参考一些突出这些要点的文档吗。我翻遍了整个互联网,但找不到任何这样的信息。你可以画阴影。试着把
nameValue
变成可选的
String
。我不知道为什么它对我不起作用。你是否对我的代码做了任何更改以使其正常工作?你可以分享你的代码,这是工作。谢谢我会尝试这个代码。我很快就会接受你的答复
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES">
    <device id="retina4_7" orientation="portrait">
        <adaptation id="fullscreen"/>
    </device>
    <dependencies>
        <deployment identifier="iOS"/>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
        <capability name="Safe area layout guides" minToolsVersion="9.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="TagListMyView" customModule="UsingMyLib" customModuleProvider="target">
            <connections>
                <outlet property="companyFieldValue" destination="kES-3d-prB" id="hm2-Td-Hwh"/>
                <outlet property="contentView" destination="iN0-l3-epB" id="4ze-kf-v9a"/>
                <outlet property="nameFieldValue" destination="rR2-wR-SQc" id="Zce-pm-bo9"/>
            </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="375" height="191"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="rR2-wR-SQc">
                    <rect key="frame" x="16" y="8" width="343" height="30"/>
                    <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <nil key="textColor"/>
                    <fontDescription key="fontDescription" type="system" pointSize="19"/>
                    <textInputTraits key="textInputTraits"/>
                </textField>
                <textField opaque="NO" contentMode="scaleToFill" verticalHuggingPriority="251" contentHorizontalAlignment="left" contentVerticalAlignment="center" borderStyle="roundedRect" textAlignment="natural" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="kES-3d-prB">
                    <rect key="frame" x="16" y="46" width="343" height="30"/>
                    <color key="backgroundColor" red="0.99806135890000003" green="0.96808904409999996" blue="0.12760734560000001" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
                    <nil key="textColor"/>
                    <fontDescription key="fontDescription" type="system" pointSize="19"/>
                    <textInputTraits key="textInputTraits"/>
                </textField>
            </subviews>
            <color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
            <constraints>
                <constraint firstItem="kES-3d-prB" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="16" id="NCh-WY-orP"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="kES-3d-prB" secondAttribute="trailing" constant="16" id="QSR-aS-Pbc"/>
                <constraint firstItem="rR2-wR-SQc" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" constant="8" id="Qip-9C-I54"/>
                <constraint firstItem="kES-3d-prB" firstAttribute="top" secondItem="rR2-wR-SQc" secondAttribute="bottom" constant="8" id="R96-jo-2bN"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="bottom" relation="greaterThanOrEqual" secondItem="kES-3d-prB" secondAttribute="bottom" constant="8" id="Rhu-LS-9Bl"/>
                <constraint firstItem="vUN-kp-3ea" firstAttribute="trailing" secondItem="rR2-wR-SQc" secondAttribute="trailing" constant="16" id="XMy-BD-5em"/>
                <constraint firstItem="rR2-wR-SQc" firstAttribute="leading" secondItem="vUN-kp-3ea" secondAttribute="leading" constant="16" id="z6P-St-aSt"/>
            </constraints>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <viewLayoutGuide key="safeArea" id="vUN-kp-3ea"/>
            <point key="canvasLocation" x="138.40000000000001" y="-59.820089955022496"/>
        </view>
    </objects>
</document>
@IBDesignable
class TagListMyView: UIView {

    let kCONTENT_XIB_NAME = "TagListMyView"

    @IBOutlet var contentView: UIView!
    @IBOutlet weak var nameFieldValue: UITextField!
    @IBOutlet weak var companyFieldValue: UITextField!

    @IBInspectable var border:CGFloat = 2.0{
        didSet{
            nameFieldValue.layer.borderWidth = border

        }
    }

    @IBInspectable var nameValue:String = "My dummy text"{
        didSet{
            nameFieldValue.text = nameValue //----> This is not working

        }
    }

    @IBInspectable var bkgColor:UIColor = UIColor.blue {
        didSet{
            contentView.layer.backgroundColor = bkgColor.cgColor ///---> This is working
        }
    }

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

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

    func commonInit(){
        let bundle = Bundle.init(for: type(of: self))
        let nib = UINib(nibName: kCONTENT_XIB_NAME, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil)[0] as! UIView
        view.frame = bounds
        addSubview(view)
        nameFieldValue.text = nameValue
    }
}