2013-07-24 52 views
1

我發現之前搜索了很多,並嘗試了很多解決方案,它沒有工作,也許在我的代碼有問題。它基本上是一個骰子應用程序,當我按下滾動按鈕時,它應該在視圖中爲圖像添加動畫,但問題出現在NSTimer中,它應該使用arc4random()的一個NSNumber參數調用下一個方法。它顯示了選擇無法識別的選擇器發送到實例在NSTimer scheduledTimerWithTimeInterval

` 
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秒),那麼應用程序將停止,並且當我認爲它將參數傳遞給showDie時,出現「無法識別的選擇器發送到實例」問題。也許我不明白如何解決它(我是Mac世界和Objective-C的新手)。請幫助。

錯誤消息:

2013年7月25日02:13:40.550測試[3166:C07] - [視圖控制器showDie:]:無法識別的選擇發送到實例0x8827830

2013年7月25日02:13:40.551測試[3166:C07] *終止應用程序由於未捕獲的異常 'NSInvalidArgumentException',原因: ' - [視圖控制器showDie:]:無法識別的選擇發送到實例0x8827830'

*第一擲調用堆棧: ( 0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0xb192c0 0x1c52376 0x1c51e06 0x1c39a82 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x252d 0x2455爲0x1) 的libC++ abi.dylib:終止叫做拋出異常

+0

郵政實際,完整的錯誤消息。 – rdelmar

+0

@rdelmar我剛發佈了它 – user2615908

+0

嗯......這很奇怪,它看起來像你有一個showDie:方法。您是否在ViewController.m中發佈了所有代碼? – rdelmar

回答

-1

用戶信息:的NSTimer的參數是給你提供一個字典對象。您行:

NSNumber *userInfo = num.userInfo; 

這將失敗,因爲對象num是一個計時器,和用戶信息對象實際上是一個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... 
+0

謝謝我會嘗試並給你反饋 – user2615908

+0

它沒有工作。它給了我同樣的錯誤。 – user2615908

+0

「NSTimer的userInfo:參數用於提供字典對象」 - 不是true,它的類型爲id,因此您可以傳遞任何對象。我的錯誤是 – rdelmar

相關問題