簡體   English   中英

`無法識別的選擇器已發送至實例`

[英]`Unrecognized selector sent to instance`

在我的應用中,我有一個在屏幕上滾動的動畫。 我的問題是,當我使用@selector調用動畫時,線程崩潰。

如果我使用相同的@selector來調用頁面中的另一個函數,則它可以完美工作,但是在調用此函數時,它似乎不起作用。 如果我將代碼放在viewDidLoad部分中,則可以正常工作。

我嘗試了很多這樣的鏈接,以將unrecognized selector sent to instance Stackoverflow中的unrecognized selector sent to instance ,但沒有任何幫助。 我還嘗試了- (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) 
- (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];
});

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

- (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];

因此,理想情況下,您應該使用上述方法進行兩個參數解析,或者將要發送的數據封裝到單個Objective C對象(例如,NSArray,NSDictionary,某些自定義Objective 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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM