2012-02-27 71 views
0

我試圖將輸入到文本字段中的內容呈現到稍後將顯示的nsmutable數組中。 我認爲問題出在 - (void)viewDidLoad方法中,但我包含了所有的代碼以防萬一。問題在於,我將離開這個頁面,然後在選擇另一條信息之後返回它。發生這種情況時,我需要跟蹤輸入到文本字段中的每件事情。謝謝你的幫助!如何將文本字段添加到NSMutableArray

#import "EnteringCoursesViewController.h" 
#import "SelectRotationController.h" 


@implementation EnteringCoursesViewController 

@synthesize classField; 
@synthesize indicatedClass; 
@synthesize labelClassTitle; 
@synthesize selectRotationController; 
@synthesize classesEnteredTable; 

- (IBAction)chooseType { 
    UIActionSheet *typeSheet = [[UIActionSheet alloc] 
           initWithTitle:@"Class types" 
           delegate:self 
           cancelButtonTitle:nil 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"Core Class", @"Elective", nil]; 
    [typeSheet showInView:self.view]; 
    [typeSheet release]; 
} 

- (void)actionSheet:(UIActionSheet *)typeSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc] 
           initWithFormat:@"%@ meets 6 times per rotation", indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 
    else if (buttonIndex == 1) { 
     self.indicatedClass = classField.text; 
     NSString *indicatedString = indicatedClass; 
     NSString *greeting = [[NSString alloc] 
           initWithFormat:@"%@ meets 3 times per rotation", indicatedString]; 
     labelClassTitle.text = greeting; 
     labelClassTitle.hidden = NO; 
     [greeting release]; 
     [indicatedClass release]; 
    } 

} 

- (IBAction)chooseFirstMeeting:(id)sender {  
    SelectRotationController *selectView = [[SelectRotationController alloc] 
               initWithNibName:@"SelectRotationController" 
               bundle:[NSBundle mainBundle]]; 
    [selectView.navigationItem setTitle:@"First Period Day Choose"]; 

    [self.navigationController pushViewController:self.selectRotationController animated:YES]; 
    self.selectRotationController = selectView; 
    [selectView release]; 
} 

- (IBAction)enteredClassText:(id)sender { 
    NSMutableArray *classesEntered = [[NSMutableArray alloc] init]; 
    [classesEntered addObject:indicatedClass]; 
    [classesEntered release]; 

} 

- (void)didReceiveMemoryWarning { 
    [super didReceiveMemoryWarning]; 

} 

- (void)viewDidLoad { 
    self.navigationItem.hidesBackButton = YES; 
    [super viewDidLoad]; 

} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [classField release]; 
    [labelClassTitle release]; 
    [indicatedClass release]; 
    [selectRotationController release]; 
    [classesEnteredTable release]; 
    [super dealloc]; 
} 


@end 

回答

0

如果viewDidLoad被調用「requestedClass」尚未初始化,因此nil。

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

Important Raises an NSInvalidArgumentException if anObject is nil. 

如果你想保存留下的觀點,在viewDidUnload法添加ADDOBJECT呼叫。當然,你應該檢查是否值爲零;)

我沒有看到你的變量指示類的任何分配,但一個釋放!?如果viewDidUnload正在調用,則該變量可能不存在。

編輯

您初始化一個NSMutableArray的對象添加到這個數組之後,你發佈的對象。因此數據已經消失。您必須隨時保存您的陣列,以便日後可以使用該內容。關鍵字:NSUserDefaults的;)

檢查也無價值觀的:

- (IBAction)enteredClassText:(id)sender { 
    if (indicatedClass != nil) { 
     NSMutableArray *classesEntered = [[NSMutableArray alloc] init]; 
     [classesEntered addObject:indicatedClass]; 
     [classesEntered release]; 
    } 
} 

如果發件人是一個的UILabel你也可以使用這個片段:

- (IBAction)enteredClassText:(id)sender { 
    if (sender.text != nil) { 
     NSMutableArray *classesEntered = [NSMutableArray arrayWithObject:sender.text]; 
     // TODO: Save your array to NSUserDefaults... 
    } 
} 
+0

所以這個有什麼改變的代碼?與另一種方法的代碼。 – mentorship 2012-02-27 14:44:44

+0

編輯我的答案;) – matzino 2012-02-27 14:57:39

+0

它說文本不在結構或聯合... – mentorship 2012-02-27 17:28:31