2011-07-23 16 views
0

我目前有一個奇怪的問題。 我有一個pickerView與兩個按鈕(取消,並確定)的動作表。 他們工作完美,直到我選擇一個值在該選擇器,我點擊我的TabBarController包含我的NavigationController的第一個按鈕。帶有TabBarController的NavController中的ActionSheet - Exc_Bad_Access

我的動作表是在我的.h中聲明的,並且具有屬性(非原子,保留),我在我的dealloc函數中釋放它。

我有一個exc_bad_access如果我釋放這個對象,我不知道爲什麼,因爲我分配它。

這是我的函數,它創建我的工作表,如果是零,並建立它。

- (IBAction)select_marque :(id)sender 
{ 
    if (!actionSheet_marque) 
    { 
     actionSheet_marque = [[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil]; 
    } 
    [actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0); 


    picker_marque = [[UIPickerView alloc] initWithFrame: pickerFrame]; 
    picker_marque.showsSelectionIndicator = YES; 
    picker_marque.dataSource = self; 
    picker_marque.delegate = self; 

    [actionSheet_marque addSubview: picker_marque]; 

    [picker_marque release]; 

    UILabel *lbl = [[UILabel alloc] initWithFrame: CGRectMake(110, 7.0f, 150.0f, 30.0f)]; 
    lbl.text = @"Marque"; 
    lbl.backgroundColor = [UIColor clearColor]; 
    lbl.textColor = [UIColor whiteColor]; 

    [actionSheet_marque addSubview:lbl]; 

    [lbl release];  

    UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Ok"]]; 
    closeButton.momentary = YES; 
    closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f); 
    closeButton.segmentedControlStyle = UISegmentedControlStyleBar; 
    closeButton.tintColor = [UIColor blueColor]; 
    [closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged]; 

    [actionSheet_marque addSubview:closeButton]; 

    [closeButton release]; 

    UISegmentedControl *cancelButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Cancel"]]; 
    cancelButton.momentary = YES; 
    cancelButton.frame = CGRectMake(5, 7.0f, 50.0f, 30.0f); 
    cancelButton.segmentedControlStyle = UISegmentedControlStyleBar; 
    cancelButton.tintColor = [UIColor redColor]; 
    cancelButton.tag = 1; 
    [cancelButton addTarget:self action:@selector(cancelActionSheet:) forControlEvents:UIControlEventValueChanged]; 

    [actionSheet_marque addSubview:cancelButton]; 

    [cancelButton release]; 

    [actionSheet_marque showInView: self.navigationController.view]; 

    [actionSheet_marque setBounds:CGRectMake(0, 0, 320, 485)]; 
} 

我也檢查指針爲nil在dealloc方法....

感謝

+0

你能提供完整的EXEC_BAD_ACCESS錯誤日誌?如果您在調試器下運行程序,堆棧跟蹤包含什麼? – sergio

回答

0

如果你有UIActionsheet的屬性然後當你分配它,你應該使用self.actionSheet_marque。嘗試改變這樣的代碼

if (!actionSheet_marque) 
    { 
     self.actionSheet_marque = [[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil] autorelease]; 
    } 


    [self.actionSheet_marque setActionSheetStyle:UIActionSheetStyleBlackTranslucent]; 

而且在整體功能,除了這行,如果(!actionSheet_marque)

+0

'self.retainOrCopyProperty = alloc-init'泄漏。 alloc-init返回一個你擁有的對象,並且retain屬性再次聲明對象的所有權,這導致對象被過度保留。 – albertamg

+0

對不起,我忘了告訴他把autorelease放在最後。我編輯了答案。 –

+0

感謝您的回答,我會嘗試你的想法。我是一個新手un Objective-C。如果我的ActionSheet_Marque在我的課程中,如果我將它分配給我的.m文件,我可能不會保留或執行一些步驟。其次,如果我對某個變量做一些屬性,當我分配它時,是否需要自己創建self.VarName? (對不起,我的英語能力很差,我是法語) – Morgan

相關問題