gpt4 book ai didi

ios - 显示 UI 元素,例如来自多个线程的警报

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:21:00 24 4
gpt4 key购买 nike

我有一个简单的应用程序,它运行良好,直到我添加了一些代码,这些代码在某个时候启动一个新线程,然后尝试显示来自该线程的警报。现在,只要点击显示警报的代码,应用就会崩溃。

UIAlertView * addAlert = [[UIAlertView alloc] initWithTitle:@"New alert"
message:@"Example alert"
delegate:nil
cancelButtonTitle:@"Cancel", otherButtonTitles:@"OK", nil];
[addAlert show];
[addAlert release];

我的问题是:是否可以在 iOS 上显示来自多个线程的警报等 UI 元素?

最佳答案

您绝对不希望从主线程以外的任何线程显示警报(或任何与 UI 相关的内容)。我建议将您的警报代码放在一个函数中并调用其中一个 performSelectorOnMainThread 调用。

- (void) showAlert
{
UIAlertView * addAlert = [[UIAlertView alloc] initWithTitle:@"New alert"
message:@"Example alert"
delegate:nil
cancelButtonTitle:@"Cancel", otherButtonTitles:@"OK", nil];
[addAlert show];
[addAlert release];
}

// ... somewhere in the worker thread ...
[self performSelectorOnMainThread:@selector(showAlert) withObject:nil waitUntilDone:NO];

关于ios - 显示 UI 元素,例如来自多个线程的警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5800589/

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