如何将许多 UIView(每个都有一个按钮)作为子视图添加到 UIScrollview

发布于 2025-01-08 21:06:33 字数 792 浏览 7 评论 0原文

我正在尝试使用循环将许多 UIView 添加到 UIScrollview,每个 UIView 代表一个对象并包含从数据库查询返回的 UIImage + UILabel。每个 UIView 内部都有一个按钮,用于调用 superview 来跳转到下一个 UIViewController。然而,由于我在 for 循环中分配这些 UIView,内存似乎没有被保留,因此按下按钮总是会导致段错误。

由于我需要将存储在 UIView 中的 UIImage 和 UILabel 传递到我正在转入的控制器,那么最好的方法是什么?

这是我的代码:

父视图:

//这是从 for 循环

IndividualObjectViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"smallObjectSubView"];
controller.delegate = self;    
controller.view.frame = CGRectMake(105*column+5, 125*row+45, 100, 120);
[self.objectScrollView addSubview:controller.view];

对象视图调用的:

  • (IBAction)objectButton:(id)sender { 我将IBAction

完全留空以进行调试,但看起来只要我将此操作与情节提要按钮连接起来,我就会收到程序收到信号:EXEC_BAD_ACCESS 错误。

I'm trying use a loop to add many UIView, each representing an object and contains an UIImage + UILabel returned from a db query, to a UIScrollview. Each UIView has a button inside, which is designed to call the superview to segue to the next UIViewController. However, since I'm allocating these UIViews within a for-loop, the memory doesn't seem to be retained, so the button press always causes a seg-fault.

Since I need to pass the UIImage and UILabel stored in the UIView to the controller I'm segueway-ing into, what's the best way to do this?

Here is my code:

Parent view:

//this is called from a for loop

IndividualObjectViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"smallObjectSubView"];
controller.delegate = self;    
controller.view.frame = CGRectMake(105*column+5, 125*row+45, 100, 120);
[self.objectScrollView addSubview:controller.view];

object view:

  • (IBAction)objectButton:(id)sender {
    }

I left the IBAction completely empty for debug, but it looks like as soon as I connect this action with the storyboard button, I get program received signal: EXEC_BAD_ACCESS error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糖 2025-01-15 21:06:33

如果您不保留控制器,它将在运行循环的当前传递结束时被释放(即使您正在使用其视图)。

您应该将所有 IndividualObjectViewController 实例存储在 NSMutableArrayNSMutableDictionary 中。这些容器保留您存储在其中的对象。

If you don't retain controller, it will be deallocated at the end of the current pass through the run loop (even though you are using its view).

You should be storing all of the IndividualObjectViewController instances in an NSMutableArray or an NSMutableDictionary. Those containers retain the objects you store in them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文