首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Nib中的IBOutlets在awakeFromNib和initWithCoder上都是零-没有子视图。

在Nib中的IBOutlets在awakeFromNib和initWithCoder上都是零-没有子视图。
EN

Stack Overflow用户
提问于 2014-08-07 10:26:29
回答 1查看 3.9K关注 0票数 11

我有一个定制的UIView子类,它通过Nib文件( UIImageView、UIView和UITextField)和IBOutlets集向它添加了三个子视图。但是当awakeFromNibinitWithCoder在子类中被调用时,所有的IBOutlets都是nil,甚至子视图数组也是

我使用Swift和XCode 6 beta 5,其中这个UIView 子类位于一个框架中,我在我的项目中将UIView子类添加到一个故事板中,这样我就可以使用新的Live 技术。但我也无法在Live中看到添加的子视图,这是非常可悲的。

我找不到任何方法来解决这个问题,下面是我的UIView子类的代码:

代码语言:javascript
运行
复制
import UIKit

@IBDesignable public class BlurredSheetEntryView: UIView {

    @IBOutlet var iconImageView: UIImageView?
    @IBOutlet var separatorView: UIView?
    @IBOutlet public var textField: UITextField?

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

    required public init(coder aDecoder: NSCoder!) {
        super.init(coder: aDecoder)

        for subView in self.subviews {
            println("subView: \(subView)")
        }

        println("self.textField: \(self.textField)")
        println("self.iconImageView: \(self.iconImageView)")
        println("self.separatorView: \(self.separatorView)")
    }

    override public func awakeFromNib() {

        for subView in self.subviews {
            println("subView: \(subView)")
        }

        println("self.textField: \(self.textField)")
        println("self.iconImageView: \(self.iconImageView)")
        println("self.separatorView: \(self.separatorView)")

    }
}

我的笔尖文件是这样的:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="6205" systemVersion="14A314h" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
    <dependencies>
        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6198"/>
        <capability name="Aspect ratio constraints" minToolsVersion="5.1"/>
    </dependencies>
    <objects>
        <placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner"/>
        <placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
        <view contentMode="scaleToFill" id="iN0-l3-epB" customClass="BlurredSheetEntryView" customModule="BlurredSheetEntry" customModuleProvider="target">
            <rect key="frame" x="0.0" y="0.0" width="300" height="50"/>
            <autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
            <subviews>
                <imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="ZnN-gy-Mc6">
                    <rect key="frame" x="8" y="5" width="40" height="40"/>
                    <constraints>
                        <constraint firstAttribute="width" secondItem="ZnN-gy-Mc6" secondAttribute="height" multiplier="1:1" id="C4N-zC-woh"/>
                    </constraints>
                </imageView>
                <textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="Placeholder" minimumFontSize="17" clearButtonMode="whileEditing" translatesAutoresizingMaskIntoConstraints="NO" id="npg-xI-NSo">
                    <rect key="frame" x="66" y="8" width="226" height="34"/>
                    <fontDescription key="fontDescription" type="system" pointSize="15"/>
                    <textInputTraits key="textInputTraits"/>
                </textField>
                <view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="8lA-9y-yoA">
                    <rect key="frame" x="56" y="5" width="2" height="40"/>
                    <color key="backgroundColor" white="0.66666666666666663" alpha="1" colorSpace="calibratedWhite"/>
                    <constraints>
                        <constraint firstAttribute="width" constant="2" id="QgG-pW-8q0"/>
                    </constraints>
                </view>
            </subviews>
            <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
            <constraints>
                <constraint firstAttribute="bottom" secondItem="8lA-9y-yoA" secondAttribute="bottom" constant="5" id="51o-Il-vGA"/>
                <constraint firstItem="8lA-9y-yoA" firstAttribute="leading" secondItem="ZnN-gy-Mc6" secondAttribute="trailing" constant="8" id="AEG-I2-vsT"/>
                <constraint firstAttribute="bottom" secondItem="npg-xI-NSo" secondAttribute="bottom" constant="8" id="E0H-fN-lg4"/>
                <constraint firstItem="8lA-9y-yoA" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="5" id="EYR-L8-96J"/>
                <constraint firstAttribute="bottom" secondItem="ZnN-gy-Mc6" secondAttribute="bottom" constant="5" id="GFW-R2-jGI"/>
                <constraint firstAttribute="trailing" secondItem="npg-xI-NSo" secondAttribute="trailing" constant="8" id="Lws-lv-n37"/>
                <constraint firstItem="ZnN-gy-Mc6" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="5" id="Pv0-ZV-aT4"/>
                <constraint firstItem="ZnN-gy-Mc6" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="8" id="XOI-jO-Sv7"/>
                <constraint firstItem="npg-xI-NSo" firstAttribute="top" secondItem="iN0-l3-epB" secondAttribute="top" constant="8" id="dA3-Wg-XfN"/>
                <constraint firstItem="npg-xI-NSo" firstAttribute="leading" secondItem="8lA-9y-yoA" secondAttribute="trailing" constant="8" id="mq6-6r-Z2b"/>
            </constraints>
            <nil key="simulatedStatusBarMetrics"/>
            <freeformSimulatedSizeMetrics key="simulatedDestinationMetrics"/>
            <connections>
                <outlet property="iconImageView" destination="ZnN-gy-Mc6" id="0Hp-sr-mqx"/>
                <outlet property="separatorView" destination="8lA-9y-yoA" id="VFx-En-K3c"/>
                <outlet property="textField" destination="npg-xI-NSo" id="tIy-N2-g2j"/>
            </connections>
            <point key="canvasLocation" x="371" y="327"/>
        </view>
    </objects>
</document>

更新:我创建了一个绝对简单的示例项目,其中我只做了以下操作:

  • 为iOS选择主视图模板
  • 用Xib文件添加名为"MyCustomTableViewCell“的新框架
  • 在Xib文件中添加一个蓝色背景色的UIView作为子视图
  • 在Swift文件中添加@IBOutlet和连接
  • 在Swift文件中的awakeFromNib()中添加println()语句
  • 将主tableView中的单元格类设置为框架中的子类

当我构建并运行并单击右上角的"+"-Sign时,就会在控制台上打印出“0”作为@IBOutlet的结果。这是我的问题,也是困扰我的。但也许我做错了什么..。

我已经上传了示例项目,下面是下载网址:https://www.dropbox.com/s/vcb0sa98tw6i1iv/NilObjectsFromXib.zip?dl=0

我使用一个框架,因为我必须这样做,因为我想在我的主要故事板中将我的子类作为一个Live。

(谢谢你提前提供帮助!)

EN

回答 1

Stack Overflow用户

发布于 2014-12-15 15:10:44

我查看了您的项目,并:您需要在项目设置中将XIB文件添加到Copy bundle resourses中。

您需要注册自定义单元格XIB,以便通过以下方法在表视图中使用:

代码语言:javascript
运行
复制
 override func viewDidLoad() {
        super.viewDidLoad()
        self.tableView.registerNib(UINib(nibName:"MyCustomTableViewCell", bundle: nil), forCellReuseIdentifier: "CustomCell")
    }

然后,您可以在表中使用自定义单元格:

代码语言:javascript
运行
复制
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("CustomCell", forIndexPath: indexPath) as MyCustomTableViewCell

    // Do something with your cell

    return cell
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25180110

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档