如何在ViewController类范围之外执行[self.view addSubview:lbl] [英] How to perform a [self.view addSubview: lbl] outside of ViewController Class scope?

查看:126
本文介绍了如何在ViewController类范围之外执行[self.view addSubview:lbl]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



或:



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



感谢

   - (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 ...
//什么是self指的是?
//如何声明和调用另一个类?

...


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 ...
//未能在表单上显示标签

...


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

calcRomanViewController * v = [[calcRomanViewController new] init];
[v.view addSubview:lbl]; //这只是回调viewDidLoad ...无限循环

}


c> c> c> c> c> 。假设你有 UIViewController * controller 变量在某处,你可以使用

  [controller.view addSubview:subview] 


How to perform a [self.view addSubview: lbl] outside of ViewController Class scope ?

or:

How do I add a label or another view in the mainview, outside of the ViewController class, in a different class?

thanks

- (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

}

解决方案

Well, view is just a property of the UIViewController class. Assuming that you have your UIViewController *controller variable somewhere, you can just use

[controller.view addSubview:subview]

这篇关于如何在ViewController类范围之外执行[self.view addSubview:lbl]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆