2015-06-16 28 views
0

我想從我的ViewController調用UIView FirstScreen。從UIController調用UIView

但我不知道該怎麼做。

的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

你是什麼意思*調用* UIView FirstScreen? –

回答

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

這將是有益的。

非常感謝您的支持。

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

我希望它能幫助你!