gpt4 book ai didi

iOS objective-c 执行选择器不执行该方法

转载 作者:行者123 更新时间:2023-11-29 11:45:14 25 4
gpt4 key购买 nike

嘿,实际上我在 url 请求 block 中使用了 perform selector,一开始它工作正常,但现在它没有执行该功能

执行选择器函数是

       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 并且应用程序停留在同一页面

最佳答案

很简单,在主线程中更新你的用户界面

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


}

关于iOS objective-c 执行选择器不执行该方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44320734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com