一般我們開發的時候都會設置一個基類的UIViewController,博主設置了一個BaseViewController基類
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController
- (id)init{
if (self == [super init]) {
self.view.backgroundColor = [UIColor redColor];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
然後設置兩個界面ViewControllView 和SectionViewControllver繼承自BaseViewControllver;
#import "BaseViewController.h"
@interface ViewController : BaseViewController
@end
#import "BaseViewController.h"
@interface SectionViewController : BaseViewController
@property (nonatomic,copy) NSString *show;
@end
其中SectionViewController有一個show 的屬性,此時我們發送一個事件讓頁面1跳轉到頁面2,並設頁面2的show有值
- (void)showssClick{
SectionViewController *secVC = [[SectionViewController alloc] init];
secVC.show = @"我跳啦";
[self.navigationController pushViewController:secVC animated:YES];
}
但是跳轉到頁面二時,在ViewDidLoad顯示是爲空
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"%@",self.show);
}
2015-10-11 00:14:32.291 HelloWorldSW[2294:97355] (null)
樓主感覺各個邏輯都沒有錯,但是爲什麼後面就是沒值呢,而且基類只是在init上設置了一個self.view的背景色
後面樓主去掉了這個背景色,改在基類的ViewDidLoad實現,頁面二的show就有值了
#import "BaseViewController.h"
@interface BaseViewController ()
@end
@implementation BaseViewController
- (id)init{
if (self == [super init]) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}