我试图从我的ViewController调用UIView FirstScreen。
但我不知道该怎么做。
的ViewController:
#import "ViewController.h"
#import "FirstScreen.h"
@interface ViewController ()
{
FirstScreen *f;
}
FirstScreen.m:
#import "FirstScreen.h"
@implementation FirstScreen
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self didLoad];
}
return self;
}
-(void) didLoad{
[self addLabel];
[self addImageView];
}
答案 0 :(得分:1)
@implementation FirstScreen
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self didLoad];
}
return self;
}
-(void) didLoad
{
f = [[FirstScreen alloc] initWithFrame:CGRectMake(0, 0, self.view.size.width, self.view.size.height)];
[self.view addSubview:f];
}
这将有所帮助。
感谢先发制人的推动。
答案 1 :(得分:0)
#import "FirstScreen.h"
@implementation FirstScreen
-(instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
// calling another methods
[self anotherMethod];
return self;
}
-(void) anotherMethod
{
// write some code here
}
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
FirstScreenObj = [[FirstScreen alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
FirstScreenObj.backgroundColor = [UIColor grayColor];
[self.view addSubview:FirstScreenObj];
}
我希望它会对你有所帮助!