Как заставить работать PerformSelector:withObject:afterDelay?

Вот код:

-(void)setProjectID:(NSString *)newProject {
    [self willChangeValueForKey:@"projectID"];
    [projectID release];
    projectID = [newProject copy];
    [self didChangeValueForKey:@"projectID"];

    // Since we have an ID, now we need to load it
    NSInvocation *returnInvocation = [NSInvocation invocationWithMethodSignature:
                                      [Detail instanceMethodSignatureForSelector:@selector(configureView:)]];
    [returnInvocation setTarget:self];
    [returnInvocation performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];
    [returnInvocation setSelector:@selector(configureView:)];
    [returnInvocation retainArguments];

    fetch = [[WBWDocumentFetcher alloc] init];
    [fetch retrieveDocument:[NSURL wb_URLForTabType:PROJECT_DETAILS inProject:projectID] returnBy:returnInvocation];
}

-(void)displayAlert
{
    UIAlertView * alert = [[UIAlertView alloc]
                           initWithTitle:@"Connection Error" 
                           message:@"Error loading Data."
                           delegate:self cancelButtonTitle:@"OK" 
                           otherButtonTitles:nil];
    [alert show];
    [alert release];
}

Приложение вылетает с сообщением NSInvalidArguementException. -[NSInvocation displayAlert]: нераспознанный селектор отправлен экземпляру 0x5842320 Пожалуйста, помогите!!!


person Ashutosh    schedule 18.07.2011    source источник


Ответы (4)


Не используйте withObject, просто используйте PerformSelector:afterDelay:

Кроме того, вы должны вызывать это на self, а не на returnInvocation

person PengOne    schedule 18.07.2011
comment
все еще происходит сбой, а также выдается предупреждение. NSInvocation может не отвечать слишком. - person Ashutosh; 18.07.2011

Я думаю, код должен быть таким:

NSInvocation *returnInvocation = [NSInvocation invocationWithMethodSignature:
                                  [Detail instanceMethodSignatureForSelector:@selector(displayAlert)]];

[returnInvocation setTarget:self];
[returnInvocation setSelector:@selector(displayAlert)];
[returnInvocation invoke];

или просто:

[self performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];
person Deniz Mert Edincik    schedule 18.07.2011

[self performSelector:@selector(displayAlert) withObject: message afterDelay:0.5];

Попробуй это..

person Rakesh Bhatt    schedule 18.07.2011

- (void)performSelector:(SEL)aSelector withObject:(id)anArgument afterDelay:(NSTimeInterval)delay выполняется для объекта, который его вызывает.

Поэтому, если вы вызовете его на returnInvocation, вы получите ошибку нераспознанного селектора, потому что NSInvocation не имеет метода displayAlert.

Использовать

[self performSelector:@selector(displayAlert) withObject:nil afterDelay:0.5];

Поскольку у «я» есть метод.

person Warren Burton    schedule 18.07.2011