[英]App suddenly crashes while processing presentViewController method in iOS 7.1
今天,我將Xcode升級到最新版本(版本5.1(5B130a))以支持iOS 7.1。 完成此操作后,我通常會在Xcode中運行我的項目。 然后應用崩潰。 在升級SDK版本之前,我沒有更改任何代碼。 該代碼可以在iOS 5.x,6.x,7.0.x中完美運行。
我只是在當前視圖控制器中呈現另一個視圖控制器。 它們都由情節提要初始化。 在處理presentViewController方法時,它會收到錯誤消息“線程1:EXC_BAD_ACCESS(代碼= 2,地址= 0x60)”。 我已經檢查了變量,它們都還活着,而不是釋放的垃圾。 iOS 7.1有什么問題?
該項目正在使用非ARC機制。 這是我的代碼:
@property (nonatomic, retain) ArticleViewController *articleView;
....
self.articleView = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ArticleViewController"];
...
[self presentViewController:self.articleView animated:NO completion:^() {
log(@"has shown article page...");
}];
但是如果通過使用addSubView函數顯示另一個視圖,則效果很好:
[self.view addSubView:self.articleView.view];
我真的不知道為什么會這樣。
這發生在我的應用程序中,同時為視圖控制器提供了modalPresentationStyle = UIModalPresentationCustom;
這是我的代碼在iOS 7.0上的樣子:
//Inside my MyPresentedViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self setupContent];
}
return self;
}
- (void)setupContent
{
//TransitionManager adopts UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning
TransitionManager *transitionManager = [[TransitionManager alloc] init];
transitionManager.presenting = YES;
self.modalPresentationStyle = UIModalPresentationCustom;
self.transitioningDelegate = transitionManager;
}
但是,以上代碼在iOS 7.1上崩潰,因此我將實現更改為:
@interface MyPresentedViewController
@property (nonatomic, strong) TransitionManager *transitionManager;
@end
...
//Inside my MyPresentedViewController:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
[self setupContent];
}
return self;
}
- (void)setupContent
{
//TransitionManager adopts UIViewControllerTransitioningDelegate and UIViewControllerAnimatedTransitioning
self.transitionManager = [[TransitionManager alloc] init];
_transitionManager.presenting = YES;
self.modalPresentationStyle = UIModalPresentationCustom;
self.transitioningDelegate = _transitionManager;
}
基本上,我沒有在setupContent
方法中聲明transitionManager
對象,而是為其創建了一個私有屬性(強引用)。
嗯,我唯一能想到的就是確保您的articleView屬性是一個強大的參考。
@property (nonatomic, strong) ArticleViewController *articleView;
我在選擇器視圖中遇到了類似的問題。 以我為例,在選擇處理程序塊之后,pickerview設置為nil,盡管直到昨晚這都沒有引起問題,但它還是導致該應用在今天早晨崩潰。 刪除該行解決了該問題,但是上個月我將此項目轉換為ARC,因此您可能必須找到一種更好的清理方法。
這可能與所陳述的問題或答案完全無關,但在iPhone 5S上訪問NSString時遇到了類似的問題。 完全相同的代碼可以同時在iPad和iPad上正常運行。
首先,我認為重要的是要聲明我在ARC下運行,並且反復告訴我不要“釋放”我在函數中實例化的任何對象。 之前我遇到過問題,因此我使用一些C#背景將幾乎所有局部變量設置為nil(在C#中為null)。 是的,我也不信任MS GC。
回到眼前的問題; 我有一個名為“數據”的NSString。 從另一個類中的另一個方法讀取“數據”。 使用NSLog,我可以看到“數據”的內容。 在下一行中,我將“數據”轉換為數組以在scanf中使用它。 那仍然有效。 在第三行,我再次嘗試NSLog'data',但是隨后出現EXC_BAD_ACCESS錯誤。 每次都有一個不同的地址。 我對ARC的適應程度甚至不及對Microsoft垃圾收集器的適應程度,因此我將函數包裝在try..catch..finally中。 現在,“數據”位於try..catch..finally之外。 最后,我將“數據”設置為nil,這似乎可以解決問題。
我知道這有點冗長,但是我真的很感激有人可以解釋為什么會這樣。 我期望現在在我的代碼中彈出很多這樣的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.