gpt4 book ai didi

iphone - 线程问题(EXC_BAD_ACCESS)

转载 作者:行者123 更新时间:2023-12-03 16:57:49 29 4
gpt4 key购买 nike

以下代码使应用程序崩溃,我不知道为什么。它不规则地崩溃,这意味着有时单击一行时 ImageView 可以显示 30 次,有时当我选择一行时它会第二次崩溃。

仅供引用:事件指示器、操作表和变量 imageNr 是全局定义的,并将在 viedDidLoad 方法中初始化。

提前感谢您的建议。
肖恩

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
imageNr = indexPath.row + 1;
activityLoadView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(-17.5, -11, 35, 35)];
activityLoadView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
activityLoadView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleTopMargin |
UIViewAutoresizingFlexibleBottomMargin);

actionSheetLoadView = [[UIActionSheet alloc] initWithTitle:@""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
actionSheetLoadView.actionSheetStyle = UIBarStyleBlackOpaque;

[actionSheetLoadView setMessage:[NSString stringWithFormat:@"\n\n\n\n\n\n\n\n\n\n\n\n\nLoading image ..."]];
[actionSheetLoadView addSubview:activityLoadView];
[actionSheetLoadView showInView:self.view];
[actionSheetLoadView setBounds:CGRectMake(0,0,320,720)];

[activityLoadView performSelector:@selector(startAnimating) withObject:nil afterDelay:0.1];
[self performSelectorInBackground:@selector(showImageView) withObject:nil];
}

- (void)showImageView
{
[self setViewInfo];
[self.navigationController pushViewController:imageViewController animated:YES];

[activityLoadView performSelector:@selector(stopAnimating) withObject:nil afterDelay:0.1];
[actionSheetLoadView dismissWithClickedButtonIndex:0 animated:YES];
[activityLoadView release];
[actionSheetLoadView release];
}

最佳答案

除此之外,您正在后台线程上执行用户界面更新(通过在后台运行-showImageView)。用户界面元素在 iPhone OS 上不是线程安全的,必须在主线程上更新。几乎所有事情都在 -showImageView 中完成方法需要在主线程上完成(-setViewInfo 可能异常(exception),具体取决于它的作用)。

此外,在后台线程上释放对象时要非常小心,因为它们可能正在主线程(或其他线程)上使用,这可能会导致崩溃。

关于iphone - 线程问题(EXC_BAD_ACCESS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2538965/

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