如何从Objective-C中的超类引用子类的视图?

时间:2016-02-10 04:02:50

标签: ios objective-c oop

所以我有几个不同的视图控制器,我想要登录屏幕,这只是一个模糊屏幕上的简单文本框。因此,我认为最好的想法是创建一个名为Login的超类,所有不同的视图控制器都可以使用。这是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

子类(我到目前为止只是尝试制作一个)是空的,除了&#34;输入&#34;的定义。它只是打印出来&#34;登录成功&#34;。我运行时获得的唯一输出是:

In the Login viewDidLoad
Presenting the login screen

屏幕上没有显示任何内容:只有白色。我假设这是因为我试图修改子类的self.view,而不是超类,因为子类是实际呈现的东西。有没有更好的方法呢?我没有想到的其他一些设计模式?或者有一种简单的方法来解决这个问题吗?

编辑:我刚刚意识到我运行的代码与我在这里粘贴的代码略有不同。我现在更新了它,但只显示模糊,而不是文本字段。我也意识到我的CGRect错了,它应该像CGRectMake(0,0,100,20);那样,所以我修复了它,文本字段仍然没有显示。是否有可能发生的原因。

1 个答案:

答案 0 :(得分:1)

设置for (int i = 0; i < addlikes.length(); i++) //looped { JSONObject addslikobj = addlikes.getJSONObject(i); //every json object data is stored in addlikobj likesdata.put(addslikobj);//finally kept all the addlikobjs of each loop is kept in likesdata Jsonobj } height,并将width设置为background color。现在它正在采用透明的文本视图

white
相关问题