如何将许多 UIView(每个都有一个按钮)作为子视图添加到 UIScrollview
我正在尝试使用循环将许多 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不保留控制器,它将在运行循环的当前传递结束时被释放(即使您正在使用其视图)。
您应该将所有
IndividualObjectViewController
实例存储在NSMutableArray
或NSMutableDictionary
中。这些容器保留您存储在其中的对象。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 anNSMutableArray
or anNSMutableDictionary
. Those containers retain the objects you store in them.