ios - 通过引用(指针)传递与iOS对象冲突?

标签 ios

我有一个包含少量 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/

相关文章:

ios - 我怎样才能简单地将 View 转换为 SwiftUI View ?

ios - CNContactStore 联系人记录数

ios - 为什么我的可旋转轮控件有时会向错误的方向旋转?

iphone - 在 MFMailComposeViewController 中获取收件人列表

ios - 如何动态获取IOS模拟器设备的UID,然后安装到那个设备中

ios - 为什么 Xcode 在使用 CocoaPods 后不安装我的 iOS 应用程序?

ios - 谷歌街景使用设备陀螺仪旋转相机

ios - 如何用 CMMotionManager 替换 UIAccelerometer?

ios - 动画 View - 旋转、缩小并移出屏幕

ios - 苹果推送证书过期