2011-07-19 31 views
0

我的iPhone應用程序是一個標籤欄的控制器負載3導航控制器:對於第一視圖是一個UIViewController,而對於第二和第三的意見是的UITableViewController。iPhone EXC_BAD_ACCESS上對象

在我所有的應用程序,我訪問對象「汽車」從核心數據的到來,我存儲在應用程序的委託。改變我的標籤欄的第一個導航控制器視圖後,我就EXC_BAD_ACCESS得到所有在3導航控制器(設置)訪問對象時的時間。調試器在@synthesize userCar=_userCar;處顯示錯誤。

怪異的一部分,當我在我的應用程序委託我的標籤欄的選擇指數的變化,它的工作原理。所以,當它加載默認值(指數= 0),當我走在設置視圖(指數= 2)它崩潰了,但如果我加載設置在第一視圖,然後我的應用程序所有的時間運行偉大。

崩潰:

// Display the window with the tab bar. 
[self.window addSubview:[self.tabBarController view]]; 
[self.window makeKeyAndVisible]; 

不會崩潰,如果負載在MyAppDelegate類:

// Loads the 3 view (Settings) to avoid crash... 
[self.tabBarController setSelectedIndex:2]; 

// Display the window with the tab bar. 
[self.window addSubview:[self.tabBarController view]]; 
[self.window makeKeyAndVisible]; 

所以我不明白,當我在我的主窗口變回筆尖的名稱和類名.xib(我有我的標籤欄控制器等)到它以前,它再次完美地工作。我嘗試清理項目,重新啓動Xcode並從模擬器和設備中刪除應用程序,但沒有任何工作。

有什麼想法? 謝謝!

更新1

所以我宣佈我的車對象這樣@property (nonatomic, retain) Car *userCar;在我所有的視圖控制器。

然後拿到現車,用戶選擇的一個,我這樣做:

// Get the latest user car selected. 
self.userCar = [EcoAppAppDelegate userCar]; 

然後我就應用委託我有2個功能,讓我與打車,如果用戶創建新的它會自動保存,如果在選擇另一輛汽車的設置中預先選擇了它,它也會保存它。在.m文件:

+ (Car *)userCar; 
+ (void)setUserCar:(Car *)newCar; 

在應用程序的委託.m文件:

// On top of the file. 
static Car *_userCar; 

#pragma mark - Static functions 

+ (Car *)userCar 
{ 
    return _userCar; 
} 

+ (void)setUserCar:(Car *)newCar 
{ 
    if (_userCar != newCar) { 
     if (newCar != nil) { 
      [_userCar release]; 
      _userCar = nil; 
      _userCar = [newCar retain]; 
     } 
     else { 
      [_userCar release]; 
      _userCar = nil; 
     } 
    } 
} 

最後一段代碼,當我從核心數據的車,我這樣做(在在應用程序的委託didFinishLaunchingWithOptions)應用程序例如開始:

// Load the car. 
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Car" inManagedObjectContext:[self managedObjectContext]]; 
[fetchRequest setEntity:entity]; 

// Set predicate and sort orderings... 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selected = 1"]; 
[fetchRequest setPredicate:predicate]; 

// Execute the fetch -- create a mutable copy of the result. 
NSError *error = nil; 
NSMutableArray *mutableFetchResults = [[self.managedObjectContext executeFetchRequest:fetchRequest error:&error] mutableCopy]; 
if (mutableFetchResults != nil && [mutableFetchResults count] > 0) { 
    // Get the car selected. 
    _userCar = (Car *)[mutableFetchResults objectAtIndex:0]; 
} 
else { 
    // Handle error. 
    _userCar = nil; 
} 

// Memory management. 
[fetchRequest release]; 
[mutableFetchResults release]; 
+0

您需要在控制器2中的CoreData中抓取對象的位置,也可能是0.這可能是問題所在。 – PengOne

+0

userCar變量的@properties是什麼以及如何從3個視圖控制器訪問它? – Joe

+0

我更新了代碼,讓我知道如果它不夠,我可以添加更多的東西。但幾乎我在視圖控制器中有一個汽車對象,並從應用程序委託中加載它。然後,當我對汽車上的設置進行更改(用戶創建一個新的汽車並自動選擇它,或者選擇之前創建的另一輛汽車時,它會更新應用程序代表中的汽車)。 – Dachmt

回答

1

我認爲主要的問題是,你是從核心數據獲取汽車對象不被保留。這是在節目的一開始一個有效的對象,但一旦一切都完成裝載車對象會被自動釋放。

您可以通過更改您的coredata代碼這個解決這個問題:

// Execute the fetch -- create a mutable copy of the result. 
NSError *error = nil; 
NSArray *fetchResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error]; 
if (fetchResults != nil && [fetchResults count] > 0) { 
    // Get the car selected. 
    _userCar = (Car *)[[fetchResults objectAtIndex:0] retain]; 
} else { 
    // Handle error. 
    _userCar = nil; 
} 

注意retain。另外,沒有理由對提取結果做一個可變的副本。

+0

真棒@lazycs!非常感謝,我沒有使用我的'+(void)setUserCar:(Car *)newCar'來執行'retain',所以它在我的應用程序委託中初始化汽車時丟失了。而且我正在創建一個可變副本,因爲我複製了另一段代碼,在那裏我需要(汽車)數組,我需要更加小心...... – Dachmt

相關問題