这是我的第一篇文章! :d
我昨天开始了一个新的Xcode项目,用于iPhone的基于文本的游戏。我刚开始在3月左右开始使用Objective-C,我休息了一段时间。我有一个故事板的GUI,显示叙述文本,然后是下面的四个按钮,旁边有选项。
在为不同场景编写了几个代码后,我尝试在iOS模拟器上运行该应用程序。当我试图运行它时,只出现黑屏,没有别的。
这是我的ViewController.h文件:
#import <UIKit/UIKit.h>
// 1 - Initializing the view controller
@interface BKViewController : UIViewController
@property int sit;
// 2 - The narrarator text
@property (strong, nonatomic) IBOutlet UILabel *narText;
// 3 - Buttons
- (IBAction)buttonAPressed:(UIButton *)sender;
- (IBAction)buttonBPressed:(UIButton *)sender;
- (IBAction)buttonCPressed:(UIButton *)sender;
- (IBAction)buttonDPressed:(UIButton *)sender;
// 4 - Option labels
@property (strong, nonatomic) IBOutlet UILabel *optionA;
@property (strong, nonatomic) IBOutlet UILabel *optionB;
@property (strong, nonatomic) IBOutlet UILabel *optionC;
@property (strong, nonatomic) IBOutlet UILabel *optionD;
@end
这是我的ViewController.m文件:
@interface BKViewController ()
@end
@implementation BKViewController
- (void)viewDidLoad
{
[super viewDidLoad];
//Starting attributes
_sit = 0;
while (_sit == 0) {
self.narText.text = @"Welcome to Adventure Game! Choose an option by tapping a blue button!";
self.optionA.text = @"What is this?";
self.optionB.text = @"Let's get started!";
self.optionC.text = @"What's next?";
self.optionD.text = @"More info!";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)buttonAPressed:(UIButton *)first sender:(id)sender {
// Situation 1
if (_sit == 0) {
_sit = 1;
}
// Situation 2
if (_sit == 1) {
_sit = 2;
}
// Situation 3
if (_sit == 2) {
_sit = 3;
}
// Situation 4
if (_sit == 3) {
_sit = 4;
}
// Situation 5
if (_sit == 4) {
_sit = 6;
}
// Situation 6
if (_sit == 5) {
_sit = 2;
}
}
- (IBAction)buttonBPressed:(UIButton *)first sender:(UIButton *)sender {
// Situation 1
if (_sit == 0) {
_sit = 2;
}
// Situation 2
if (_sit == 1) {
_sit = 5;
}
// Situation 3
if (_sit == 3) {
_sit = 13;
}
// Situation 4
if (_sit == 4) {
_sit = 7;
}
// Situation 5
if (_sit == 5) {
_sit = 0;
}
}
- (IBAction)buttonCPressed:(UIButton *)first sender:(UIButton *)sender {
// Situation 1
if (_sit == 0) {
_sit = 8;
}
// Situation 2
if (_sit == 1) {
_sit = 0;
}
// Situation 3
if (_sit == 3) {
_sit = 9;
}
// Situation 4
if (_sit == 4) {
_sit = 10;
}
}
- (IBAction)buttonDPressed:(UIButton *)first sender:(UIButton *)sender {
// Situation 1
if (_sit == 0) {
_sit = 11;
}
// Situation 2
if (_sit == 1) {
_sit = 5;
}
// Situation 3
if (_sit == 3) {
_sit = 12;
}
}
@end
这是最新的崩溃日志:
2014-08-07 05:45:18.007 Adventure Game[5942:60b] -[BKViewController aButtonPressed:]: unrecognized selector sent to instance 0x8c80850
2014-08-07 05:45:18.032 Adventure Game[5942:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BKViewController aButtonPressed:]: unrecognized selector sent to instance 0x8c80850'
*** First throw call stack:
(
0 CoreFoundation 0x017ee1e4 __exceptionPreprocess + 180
1 libobjc.A.dylib 0x0156d8e5 objc_exception_throw + 44
2 CoreFoundation 0x0188b243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3 CoreFoundation 0x017de50b ___forwarding___ + 1019
4 CoreFoundation 0x017de0ee _CF_forwarding_prep_0 + 14
5 libobjc.A.dylib 0x0157f880 -[NSObject performSelector:withObject:withObject:] + 77
6 UIKit 0x0022f3b9 -[UIApplication sendAction:to:from:forEvent:] + 108
7 UIKit 0x0022f345 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 61
8 UIKit 0x00330bd1 -[UIControl sendAction:to:forEvent:] + 66
9 UIKit 0x00330fc6 -[UIControl _sendActionsForEvents:withEvent:] + 577
10 UIKit 0x00330243 -[UIControl touchesEnded:withEvent:] + 641
11 UIKit 0x0026eddd -[UIWindow _sendTouchesForEvent:] + 852
12 UIKit 0x0026f9d1 -[UIWindow sendEvent:] + 1117
13 UIKit 0x002415f2 -[UIApplication sendEvent:] + 242
14 UIKit 0x0022b353 _UIApplicationHandleEventQueue + 11455
15 CoreFoundation 0x0177777f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
16 CoreFoundation 0x0177710b __CFRunLoopDoSources0 + 235
17 CoreFoundation 0x017941ae __CFRunLoopRun + 910
18 CoreFoundation 0x017939d3 CFRunLoopRunSpecific + 467
19 CoreFoundation 0x017937eb CFRunLoopRunInMode + 123
20 GraphicsServices 0x037e25ee GSEventRunModal + 192
21 GraphicsServices 0x037e242b GSEventRun + 104
22 UIKit 0x0022df9b UIApplicationMain + 1225
23 Adventure Game 0x00003e1d main + 141
24 libdyld.dylib 0x01e35701 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
答案 0 :(得分:1)
2014-08-07 05:45:18.007 Adventure Game[5942:60b] -[BKViewController aButtonPressed:]:
您的崩溃报告显示BKViewController引用了aButtonPressed,但在您的代码中没有这样的按钮。检查是否已使用aButtonPressed为按钮创建了IBAction,如果是,则将其删除。还要使用iOS模拟器重置模拟器&gt;重置内容和设置选项。
答案 1 :(得分:0)
您的应用崩溃了,因为您添加了名为“aButtonPressed”的按钮操作并将其连接到故事板中,但看起来您将其重命名为buttonAPressed:(UIButton *)first sender:(id)sender
但未在故事板中更新(您应该使用refactor-&gt;为此重命名,通常xcode重命名故事板中的选择器)。因此,您尝试在目标(控制器)上调用选择器,但控制器不知道如何响应它,默认行为是引发异常。
@codeIgnitor提到了你应该注意的这种情况。