从 UIController 调用 UIView

Posted

技术标签:

【中文标题】从 UIController 调用 UIView【英文标题】:Calling UIView from UIController 【发布时间】:2015-06-16 09:39:19 【问题描述】:

我正在尝试从我的 ViewController 调用 UIView FirstScreen。

但我不知道该怎么做。

视图控制器:

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

【问题讨论】:

你是什么意思调用 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];

【讨论】:

【参考方案2】:
#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];


希望对你有帮助!

【讨论】:

以上是关于从 UIController 调用 UIView的主要内容,如果未能解决你的问题,请参考以下文章

加深理解UIView,UIResponder,UIController

在将 iPhone 应用程序带回前台后,如何判断当前的 UIView/UIController 是啥?

如何从swiftui视图调用uikit viewcontroller方法

iOS基础之UIController(视图控制器)

如何从 Popover 调用主视图控制器的方法?

使用 RefreshControl 异步刷新