[英]iOS - How can I initialize a UINavigationController to display a pushed ViewController?
[英]How do I initialize instance variables and properties in the ViewController
在ViewController中初始化實例變量和屬性的語法是什么? 這是實現的代碼
#import "CalcViewController.h"
@interface CalcViewController ()
{
int initialValue;
}
@property(nonatomic,weak) BOOl isFirstEntry;
@end
@implementation CalcViewController
@synthesize isFirstEntry = _isFirstEntry;
.......
.......
.......
.......
@end
-(id) init
{
self = [super init];
if(self)
{
............;
}
return self;
}
這不會被觸發。
如果您不想在加載方法期間執行此操作,請實現指定的初始化程序並在其中初始化它。 如果你沒有重新定義指定的初始化程序,那么你從超類繼承它,你將在超類的指定初始化程序中初始化。 UIViewController
的指定初始化程序是:
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
使用時創建對象時將調用init方法
CalcViewController *calcView = [CalcViewController alloc] init]; or [CalcViewController new];
如果使用其他一些啟動方法創建對象,則不會調用它。 簡而言之,您必須記住init就像任何其他方法一樣。
我想你的ViewController是從一個筆尖加載..(Xcode項目模板)嘗試下列之一:
- (id)initWithCoder:(NSCoder)aDecoder {
self = [super initWithCoder:aDecoder];
if(self) {
...
}
return self;
}
但請注意,調用此方法時,未設置Interface Builder的所有連接(父級,子級,操作,...)。 如果您想依賴IB連接,可以使用以下方法:
- (void)awakeFromNib{
...
}
請注意,只有從NIB加載ViewController本身時才會調用這兩種方法,而不僅僅是視圖。 這通常是因為XCode為您提供了一個MainWindow.xib,其中包含一個視圖控制器( - > NIB)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.