gpt4 book ai didi

objective-c - 如何执行 [self.view addSubview : lbl] outside of ViewController Class scope?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:13 27 4
gpt4 key购买 nike

如何在 ViewController 类范围之外执行 [self.view addSubview: lbl]?

或:

如何在 ViewController 类之外的主视图中的不同类中添加标签或其他 View ?

谢谢

- (void)viewDidLoad { 
[super viewDidLoad];

UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[lbl setText:@"hi there"];

[self.view addSubview:lbl];// <-- this works, but ...
// what is "self" referring to?
// and how can I declare and call from another class?

...


UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[lbl setText:@"hi there"];

calcRomanAppDelegate *v = [[calcRomanAppDelegate new] init];
[v.viewController.view addSubview:lbl]; // this compiles, but...
// fails to shows a label on the form

...


UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
[lbl setText:@"hi there"];

calcRomanViewController *v = [[calcRomanViewController new] init];
[v.view addSubview:lbl]; // this just makes a call back to viewDidLoad... endless loop

}

最佳答案

好吧,view 只是 UIViewController 类的一个属性。假设你的 UIViewController *controller 变量在某处,你可以使用

[controller.view addSubview:subview]

关于objective-c - 如何执行 [self.view addSubview : lbl] outside of ViewController Class scope?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6796979/

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