gpt4 book ai didi

ios - 在 completionblock 中无法更改 UILabel 文本

转载 作者:行者123 更新时间:2023-11-29 01:03:28 26 4
gpt4 key购买 nike

在调用performpost之前,notifyDownloadInfo可以工作,但在completionHandler中调用setTextForDownloadInfo,但标签未更新。

performpost 是一个异步函数,通过 AFNetworking 发出请求。

A类.m

       +(void)downloadAinformation{
[self performPost:....... completionHandler:^() {

[ClassUtil notifyDownloadInfo:@"string"];
});
}
+(void)notifyDownloadInfo:(NSString*)str{

NSDictionary* userInfo = @{@"text": str};
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDownloadInfoText object:self userInfo:userInfo];

}

B类.m

    - (void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];


[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(setTextForDownloadInfo:)
name:kNotificationDownloadInfoText
object:nil];
[self downloadData];
}

- (void)setTextForDownloadInfo:(NSNotification*)notification{
NSString *str = notification.userInfo.allValues.firstObject;
dispatch_async(dispatch_get_main_queue(), ^{
self.downloadInfoLabel.text = str;
});
NSLog(@"%@",str);
}

-(void)downloadData{
[Bclass downloadAinformation];
}

我尝试将 setTextForDownloadInfo 放入其中,但它没有进入其中:

dispatch_async(dispatch_get_main_queue(), ^{

我还performSelectorOnMainthread并且选择器方法没有被调用。

收到函数的响应后,如何更改完成 block 中的文本?谢谢。

最佳答案

@e.ozmen,

self.downloadInfoLabel.text = str; 

有用吗?

如果是这样,也许 IB 中没有连接 downloadInfoLabel?

如果不是,可能通知发布有问题,尝试改成

[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationDownloadInfoText 
object:nil
userInfo:userInfo];

其中参数 'object' 为 nil。

希望对您有所帮助:)

关于ios - 在 completionblock 中无法更改 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718879/

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