xcode iOS目标c执行选择器未执行该方法

bd1hkmkf  于 2022-12-05  发布在  iOS
关注(0)|答案(4)|浏览(126)

嘿,实际上我在一个url请求块中使用了执行选择器,一开始它工作得很好,但是现在它不执行这个函数了
所执行选择器功能

NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
        NSLog(@"requestReply: %@", jsonDict);
        self.tmp=[jsonDict valueForKey:@"otp"] ;
        self.str=self.tmp;
        NSLog(@"tmp storage inside block:%@",self.tmp);
      //  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateStatus) userInfo:nil repeats:YES];
        [self performSelector:@selector(updateStatus) withObject:self afterDelay:1.0];
    }] resume];
}

和更新状态的方法,

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
[ self performSegueWithIdentifier:@"b1" sender:self];
}

由于它没有执行,我无法执行segue,应用程序停留在同一页面

lawou6xi

lawou6xi1#

简单,在主线程中更新UI

dispatch_async(dispatch_get_main_queue(), ^{
        [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
         });

备选案文2

否则创建简单方法

NSLog(@"tmp storage inside block:%@",self.tmp);
        [self updateStatus];

并在主线程中调用该方法

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
dispatch_async(dispatch_get_main_queue(), ^{
    [ self performSegueWithIdentifier:@"b1" sender:self];
});
}

已更新代码

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
       // NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
        //   NSError *error;
        NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
        NSLog(@"requestReply: %@", jsonDict);
        self.tmp=[jsonDict valueForKey:@"otp"] ;
        self.str=self.tmp;
        NSLog(@"tmp storage inside block:%@",self.tmp);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
        });

    }] resume];

并将方法调用为

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
[ self performSegueWithIdentifier:@"b1" sender:self];

}
8gsdolmq

8gsdolmq2#

如果从后台线程调用performSelector,它将不起作用。请尝试以下操作...

dispatch_async(dispatch_get_main_queue(), ^(void){
    //Run UI Updates
    [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
});
n53p2ov0

n53p2ov03#

[self performSelector:@selector(updateStatus) withObject:nil afterDelay:1.0];依赖于runloop,并且background threadrunloop默认不打开。
因此应该打开runloop或将该方法置于main thread

lmvvr0a8

lmvvr0a84#

你必须传入相应的对象来响应指定的选择器。传入nil会向nil发送一条消息,并且什么也不做。假设updateStatus方法在你调用performSelector的同一个类中声明,你可以这样做:

[self performSelector:@selector(updateStatus) withObject:self afterDelay:0.1];

相关问题