您必須將該方法添加到您的MyViewController
類中。
因此,您需要添加一個方法聲明及其相關實現。現在
//MyViewController.h
@interface MyViewController
{
int _controllerId;
}
@property (nonatomic, assign) int controllerId;
@end
//MyViewController.m
@implementation MyViewController
@synthesize controllerId = _controllerId;
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle andId:(int)contrId
{
if(self = [super initWithNibName:nibName bundle:nibBundle])
{
self.controllerId = contrId;
}
return self;
}
@end
您可以創建MyViewController
類像下面的實例。
MyViewController myController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil andID:0];
self.viewController1 = myController;
[myController release]; // if viewController1 has a retain policy
我建議你不要alloc-init並將新實例分配到同一行中的屬性。事實上,如果你的屬性viewController1有一個保留策略,你會創建一個內存泄漏。在替代:
self.viewController1 = [[[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil andID:0] autorelease]; // if viewController1 has a retain policy
的一些注意事項
由於initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle
是,你可以把它叫做沒有通過一個id爲您的新方法的公共方法。爲了避免這種情況,您可以將(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle andId:(int)contrId
作爲指定的初始值設定項。以這種方式,您不能創建沒有ID的控制器。
你能解釋一下你想要做什麼。你使用這個ID的是什麼? – Hosam 2012-01-15 00:52:03