我有一个包含少量 UITableView 和 UILabel 的 View 。我以编程方式创建它们(即不使用 NIB)。
我已将 tableView 和标签创建合并到带有签名的方法中:
- (void) CreateTableView:(UITableView*) outTableView andLabel:(UILabel*)OutLabel AtFrame:(CGRect) frame{
CGRect labelFrame = frame;
labelFrame.origin.x = LABEL_LEFT_ALIGNMENT;
labelFrame.origin.y -= LABEL_TOP_ALIGNMENT;
labelFrame.size.height = LABEL_HEIGHT;
outLabel = [[UILabel alloc] initWithFrame:labelFrame];
[[self view] addSubview:outLabel];
outTableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
[outTableView setDataSource:self];
[outTableView setDelegate:self];
[[self view] addSubview:outTableView];
}
这里,outTableView和outLabel是输出参数。也就是说,方法完成后,调用者将使用outTableView和outLabel。
我的应用程序有 3 个 tableview 实例变量——tableView1、tableView2、tableView3。和三个标签实例变量。查看 Controller (调用者)调用,例如:
[self CreateTableView:tableView1 andLabel:label1 AtFrame:frame1];
[self CreateTableView:tableView2 andLabel:label2 AtFrame:frame2];
[self CreateTableView:tableView3 andLabel:label3 AtFrame:frame3];
此方法完成后,UILabel* 将呈现在屏幕上,调用者可以使用 UILabel* 对象。奇怪的是,UITableView* 对象并非如此。
知道为什么会有不同的行为吗?
注意:我的应用程序启用了 ARC。
最佳答案
错了。这些参数实际上不会包含分配/初始化的实例,因为您是按值传递它们,而不是按引用传递它们。考虑将指针传递给对象,然后在分配新实例时取消引用它:
- (void)createTableView:(UITableView **)tvPtr
{
*tvPtr = [[UITableView alloc] init...];
// etc.
}
这样调用:
UITableView *tv;
[self createTableView:&tv];
关于ios - 通过引用(指针)传递与iOS对象冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231286/