Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从ViewController外部向视图添加UILabel_Ios_Objective C_Cocoa Touch - Fatal编程技术网

Ios 从ViewController外部向视图添加UILabel

Ios 从ViewController外部向视图添加UILabel,ios,objective-c,cocoa-touch,Ios,Objective C,Cocoa Touch,我在ViewController中有一个UIView,它创建并显示标签。而且,它做得很好。不过,我想做的是能够在其他类文件中调用方法,并让它们也能够将UILabel添加到我的视图中,但我在这方面遇到了困难 我在这里尝试了其他一些答案,比如: 但出于某种原因,这在我的项目中不起作用。我一定是用得不对,否则别人可能会有别的想法 我试过十几种不同的方法。这似乎是一种可行的方法,我将在我的ViewController中创建UIView作为属性,以便可以从其他类访问它,然后,在我的其他类文件中,我可以创

我在ViewController中有一个UIView,它创建并显示标签。而且,它做得很好。不过,我想做的是能够在其他类文件中调用方法,并让它们也能够将UILabel添加到我的视图中,但我在这方面遇到了困难

我在这里尝试了其他一些答案,比如:

但出于某种原因,这在我的项目中不起作用。我一定是用得不对,否则别人可能会有别的想法


我试过十几种不同的方法。这似乎是一种可行的方法,我将在我的ViewController中创建UIView作为属性,以便可以从其他类访问它,然后,在我的其他类文件中,我可以创建一个作为该ViewController类成员的新实例,然后向该实例添加新的子视图,它们将显示出来。但是,我错了。这是我最近犯的错误

--ViewController.h

#import <UIKit/UIKit.h>
#import "OtherClass.h"

@interface ViewController : UIViewController

@property UIView *myView;

@end

--ViewContoller.m 


- (void)viewDidLoad {
    [super viewDidLoad];



    // Do any additional setup after loading the view, typically from a nib.

    self.myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width)];

    self.myView = (UIView *)self.view;

    UILabel *myLabel = [[UILabel alloc] init];

    myLabel.text = @"Label #1";

    myLabel.textColor = [UIColor blackColor];

    myLabel.frame = CGRectMake(50,50,200,50);

    [self.myView addSubview:myLabel];

    OtherClass *oc = [[OtherClass alloc] init];


    [oc methodInOtherClass];

}

--OtherClass.m

- (void) methodInOtherClass {

    NSLog(@"hello!  In the other class file now.");

    UILabel *nooLabel = [[UILabel alloc] init];

    nooLabel.text = @"Label #2!”;

    nooLabel.frame = CGRectMake(75, 75, 100, 50);

    nooLabel.textColor = [UIColor blackColor];

    UIView *nooView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    nooView.backgroundColor = [UIColor redColor];

    [nooView addSubview:nooLabel];

    ViewController *myView2 = [[ViewController alloc] init];

    [myView2.myView addSubview:nooView];

}

@end
--ViewController.h
#进口
#导入“OtherClass.h”
@界面ViewController:UIViewController
@属性UIView*myView;
@结束
--ViewContoller.m
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
self.myView=[[UIView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.height,self.view.frame.size.width)];
self.myView=(UIView*)self.view;
UILabel*myLabel=[[UILabel alloc]init];
myLabel.text=@“标签#1”;
myLabel.textColor=[UIColor blackColor];
myLabel.frame=CGRectMake(50,50200,50);
[self.myView addSubview:myLabel];
OtherClass*oc=[[OtherClass alloc]init];
[oc methodInOtherClass];
}
--另一类
-(void)其他类中的方法{
NSLog(@“你好!现在在另一个类文件中。”);
UILabel*nooLabel=[[UILabel alloc]init];
noolable.text=@“Label#2!”;
noolable.frame=CGRectMake(75,75,100,50);
noolable.textColor=[UIColor blackColor];
UIView*nooView=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]bounds];
nooView.backgroundColor=[UIColor redColor];
[nooView添加子视图:nooLabel];
ViewController*myView2=[[ViewController alloc]init];
[myView2.myView addSubview:nooView];
}
@结束

您可以将视图传递到另一个类的方法中。现在,您正在创建视图控制器并向其添加标签,但您没有使用新的视图控制器执行任何操作

第一节课:

[oc methodInOtherClass:self.view];
第二节课:

- (void) methodInOtherClass:(UIView*)view {

    UILabel *nooLabel = [[UILabel alloc] init];
    nooLabel.text = @"Label #2!”;
    nooLabel.frame = CGRectMake(75, 75, 100, 50);
    nooLabel.textColor = [UIColor blackColor];

    UIView *nooView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    nooView.backgroundColor = [UIColor redColor];
    [nooView addSubview:nooLabel];

    [view addSubview:nooView];
}

正如其他人所指出的,首先这可能不是正确的方法,但我不会对您的体系结构进行任何假设。

-methodInOtherClass
创建一个新的视图控制器,而不是使用现有的视图控制器。”然后,在我的另一个类文件中,我可以创建一个作为ViewController类成员的新实例,然后向其中添加新的子视图,它们就会显示出来。“--这是你的主要误解。德拉特。这里至少有一个,也许还有两个答案是有趣的&进行了一些深思熟虑的讨论,人们都很友好地做出了贡献,我正在寻找一个特别是关于使用代表的答案。但我刷新了页面,看看是否有人有什么要补充的,特别是现在这个答案和它的讨论似乎消失了。有没有办法把那些拿回来?对不起,我以前从未在这里发帖。我已经潜伏了大约一年,但从未发布过。