把xib文件转换成objective-C代码

如何将XIB文件转换为Object-C源程序呢?

使用nib2objc这么一个小工具就可以了,该工具的作者是akosma,你可以通过https://github.com/akosma/nib2objc下载到这个工具的源代码。

下载的工程代码并用XCode编译,生成一个命令行程序文件nib2objc。然后执行如下命令开始转换。

nib2objc 1.xib > 1.m

如果1.xib的内容如下:

<!--StartFragment -->
<?xmlversion="1.0"encoding="UTF-8"?>
<archivetype="com.apple.InterfaceBuilder3.CocoaTouch.XIB"version="7.10">
<data>
<intkey="IBDocument.SystemTarget">800</int>
<stringkey="IBDocument.SystemVersion">10C540</string>
<stringkey="IBDocument.InterfaceBuilderVersion">759</string>
<stringkey="IBDocument.AppKitVersion">1038.25</string>
<stringkey="IBDocument.HIToolboxVersion">458.00</string>
<objectclass="NSMutableDictionary"key="IBDocument.PluginVersions">
<stringkey="NS.key.0">com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
<stringkey="NS.object.0">77</string>
</object>
<objectclass="NSMutableArray"key="IBDocument.EditedObjectIDs">
<boolkey="EncodedWithXMLCoder">YES</bool>
<integervalue="6"/>
</object>
<objectclass="NSArray"key="IBDocument.PluginDependencies">
<boolkey="EncodedWithXMLCoder">YES</bool>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
<objectclass="NSMutableDictionary"key="IBDocument.Metadata">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="NSArray"key="dict.sortedKeys"id="0">
<boolkey="EncodedWithXMLCoder">YES</bool>
</object>
<objectclass="NSMutableArray"key="dict.values">
<boolkey="EncodedWithXMLCoder">YES</bool>
</object>
</object>
<objectclass="NSMutableArray"key="IBDocument.RootObjects"id="1000">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="IBProxyObject"id="372490531">
<stringkey="IBProxiedObjectIdentifier">IBFilesOwner</string>
<stringkey="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
<objectclass="IBProxyObject"id="843779117">
<stringkey="IBProxiedObjectIdentifier">IBFirstResponder</string>
<stringkey="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
<objectclass="IBUIView"id="774585933">
<referencekey="NSNextResponder"/>
<intkey="NSvFlags">274</int>
<stringkey="NSFrameSize">{320,460}</string>
<referencekey="NSSuperview"/>
<objectclass="NSColor"key="IBUIBackgroundColor">
<intkey="NSColorSpace">3</int>
<byteskey="NSWhite">MC43NQA</bytes>
<objectclass="NSColorSpace"key="NSCustomColorSpace">
<intkey="NSID">2</int>
</object>
</object>
<boolkey="IBUIClearsContextBeforeDrawing">NO</bool>
<objectclass="IBUISimulatedStatusBarMetrics"key="IBUISimulatedStatusBarMetrics"/>
<stringkey="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
</object>
</object>
<objectclass="IBObjectContainer"key="IBDocument.Objects">
<objectclass="NSMutableArray"key="connectionRecords">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="IBConnectionRecord">
<objectclass="IBCocoaTouchOutletConnection"key="connection">
<stringkey="label">view</string>
<referencekey="source"ref="372490531"/>
<referencekey="destination"ref="774585933"/>
</object>
<intkey="connectionID">7</int>
</object>
</object>
<objectclass="IBMutableOrderedSet"key="objectRecords">
<objectclass="NSArray"key="orderedObjects">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="IBObjectRecord">
<intkey="objectID">0</int>
<referencekey="object"ref="0"/>
<referencekey="children"ref="1000"/>
<nilkey="parent"/>
</object>
<objectclass="IBObjectRecord">
<intkey="objectID">-1</int>
<referencekey="object"ref="372490531"/>
<referencekey="parent"ref="0"/>
<stringkey="objectName">File'sOwner</string>
</object>
<objectclass="IBObjectRecord">
<intkey="objectID">-2</int>
<referencekey="object"ref="843779117"/>
<referencekey="parent"ref="0"/>
</object>
<objectclass="IBObjectRecord">
<intkey="objectID">6</int>
<referencekey="object"ref="774585933"/>
<referencekey="parent"ref="0"/>
</object>
</object>
</object>
<objectclass="NSMutableDictionary"key="flattenedProperties">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="NSArray"key="dict.sortedKeys">
<boolkey="EncodedWithXMLCoder">YES</bool>
<string>-1.CustomClassName</string>
<string>-2.CustomClassName</string>
<string>6.IBEditorWindowLastContentRect</string>
<string>6.IBPluginDependency</string>
</object>
<objectclass="NSMutableArray"key="dict.values">
<boolkey="EncodedWithXMLCoder">YES</bool>
<string>UntitledViewController</string>
<string>UIResponder</string>
<string>{{239,654},{320,480}}</string>
<string>com.apple.InterfaceBuilder.IBCocoaTouchPlugin</string>
</object>
</object>
<objectclass="NSMutableDictionary"key="unlocalizedProperties">
<boolkey="EncodedWithXMLCoder">YES</bool>
<referencekey="dict.sortedKeys"ref="0"/>
<objectclass="NSMutableArray"key="dict.values">
<boolkey="EncodedWithXMLCoder">YES</bool>
</object>
</object>
<nilkey="activeLocalization"/>
<objectclass="NSMutableDictionary"key="localizations">
<boolkey="EncodedWithXMLCoder">YES</bool>
<referencekey="dict.sortedKeys"ref="0"/>
<objectclass="NSMutableArray"key="dict.values">
<boolkey="EncodedWithXMLCoder">YES</bool>
</object>
</object>
<nilkey="sourceID"/>
<intkey="maxID">7</int>
</object>
<objectclass="IBClassDescriber"key="IBDocument.Classes">
<objectclass="NSMutableArray"key="referencedPartialClassDescriptions">
<boolkey="EncodedWithXMLCoder">YES</bool>
<objectclass="IBPartialClassDescription">
<stringkey="className">UntitledViewController</string>
<stringkey="superclassName">UIViewController</string>
<objectclass="IBClassDescriptionSource"key="sourceIdentifier">
<stringkey="majorKey">IBProjectSource</string>
<stringkey="minorKey">Classes/UntitledViewController.h</string>
</object>
</object>
</object>
</object>
<intkey="IBDocument.localizationMode">0</int>
<stringkey="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<objectclass="NSMutableDictionary"key="IBDocument.PluginDeclaredDevelopmentDependencies">
<stringkey="NS.key.0">com.apple.InterfaceBuilder.CocoaTouchPlugin.InterfaceBuilder3</string>
<integervalue="3100"key="NS.object.0"/>
</object>
<boolkey="IBDocument.PluginDeclaredDependenciesTrackSystemTargetVersion">YES</bool>
<stringkey="IBDocument.LastKnownRelativeProjectPath">Untitled.xcodeproj</string>
<intkey="IBDocument.defaultPropertyAccessControl">3</int>
<stringkey="IBCocoaTouchPluginVersion">77</string>
<nilkey="IBCocoaTouchSimulationTargetRuntimeIdentifier"/>
</data>
</archive>
执行转换后的源代码如下:
UIView *view6 = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 460.0)];
view6.frame = CGRectMake(0.0, 0.0, 320.0, 460.0);
view6.alpha = 1.000;
view6.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
view6.backgroundColor = [UIColor colorWithWhite:0.750 alpha:1.000];
view6.clearsContextBeforeDrawing = NO;
view6.clipsToBounds = NO;
view6.contentMode = UIViewContentModeScaleToFill;
view6.hidden = NO;
view6.multipleTouchEnabled = NO;
view6.opaque = YES;
view6.tag = 0;
view6.userInteractionEnabled = YES;
从试验的结果看,只能转换VIEW(包括UIVIEW的子类)相关的东西,而且控件和其对应事件的联系丢失。
而且转换后所有的属性的设置都会生成相应的Objective-C代码,不论是否属性值本身是缺省的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值