我对Objective-C相当新,我在autoReleasepool块中获得了一个Thread 1:signal SIGBRT。
这是我的代码:我只在这两个文件中添加了代码。我还将3张图像导入支持文件夹。
ViewController.m
//
// ViewController.m
// TicTacToe
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
oImg = [UIImage imageNamed:@"o.png"];
xImg = [UIImage imageNamed:@"x.png"];
// set the player to 1
playerToken = 1;
// update the label
whoseTurn.text =@"X will go first!";
}
- (void) updatePlayerInfo{
if(playerToken == 1) {
playerToken = 2;
whoseTurn.text = @"Its O's turn";
NSLog(@"playerToken = %d", playerToken);
}
else if(playerToken == 2) {
playerToken = 1;
whoseTurn.text =@"Its X's turn";
NSLog(@"playerToken = %d", playerToken);
}
}
-(void) resetBoard{
/// clear the images stored in the UIIMageView
b1.image = NULL;
b2.image = NULL;
b3.image = NULL;
b4.image = NULL;
b5.image = NULL;
b6.image = NULL;
b7.image = NULL;
b8.image = NULL;
b9.image = NULL;
// reset the player and update the label text
playerToken= 1;
whoseTurn.text = @"X goes first";
}
- (IBAction)buttonReset:(UIButton *)sender {
[self resetBoard];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)touchesBegan:(NSSet *)touches
withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
if(CGRectContainsPoint([b1 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b1.image = xImg; }
if(playerToken==2){ b1.image = oImg; }
}
if(CGRectContainsPoint([b2 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b2.image = xImg; }
if(playerToken==2){ b2.image = oImg; }
}
if(CGRectContainsPoint([b3 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b3.image = xImg; }
if(playerToken==2){ b3.image = oImg; }
}
if(CGRectContainsPoint([b4 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b4.image = xImg; }
if(playerToken==2){ b4.image = oImg; }
}
if(CGRectContainsPoint([b5 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b5.image = xImg; }
if(playerToken==2){ b5.image = oImg; }
}
if(CGRectContainsPoint([b6 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b6.image = xImg; }
if(playerToken==2){ b6.image = oImg; }
}
if(CGRectContainsPoint([b7 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b7.image = xImg; }
if(playerToken==2){ b7.image = oImg; }
}
if(CGRectContainsPoint([b8 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b8.image = xImg; }
if(playerToken==2){ b8.image = oImg; }
}
if(CGRectContainsPoint([b9 frame], [touch
locationInView:self.view])){
if(playerToken==1){ b9.image = xImg; }
if(playerToken==2){ b9.image = oImg; }
}
[self updatePlayerInfo];
}
@end
ViewController.h
//
// ViewController.h
// TicTacToe
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
// the X or O images
IBOutlet UIImage * oImg;
IBOutlet UIImage * xImg;
NSInteger playerToken;
IBOutlet UIImageView*board;
IBOutlet UILabel *whoseTurn;
IBOutlet UIButton *resetButton;
//UIImages X's and O's
IBOutlet UIImageView *b1;
IBOutlet UIImageView *b2;
IBOutlet UIImageView *b3;
IBOutlet UIImageView *b4;
IBOutlet UIImageView *b5;
IBOutlet UIImageView *b6;
IBOutlet UIImageView *b7;
IBOutlet UIImageView *b8;
IBOutlet UIImageView *b9;
}
@end
答案 0 :(得分:1)
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<ViewController 0x109356bd0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key sd.' ***
这通常意味着您曾经删除了文件中的IBOutlet
,但是您忘记在Interface Builder中删除xib或storyboard中的连接。寻找一个不应该存在的名为sd
的属性。