[英]presentModalViewController:animated: crash after dismiss
[英]application crashed after dismiss presentModalViewController
使用以下按鈕轉到另一個控制器時出現問題
-(IBAction)art:(id)sender{
TestYourInfoViewController *test = [[TestYourInfoViewController alloc]
initWithNibName:@"TestYourInfoViewController" bundle:[NSBundle mainBundle]];
test.questionType = @"art";
testYourInfoViewC = test;
[testYourInfoViewC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController presentModalViewController:testYourInfoViewC animated:YES ];
[test release];
}
當我返回以下內容時
-(IBAction)back:(id)sender{
[[self parentViewController] dismissModalViewControllerAnimated:YES];
}
它崩潰沒有堆棧跟蹤的應用程序..請問這是怎么回事。
標頭中是否將testYourInfoViewC
定義為保留的@property
? 如果是這樣,您應該始終使用self
和點表示法來引用它。
- (IBAction)art:(id)sender
{
TestYourInfoViewController *test = [[TestYourInfoViewController alloc]
initWithNibName:@"TestYourInfoViewController" bundle:[NSBundle mainBundle]];
test.questionType = @"art";
self.testYourInfoViewC = test;
[self.testYourInfoViewC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[self.navigationController presentModalViewController:self.testYourInfoViewC animated:YES ];
[test release];
}
當創建保留的@property
並@synthesize
時,將創建一個setter,用於處理與保留新對象和釋放舊對象有關的內存管理,但是通過將test
分配給testYourInfoViewC
您將繞過該合成的setter。
讓我們在這里逐步完成。 您已經使用alloc/init
創建了test
,因此將其keepCount設置為1。接下來,您將testYourInfoViewC
分配給test
。 保留計數沒有變化, testYourInfoViewC
現在只是指向與test
相同的對象,而不是為其自身保留副本。
現在,當您在test
中調用release
時,保留計數將返回0,並且對象將被釋放。 您的TestYourInfoViewController
實例已完全消失,而testYourInfoViewC
現在隨風飄揚。 嘗試關閉它時, parentViewController
將嘗試向幕后的對象發送一些消息,例如-viewWillDisappear:
,- -viewDidDisappear:
等。
編輯:這是我在項目中處理這種情況的方式。 我覆蓋屬性的getter並確定是否需要創建它。 這樣,我可以在代碼中的任何位置調用該屬性,並且可以確保,如果未創建該屬性,則將及時對其進行分配,初始化和設置。
- (TestYourInfoViewController *)testYourInfoViewC
{
if (!testYourInfoViewC)
{
testYourInfoViewC = [[TestYourInfoViewController alloc] init]; // This will work because the .xib and class name are identical.
[testYourInfoViewC setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
}
return testYourInfoViewC;
}
設置獲取程序以提供延遲實例化之后,您的-art:
方法將如下所示...
- (IBAction)art:(id)sender
{
[self.navigationController presentModalViewController:self.testYourInfoViewC animated:YES];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.