2013-03-28 61 views
0

在我的應用程序中,我有一個可以在屏幕上滾動的動畫。我的問題是,當我使用@selector來調用我的動畫時,線程崩潰。`發送給實例的無法識別的選擇器'

如果我用同樣的@selector打電話給我的網頁另一個函數它完美的作品,但是當調用這個函數它似乎並沒有工作。如果我把我的代碼放在viewDidLoad部分,它可以正常工作。

我嘗試了不少這些鏈接爲unrecognized selector sent to instance在#1,但沒有任何幫助。我也試過- (void)imageSpawn代替- (void) imageSpawn:(id)sender withEvent:(UIEvent *)事件和選擇更改爲(imageSpawn),而不是`(ImageSpawn :)仍然沒有運氣....

- (void)viewDidLoad { 

    [self performSelector:@selector(imageSpawn:) withObject:nil afterDelay:3]; 
} 

- (void) imageSpawn:(id) sender withEvent:(UIEvent *) event 
{ 

    UIImage* image = [UIImage imageNamed:@"ae"]; 
    UIImageView *rocket = [[UIImageView alloc] initWithImage:image]; 
    rocket.frame = CGRectMake(-25, 200, 25, 40); 
    [UIView animateWithDuration:5 animations:^(){rocket.frame=CGRectMake(345, 200, 25, 40);} completion:^(BOOL finished){if (finished){ 

      //trigger an event. 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Tapped row!" 
                  message:[NSString stringWithFormat:@"Shot"] 
                  delegate:nil 
               cancelButtonTitle:@"Yes, I did!" 
               otherButtonTitles:nil]; 
      [alert show]; 
     } 
    }]; 
    [myScrollView addSubview:rocket]; 
} 



2013-03-28 10:14:31.661 shotplacementgiude001[16897:c07] -[SelectedCellViewController imageSpawn:]: unrecognized selector sent to instance 0xa159480 
2013-03-28 10:14:31.663 shotplacementgiude001[16897:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SelectedCellViewController imageSpawn:]: unrecognized selector sent to instance 0xa159480' 
*** First throw call stack: 
(0x16b4012 0x13c1e7e 0x173f4bd 0x16a3bbc 0x16a394e 0xdbf5b3 0x1673376 0x1672e06 0x165aa82 0x1659f44 0x1659e1b 0x27157e3 0x2715668 0x305ffc 0x2c3d 0x2b65 0x1) libc++abi.dylib: terminate called throwing an exception 
(lldb) 
+0

實際上@ peko的回答(現在刪除)非常正確,對於帶有2個參數的方法你不能使用'performSelector:withObject:afterDelay:'。 – 2013-03-28 09:11:10

回答

1
- (void)viewDidLoad 
{ 
    [self performSelector:@selector(imageSpawn:withEvent:) withObject:nil withObject:nil]; 
} 

你的方法認定中包含兩個參數- (void) imageSpawn:(id) sender withEvent:(UIEvent *) event,所以你需要在選擇器中使用兩個參數調用方法。

注意

如果你想傳遞兩個對象到選擇那麼另一種方法是可用的是performSelector:withObject:withObject:

,它發送的消息與兩個對象作爲參數的接收器。

[self performSelector:@selector(imageSpawn:withEvent:) withObject:senderObject withObject:eventObject]; 

所以理想情況下,應使用上述方法有兩個參數解析或封裝要一起發送到一些單個目標C對象中的數據(例如一個的NSArray,一個的NSDictionary,一些自定義目標C型),然後使其通過[NSObject performSelector:withObject:afterDelay:]

NSDictionary *dictionary = [[NSDictionary alloc] init]; 
[dictionary setObject:senderObject forKey:@"sender"]; 
[dictionary setObject:eventObject forKey:@"event"]; 
[self performSelector:@selector(imageSpawn:withEvent:) withObject:dictionary afterDelay:3.0]; 
+0

請注意,只應爲具有零個或一個參數的方法調用「performSelector:withObject:afterDelay:',所以此解決方案不太正確(未定義的行爲)。 – 2013-03-28 08:50:20

2
- (void) imageSpawn:(id) sender withEvent:(UIEvent *) event 

是具有2個參數的方法,以及其選擇是

@selector(imageSpawn:withEvent:) 

然而,

performSelector:withObject:afterDelay: 

必須只與具有零或一個參數方法中使用。所以,你可以通過

- (void) imageSpawn:(id) sender 

更換你的方法,並呼籲

[self performSelector:@selector(imageSpawn:) withObject:nil afterDelay:3]; 

或使用GCD方法:

double delayInSeconds = 3.0; 
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC)); 
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
    [self imageSpawn:nil withEvent:nil]; 
}); 

的優點是更好的參數和類型檢查。

相關問題