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!
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With