IOS 子类对父类的属性修改必须在viewDidLoad函数中进行,否则该属性值将为nil

发布于 2022-09-01 05:38:27 字数 7126 浏览 14 评论 0

我是弄了一个有公用组件的viewcontroller类作为基类,其他使用到该组件的类作为其子类。但是IOS 子类对父类的属性修改必须在viewDidLoad函数中进行,否则该属性值将为nil。
不知道是什么原因所致,求各位大大帮忙解决下
```object-c
//ViewController作为基类
ViewController.h

import <UIKit/UIKit.h>

define kWidth [UIScreen mainScreen].bounds.size.width

define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController : UIViewController

@property (nonatomic,strong)UINavigationItem *navi;
@property (nonatomic,strong)UIButton *barButton;
@property (strong,nonatomic)UIButton *bButton;

-(void)goBack:(id)sender;
-(void)addChose:(id)sender;
@end
ViewController.m

import "ViewController.h"

import "SubViewController.h"

define kWidth [UIScreen mainScreen].bounds.size.width

define kHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()

@end
@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self initView];
    }

  • (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }
    -(void)initView{
    self.view.backgroundColor = [UIColor whiteColor];
    UIImageView *bgImage = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    bgImage.image = [UIImage imageNamed:@"BG1.jpg"];
    bgImage.alpha = 1;
    [self.view addSubview:bgImage];

    UINavigationBar *naviBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,20,kWidth,44)];
    //naviBar.barTintColor = [UIColor colorWithRed:1.0f green:1.0f blue:1.0f alpha:0.39f];
    _navi = [[UINavigationItem alloc] initWithTitle:@"personBlog"];

    _barButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _barButton.frame = CGRectMake(0,0,38,38);
    [_barButton setImage:[UIImage imageNamed:@"btn_back_normal@2x.png"] forState:UIControlStateNormal];
    [_barButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:_barButton];
    _navi.leftBarButtonItem = leftButton;

    _bButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _bButton.frame = CGRectMake(0,0,38,38);
    [_bButton setImage:[UIImage imageNamed:@"btn_other_normal@2x.png"] forState:UIControlStateNormal];
    [_bButton addTarget:self action:@selector(addChose:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:_bButton];
    _navi.rightBarButtonItem = rightButton;

    [naviBar pushNavigationItem:_navi animated:NO];
    [self.view addSubview:naviBar];

}
-(void)goBack:(id)sender{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)addChose:(id)sender{
SubViewController * sub = [[SubViewController alloc]init];
[self presentViewController:sub animated:YES completion:nil];
}
@end

//SubViewController.h作为子类
SubviewController.h

import "ViewController.h"

@interface SubViewController : ViewController
@end
SubviewController.m

import "SubViewController.h"

define kWidth [UIScreen mainScreen].bounds.size.width

define kHeight [UIScreen mainScreen].bounds.size.height

@interface SubViewController ()

@end

@implementation SubViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%f,%f",kWidth,kHeight);
    [self.navi setTitle:@"Subview"];
    UIButton *helloButton = [[UIButton alloc]initWithFrame:CGRectMake(0,64,kWidth,kHeight)];
    [helloButton setTitle:@"subview" forState:UIControlStateNormal];
    [helloButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    [helloButton setBackgroundColor:[UIColor whiteColor]];

    UIButton * innerButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,kWidth,110)];
    [innerButton setTitle:@"hello,innerButton"forState:UIControlStateNormal];
    [innerButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    [innerButton setBackgroundColor:[UIColor blueColor]];

    [helloButton addSubview:innerButton];
    // UIButton * small = [[UIButton alloc]initWithFrame:CGRectMake(0,0,)]

    [self.view addSubview:helloButton];
    [self.bButton setImage:[UIImage imageNamed:@""]forState:UIControlStateNormal];
    }
    将属性修改放在viewDidLoad函数中进行,结果是正确的。如图所示:
    图片描述

如果将属性修改作为一个函数放在外面就会出现错误结果,代码如下,结果如图所示:
SubviewController.m

  • (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    NSLog(@"%f,%f",kWidth,kHeight);
    // [self.navi setTitle:@"Subview"];
    // UIButton *helloButton = [[UIButton alloc]initWithFrame:CGRectMake(0,64,kWidth,kHeight)];
    // [helloButton setTitle:@"subview" forState:UIControlStateNormal];
    // [helloButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    // [helloButton setBackgroundColor:[UIColor whiteColor]];
    //
    // UIButton * innerButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,kWidth,110)];
    // [innerButton setTitle:@"hello,innerButton"forState:UIControlStateNormal];
    // [innerButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    // [innerButton setBackgroundColor:[UIColor blueColor]];
    //
    // [helloButton addSubview:innerButton];
    // // UIButton * small = [[UIButton alloc]initWithFrame:CGRectMake(0,0,)]
    //
    // [self.view addSubview:helloButton];
    // [self.bButton setImage:[UIImage imageNamed:@""]forState:UIControlStateNormal];
    }
    -(void)initView{
    [self.navi setTitle:@"Subview"];
    UIButton *helloButton = [[UIButton alloc]initWithFrame:CGRectMake(0,64,kWidth,kHeight-110)];
    [helloButton setTitle:@"subview" forState:UIControlStateNormal];
    [helloButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    [helloButton setBackgroundColor:[UIColor whiteColor]];

    UIButton * innerButton = [[UIButton alloc]initWithFrame:CGRectMake(0,0,kWidth,110)];
    [innerButton setTitle:@"hello,innerButton"forState:UIControlStateNormal];
    [innerButton setTitleColor:[UIColor redColor]forState:UIControlStateNormal];
    [innerButton setBackgroundColor:[UIColor blueColor]];

    [helloButton addSubview:innerButton];
    // UIButton * small = [[UIButton alloc]initWithFrame:CGRectMake(0,0,)]

    [self.view addSubview:helloButton];
    [self.bButton setImage:[UIImage imageNamed:@""]forState:UIControlStateNormal];
    }

图片描述

加断点调试发现:self.navi = nil,不知道原因,调试截图如下:
图片描述

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

问题的原因是因为我的”属性“其实是局部变量,在函数的内部的变量,在函数外面就变为nil了。

浪菊怪哟 2022-09-08 05:38:27

对父类属性的修改肯定子类的任何方法里面都可以进行更改啊。你可以参照一下ViewController的生命周期,看看你获取属性时,你重载的方法是否已经执行了。要不然你不会是忘了调用super xxxmethod了吧

与往事干杯 2022-09-08 05:38:27

SubviewController中的-(void)initView 方法名不要和父类的-(void)initView 方法重名。

情深缘浅 2022-09-08 05:38:27

这是多态写法。子类调用[self initView] 不会执行父类的initView,你需要[super initView]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文