IOS 子类对父类的属性修改必须在viewDidLoad函数中进行,否则该属性值将为nil
我是弄了一个有公用组件的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题的原因是因为我的”属性“其实是局部变量,在函数的内部的变量,在函数外面就变为nil了。
对父类属性的修改肯定子类的任何方法里面都可以进行更改啊。你可以参照一下ViewController的生命周期,看看你获取属性时,你重载的方法是否已经执行了。要不然你不会是忘了调用super xxxmethod了吧
SubviewController中的-(void)initView 方法名不要和父类的-(void)initView 方法重名。
这是多态写法。子类调用[self initView] 不会执行父类的initView,你需要[super initView]