gpt4 book ai didi

iphone - 如何将 UIButton 传递给函数并在那里创建...?

转载 作者:行者123 更新时间:2023-11-28 22:30:40 24 4
gpt4 key购买 nike

问题可能看起来很愚蠢,只是在 Objective-C 的一些基础知识中遇到了问题。我知道 Objective-C 只支持按值传递,但是我的要求需要传递地址。我有一个 UIButton 成员变量 (iVar),我将它们传递给一个函数,并尝试使用如下参数在函数内部分配 init...

函数调用:

[self create_button : ivar_btn];

定义:

- (void) create_button : (UIButton*) button
{
// Create button
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"00/00/0000" forState:UIControlStateNormal];
[self.add_scroll_view addSubview:button];
}

那么在 Objective-C 中没有通过引用传递,我该如何处理这种情况?

如果理解有误,请指正。

注意:创建按钮是一个通用代码,我一直在使用它在运行时根据需求创建按钮。

谢谢

最佳答案

这是无用的实现,我还没有尝试过,但仍然如此。

UIButton *ivar_btn = nil;   
[self create_button : &ivar_btn];
- (void) create_button : (UIButton**) button
{
// Create button
*button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[*button setTitle:@"00/00/0000" forState:UIControlStateNormal];
[self.add_scroll_view addSubview:*button];
}

关于iphone - 如何将 UIButton 传递给函数并在那里创建...?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17564480/

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