从UIController调用UIView

时间:2015-06-16 09:39:19

标签: ios objective-c uiview

我试图从我的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];
}

2 个答案:

答案 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];
}

我希望它会对你有所帮助!