我在iOS中有一個非常奇怪的委託行爲。我設置一個自定義委託在子類的UIViewController這樣的:iOS奇怪的委託行爲?
- (void)viewDidLoad
{
[super viewDidLoad];
self.baseNavigationBar = [[BaseNavigationBar alloc] initWithNibName:nil bundle:nil];
self.baseNavigationBar.delegate = self;
self.baseNavigationBar.navigationController = self.navigationController;
[self.navigationController.navigationBar addSubview:self.baseNavigationBar];
}
的initWithNibName可以使用零,使用默認筆尖的時候,因爲在內部它會在nibName檢查論壇爲零。
if (!nibNameOrNil) {
nibNameOrNil = NSStringFromClass([self class]);
}
從baseNavigationBar對象的委託聲明看起來如下:從運行的應用程序
@property (nonatomic, retain) id<BaseNavigationBarDelegate> delegate;
/*same thing with assign, which I you should use*/
/*and in the .m of course @synthesize*/
現在2截圖。
第一個節目從BaseListViewController,這是BaseCoreViewController,這是的UIViewController的子類的子類的調試器的值。
截圖是在調用viewDidLoad方法時進行的。
第二個節目是從BaseNavigationBar,這是一個的UIView子類的值。
的截圖被採取在一段時間,當用戶點擊「下一步」按鈕
- (IBAction)nextAction:(id)sender {
if (self.delegate) {
[self.delegate navigationBarDidClickNextButton:self];
}
}
那麼,爲什麼這是一個問題?通過單擊BaseNavigationBar中的按鈕,我的代理始終爲零,因此程序被卡住了。但是,在同時查看來自BaseCoreViewController的值時,代表不是零。很奇怪。店主:選擇:功能
編輯
的BaseNavigationBar從使用UINib loadNibNamed一個筆尖文件加載。
編輯2
所以這幾乎是所有的代碼。
編輯3
最後我們得到了在代碼的最後一件錯誤的源...設置自=東西完全不準......
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (!nibNameOrNil) {
nibNameOrNil = NSStringFromClass([self class]);
}
if (!nibBundleOrNil) {
nibBundleOrNil = [NSBundle mainBundle];
}
NSArray *topLevelObjects = [nibBundleOrNil loadNibNamed:nibNameOrNil owner:self options:nil];
/*Here is the bad part*/
self = [topLevelObjects objectAtIndex:0];
state = BaseNavigationBarButtonStateNext;
if (self) {
self.title = @"";
}
return self;
}
使用解決類方法而不是定義一個自定義的init。
@property(nonatomic,retain)id delegate; 爲什麼保留? http://stackoverflow.com/questions/918698/why-are-objective-c-delegates-usually-given-the-property-assign-instead-of-retai –
Injectios
根本無法使用assign。我不知道爲什麼。你對「爲什麼使用保留」是正確的。我快速添加模式代碼。 –
self.baseNavigationBar屬性強?給更多的代碼 – Injectios