我在发布此内容之前进行了大量搜索并尝试了许多解决方案但它没有用,可能我的代码中有问题。它基本上是一个骰子应用程序,当我按下滚动按钮时它应该为视图中的图像设置动画,但问题是在NSTimer中它应该使用NSNumber参数调用下一个方法,该参数是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`
showDie
-(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];}
randDie
-(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秒)然后应用程序停止,并且当-i认为它将参数传递给showDie时,会出现“无法识别的选择器发送到实例”问题。也许我不明白如何解决它(我是Mac世界和客观C的新手)。请帮助。
错误消息:
2013-07-25 02:13:40.550测试[3166:c07] - [ViewController showDie:]:无法识别的选择器发送到实例0x8827830
2013-07-25 02:13:40.551 test [3166:c07] * 由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:' - [ViewController showDie:]:无法识别的选择器已发送例如0x8827830'
* 第一次抛出调用堆栈: (0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0xb192c0 0x1c52376 0x1c51e06 0x1c39a82 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x252d 0x2455 0x1) libc ++ abi.dylib:terminate调用抛出异常
答案 0 :(得分:-1)
NSTimer的userInfo:参数用于提供字典对象。你的行:
NSNumber *userInfo = num.userInfo;
这将失败,因为对象num
是一个计时器,而userInfo对象实际上是一个NSDictionary。您可能在此对象上调用intValue
时出现选择器错误。
正确的做法是首先将NSNumber折叠成NSDictionary对象:
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:roll1,@"Roll1Key",nil];
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showDie:) userInfo:info repeats:NO];
//in showDie
NSDictionary *info = num.userInfo;
NSNumber *roll1 = [info objectForKey:@"Roll1Key"];
NSInteger rollInteger = [roll1 intValue];
//continue...