HHXibHelper - Xcode Pulg In
github地址: https://github.com/xiaoxm/HHXibHelper/
What?
一款Xib輔助工具雷逆,可以在對應.m文件中批量生成property屬性且自動完成連線操作其做。
How?
- .xib文件:界面控件布局完成后腊凶,將需要生成property的控件
重命名
,作為propertyname
谚鄙; - Xcode菜單欄找到
Windwo -> HHXibHelper
單擊,調(diào)出HHXibHelper; - 將.m或.xib文件拖入filePath文本框內(nèi)誊涯,獲取文件路徑洪鸭;
- 單擊
走你~
样刷,即可生成property屬性并自動完成連線操作;

Verson
v1.0
實現(xiàn)原理篇
.xib文件右鍵 Open As -> Source Code 打開源代碼觀察览爵,摘錄部分代碼如下:
...
<subviews>
<imageView userInteractionEnabled="NO" contentMode="scaleToFill" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="hFI-g2-bIc" userLabel="iconIV">
<rect key="frame" x="15" y="17" width="82" height="82"/>
</imageView>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="erf-m2-Vxb" userLabel="titleL">
<rect key="frame" x="118" y="17" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" text="Label" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vkQ-Uo-8fK" userLabel="descL">
<rect key="frame" x="118" y="58" width="42" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" red="0.0" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="6cH-um-Beo" userLabel="btn">
<rect key="frame" x="256" y="43" width="46" height="30"/>
<state key="normal" title="Button"/>
</button>
</subviews>
...
<connections>
<outlet property="btn" destination="6cH-um-Beo" id="enz-ri-1Dn"/>
<outlet property="descL" destination="vkQ-Uo-8fK" id="BXc-sN-Ex9"/>
<outlet property="iconIV" destination="hFI-g2-bIc" id="L5f-1D-Ivo"/>
<outlet property="titleL" destination="erf-m2-Vxb" id="baV-5g-sJE"/>
</connections>
...
在無數(shù)次的拖拽與觀察中做出如下假設:
-
<subviews>
標簽下置鼻,每一個<imageView>
、<label>
標簽對應著一個子控件蜓竹。如果有過重命名操作箕母,會生成一個userLabel屬性; -
<connections>
標簽下俱济,每一個<outlet>
標簽對應一條連線嘶是; -
<outlet>
的property屬性對應.m文件連線property name; -
<outlet>
的destination屬性對應連線子控件的id蛛碌;
這樣一來Xcode的拖線操作就可以這么理解:
- 在.h或者.m文件中生成了一條IBOutlet property屬性聂喇;
- 在.xib文件中的
<connections>
標簽下生成了一條outlet記錄;
于是乎,就有了HHXibHelper蔚携,它做如下操作:
- 獲取文件路徑希太;
- 打開.xib文件,找出存在userLabel屬性的控件酝蜒,然后生成outlet記錄;
- 打開.m文件跛十,生成對應IBOutlet property屬性;
- 實現(xiàn)批量連線秕硝。