我有一个使用Interface Builder构建的应用程序不,我需要添加一个新的弹出视图,让用户做出以下三种选择之一:攻击,防御或返回上一个屏幕。我以为我会尝试使用Interface Builder来创建视图,构建相关的方法,并轻松地将它们链接在一起,但我遗憾地错误。
我已经看到了这个网站上有关使用Interface Builder的所有抱怨,但是我希望我犯了一些简单的错误,因为他们自己找到了结果,所以没有人打扰报告。
这是Information Builder生成的视图的XML:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</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">
<rect key="frame" x="0.0" y="0.0" width="568" height="320"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dCs-dd-YuE">
<rect key="frame" x="0.0" y="165" width="568" height="54"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="32"/>
<size key="titleShadowOffset" width="1" height="0.0"/>
<state key="normal" title="Paint My Opponent">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="btnAttack" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="BBO-8w-J5r">
<rect key="frame" x="0.0" y="70" width="568" height="54"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="32"/>
<size key="titleShadowOffset" width="1" height="0.0"/>
<state key="normal" title="Defend My School">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="btnDefend" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="huu-90-yBz">
<rect key="frame" x="513" y="270" width="35" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="15"/>
<state key="normal" title="Back">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="boolean" keyPath="btnBack" value="YES"/>
</userDefinedRuntimeAttributes>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="string" keyPath="keyPath" value="AttackOrDefendViewController"/>
</userDefinedRuntimeAttributes>
</view>
</objects>
</document>
这是我为“AttackOrDefendViewController”类创建的头文件:
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <UIKit/UIKit.h>
@interface AttackOrDefendViewController : UIViewController
{
}
// the player has chosen to attack...
- ( IBAction ) btnAttack: ( id ) sender;
// the player has chosen to defend...
- ( IBAction ) btnDefend: ( id ) sender;
// go back to the previous screen...
- ( IBAction ) btnBack : ( id ) sender;
@end // end @interface AttackOrDefendViewController
这是该类的实现:
#import "AttackOrDefendViewController.h"
@implementation AttackOrDefendViewController
// additional setup while loading the view from the nib ("xib" file)...
- ( id ) initWithNibName: ( NSString * ) nibNameOrNil
bundle: ( NSBundle * ) nibBundleOrNil
{
// always call the parent method...
self = [ super initWithNibName: nibNameOrNil
bundle: nibBundleOrNil
];
// idiot check...
NSAssert( ( self )
, @"error allocating object"
);
return self;
} // end AttackOrDefendViewController::initWithNibName
// additional setup after loading the view from the nib ("xib" file)...
- ( void ) viewDidLoad
{
// always call the parent method...
[ super viewDidLoad ];
return;
} // end AttackOrDefendViewController::viewDidLoad
// clean-up method...
- ( void ) dealloc
{
// remember to call the parent "dealloc" method...
[ super dealloc ];
return;
} // end AttackOrDefendViewController::dealloc
// the player has chosen to attack...
- ( IBAction ) btnAttack: ( id ) sender
{
return;
} // end AttackOrDefendViewController::btnAttack
// the player has chosen to defend...
- ( IBAction ) btnDefend: ( id ) sender
{
return;
} // end AttackOrDefendViewController::btnDefend
// go back to the previous screen...
- ( IBAction ) btnBack : ( id ) sender
{
return;
} // end AttackOrDefendViewController::btnBack
@end // end @implementation AttackOrDefendViewController
我遇到的问题是,根据我在StackOverflow上阅读的所有内容,应该有一种简单的方法将视图中的对象拖动连接到适当的方法,但没有显示任何内容当我控制时 - 点击指示的单选按钮,特别是“触摸内部”按钮。我已经尝试将“文件所有者”选项链接到该类,但同样没有任何显示。
答案 0 :(得分:0)
我发现了这个问题。当您想在最初使用它构建的应用程序中使用Interface Builder时,XCode的表现并不好。我需要手动编辑&#34; xib&#34; file使用&#34; customClass&#34;提供视图对象和代码之间的链接。 &#34;占位符&#34;中的字段标签
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AttackOrDefendViewController">
在我这样做之后,我能够将相应的按钮与相应的方法相关联。
新的&#34; xib&#34;文件现在看起来像这样:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES">
<dependencies>
<deployment defaultVersion="1536" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="AttackOrDefendViewController">
<connections>
<outlet property="btnAttack" destination="dCs-dd-YuE" id="SWe-vI-ryH"/>
<outlet property="btnBack" destination="huu-90-yBz" id="Pca-Y8-pLI"/>
<outlet property="btnDefend" destination="BBO-8w-J5r" id="1IN-ev-lSD"/>
<outlet property="view" destination="iN0-l3-epB" id="cVd-3t-nU0"/>
</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="568" height="320"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dCs-dd-YuE">
<rect key="frame" x="0.0" y="165" width="568" height="54"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="32"/>
<size key="titleShadowOffset" width="1" height="0.0"/>
<state key="normal" title="Paint My Opponent">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnAttack:" destination="-1" eventType="touchUpInside" id="TnY-5r-xlK"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="BBO-8w-J5r">
<rect key="frame" x="0.0" y="70" width="568" height="54"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="32"/>
<size key="titleShadowOffset" width="1" height="0.0"/>
<state key="normal" title="Defend My School">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnDefend:" destination="-1" eventType="touchUpInside" id="vgx-KL-FM0"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="huu-90-yBz">
<rect key="frame" x="513" y="270" width="35" height="30"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<fontDescription key="fontDescription" name="MarkerFelt-Wide" family="Marker Felt" pointSize="15"/>
<state key="normal" title="Back">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="btnBack:" destination="-1" eventType="touchUpInside" id="fl1-IY-UXH"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
<simulatedOrientationMetrics key="simulatedOrientationMetrics" orientation="landscapeRight"/>
<simulatedScreenMetrics key="simulatedDestinationMetrics" type="retina4"/>
</view>
</objects>
</document>
新的&#34; .h&#34;文件现在看起来像这样:
//
// AttackOrDefendViewController.h
//
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import <UIKit/UIKit.h>
@interface AttackOrDefendViewController : UIViewController
{
}
@property ( retain, nonatomic ) IBOutlet UIView * viewMain;
@property ( retain, nonatomic ) IBOutlet UIButton * btnAttack;
@property ( retain, nonatomic ) IBOutlet UIButton * btnDefend;
@property ( retain, nonatomic ) IBOutlet UIButton * btnBack;
// create the view/dialog and move to the main menu screen when completed...
+ ( void ) PromptForAttackOrDefend;
// the player has chosen to attack...
- ( IBAction ) btnAttack: ( id ) sender;
// the player has chosen to defend...
- ( IBAction ) btnDefend: ( id ) sender;
// go back to the previous screen...
- ( IBAction ) btnBack : ( id ) sender;
@end // end @interface AttackOrDefendViewController
感谢。