在ViewController类之外添加UIView类以显示子视图(Adding a UIView class outside ViewController class to show subview

编程入门 行业动态 更新时间:2024-10-26 08:35:01
在ViewController类之外添加UIView类以显示子视图(Adding a UIView class outside ViewController class to show subview)

我在这里检查了许多接近的答案,没有人可以解决我的愚蠢问题..

我的问题是:我有2个类,UIViewController classA和UIView classB。

一个按钮(classA)触发器处理(classB),然后在屏幕上显示子视图(classA)。 但它不起作用。

classA .m:

@implementation ViewController ... - (IBAction)trigger:(UIButton *)sender { [classB makeViewOn]; }

classB .h:

#import <UIKit/UIKit.h> @interface classB : UIView + (void)makeViewOn; @end

classB .m:

#import "classB.h" #import "ViewController.h" @implementation classB + (void)makeViewOn { ViewController *pointer = [ViewController new]; UIWindow *window = pointer.view.window; classB *overlayView = [[classB alloc] initWithFrame:window.bounds]; overlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; overlayView.userInteractionEnabled = YES; [pointer.view addSubview:overlayView]; } @end

如果我只在一个类UIViewController中执行此操作,它可以正常工作; 但是,如果我在两个单独的类(UIViewController和UIView)上执行此操作,我该如何解决它?

我是否在类之间进行通信的基本概念上做错了什么?

非常感谢!

I have been check many close answers here, no one can solve my stupid problem..

My problem is: I have 2 classes, UIViewController classA and UIView classB.

A button(classA) trigger to process(classB), then show the subview on screen(classA). But it doesn't work.

classA .m:

@implementation ViewController ... - (IBAction)trigger:(UIButton *)sender { [classB makeViewOn]; }

classB .h:

#import <UIKit/UIKit.h> @interface classB : UIView + (void)makeViewOn; @end

classB .m:

#import "classB.h" #import "ViewController.h" @implementation classB + (void)makeViewOn { ViewController *pointer = [ViewController new]; UIWindow *window = pointer.view.window; classB *overlayView = [[classB alloc] initWithFrame:window.bounds]; overlayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5f]; overlayView.userInteractionEnabled = YES; [pointer.view addSubview:overlayView]; } @end

If I do this in only one class, UIViewController, it works fine; However, if I do this on two separate class(UIViewController & UIView), how can I fix it?

Am I doing something wrong on basic concepts of communicating between classes?

Thanks a lot!

最满意答案

首先 ,您正在创建一个名为ViewController类的指针的新对象,因此您的classB对象overlayView不会作为子视图添加到classA,而是添加到对象指针

其次 ,如果你打印并检查你的window.bounds它返回(null)。

修改classB中的类方法

+ (void)makeViewOnParentView:(id)sender; + (void)makeViewOnParentView:(id)sender { ViewController *pointer = (ViewController*)sender; //UIWindow *window = pointer.view.window; CGRect rect=pointer.view.frame; classB *overlayView = [[classB alloc] initWithFrame:CGRectMake(0, 0,rect.size.width, rect.size.height)]; overlayView.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5f]; overlayView.userInteractionEnabled = YES; [pointer.view addSubview:overlayView]; }

在你的classA中调用方法

[classB makeViewOnParentView:self];

希望这对你有用......

Firstly, you are creating a new object named pointer of class ViewController, therefore your classB object overlayView is not being added as a subview to the classA but rather to the object pointer.

Secondly, if you print and check your window.bounds its returning (null).

Modify you class method in classB

+ (void)makeViewOnParentView:(id)sender; + (void)makeViewOnParentView:(id)sender { ViewController *pointer = (ViewController*)sender; //UIWindow *window = pointer.view.window; CGRect rect=pointer.view.frame; classB *overlayView = [[classB alloc] initWithFrame:CGRectMake(0, 0,rect.size.width, rect.size.height)]; overlayView.backgroundColor = [[UIColor greenColor] colorWithAlphaComponent:0.5f]; overlayView.userInteractionEnabled = YES; [pointer.view addSubview:overlayView]; }

And in your classA call the method

[classB makeViewOnParentView:self];

Hope this will work for you...

更多推荐

本文发布于:2023-07-28 18:41:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1307968.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   UIView   ViewController   Adding   subview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!