John Farkerson
John Farkerson

Reputation: 2632

How can I refer to the view of a subclass from a superclass in Objective-C?

So I have a few different view controllers that I want to have login screens over, which are just a simple text box over a blurred screen. Thus, I thought the best idea would be to make a superclass called Login that all the different view controllers could use. Here's the code for Login.h:

#import <UIKit/UIKit.h>

@interface Login : UIViewController

@property (strong, nonatomic) UIVisualEffectView *blurEffectView;
@property (strong, nonatomic) UITextField *pass;

- (void) enter;

@end

Login.m:

#import "Login.h"

@interface Login () {
    NSString *password;
}


@end

@implementation Login

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NSLog(@"In the Login viewDidLoad");
    [self presentLogin];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) presentLogin {
    NSLog(@"Presenting the login screen");

    [self.view setBackgroundColor:[UIColor whiteColor]];

    self.pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.pass setCenter:self.view.center];

    [self.view addSubview: self.pass];

    [self.pass addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];


    if (!UIAccessibilityIsReduceTransparencyEnabled()) {

        UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
        self.blurEffectView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
        self.blurEffectView.frame = self.view.bounds;
        self.blurEffectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        [self.view insertSubview:self.blurEffectView belowSubview:self.pass];
    }


}

- (void) textFieldDidChange: (UITextField *) t {
    if ([self.pass.text isEqualToString:[NSString stringWithFormat:@"17"]]) {
        [self.blurEffectView removeFromSuperview];
        [self.pass removeFromSuperview];
        [self enter];
    }    
    NSLog(@"You're changing the text.");
}

- (void) enter {
    //to implement in the subclasses individually
}

@end

The subclass (I am just trying to make one so far) is empty except for a definition of "enter" which simply prints out "login successful". The only output I am getting when I run this is:

In the Login viewDidLoad
Presenting the login screen

Nothing shows up on the screen: just white. I assume this is because I am trying to modify the self.view of the subclass, not the superclass, since the subclass is the thing that is actually getting presented. Is there a better way of doing this? Some other design pattern that I am not thinking of? Or is there an easy way to get around this?

Edit: I just realized that the code I was running was slightly different from what I pasted here. I now updated it, but only the blur shows up, not the text field. Also I realized I had the CGRect wrong, it should be something like CGRectMake(0,0,100,20); so I fixed that, and the text field still doesn't show. Is there a reason that might be happening.

Upvotes: 0

Views: 56

Answers (1)

KDeogharkar
KDeogharkar

Reputation: 10959

Set your height and width and also set your background color to white . for now it is taking transparent text view

self.pass = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    [self.pass setCenter:self.view.center];
    [self.pass setBackgroundColor:[UIColor whiteColor]]; // default taking clear color for now

Upvotes: 1

Related Questions