在发布此之前我进行了很多搜索并尝试了许多解决方案但它没有工作,也许我的代码有问题。它基本上是一个骰子应用程序,当我按下滚动按钮时,它应该为视图中的图像设置动画,但问题出在 NSTimer 中,它应该使用 NSNumber 参数调用 next 方法,这是 arc4random() 的结果它显示了所选骰子的真实图像
`
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize firstDie;
@synthesize secondDie;
@synthesize animation;
- (IBAction)rollButtonClick:(id)sender {
NSNumber* roll1 =[[NSNumber alloc] initWithInt:[self.model getDiceRoll]];
NSNumber* roll2 =[[NSNumber alloc] initWithInt:[self.model getDiceRoll]];
[self.firstDie randDie];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:roll1,@"Roll1Key",nil];
NSArray *info = [[NSArray alloc] initWithObjects:roll1, nil];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showDie:) userInfo:info repeats:NO];
NSString *sum = [[NSString alloc] initWithFormat: @"Sum = %d",[roll1 intValue]+[roll2 intValue]];
self.sumLabel.text = sum;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.model = [[Model alloc]init];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end`
秀模
-(void) showDie:(NSTimer*)num
{
if (self.dieImage == nil)
{
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
NSNumber *userInfo = num.userInfo;
NSString *filename = [[NSString alloc] initWithFormat:@"dice%d.png", [userInfo intValue]];
self.dieImage.image = [UIImage imageNamed:filename];}
兰迪
-(void) randDie
{
if (self.dieImage == nil)
{
self.dieImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 90, 90)];
[self addSubview:self.dieImage];
}
UIImage *dieOne = [UIImage imageNamed:@"dice1.png"];
UIImage *dieTwo = [UIImage imageNamed:@"dice2.png"];
UIImage *dieThree = [UIImage imageNamed:@"dice3.png"];
UIImage *dieFour = [UIImage imageNamed:@"dice4.png"];
UIImage *dieFive = [UIImage imageNamed:@"dice5.png"];
UIImage *dieSix = [UIImage imageNamed:@"dice6.png"];
NSArray *animate = [NSArray arrayWithObjects: dieFive,dieThree,dieOne,dieFour,dieSix,dieTwo, nil];
self.dieImage.image = [UIImage animatedImageWithImages:animate duration:0.8];}
该应用程序正常工作并且firstDie中的图像不断变化(我想是2秒)然后应用程序停止并且当-我认为-它将参数传递给showDie时出现“无法识别的选择器发送到实例”问题。也许我不明白如何解决它(我是 Mac 世界和 Objective-C 的新手)。请帮忙。
错误信息:
2013-07-25 02:13:40.550 测试[3166:c07]-[ViewController showDie:]:无法识别的选择器发送到实例 0x8827830
2013-07-25 02:13:40.551 测试 [3166:c07] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ViewController showDie:]:无法识别的选择器发送到实例 0x8827830”
* First throw call stack: (0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0xb192c0 0x1c52376 0x1c51e06 0x1c39a82 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x252d 0x2455 0x1) libc++abi.dylib: terminate called throwing an exception