我在这里检查了许多接近的答案,没有人可以解决我的愚蠢问题..
我的问题是:我有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; @endclassB .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; @endclassB .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]; } @endIf 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...
更多推荐
发布评论