如何将文本字段添加到NSMutableArray

时间:2012-02-27 14:21:45

标签: ios nsmutablearray uitextfield viewdidload

我正在尝试将输入到文本字段中的内容呈现为稍后将显示的nsmutable数组。 我认为问题出现在 - (void)viewDidLoad方法中,但我包含了所有代码以防万一。问题是,我将离开此页面,然后在选择另一条信息后返回该页面。当发生这种情况时,我需要跟踪输入文本字段的每个事物。谢谢你的帮助!

#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

1 个答案:

答案 0 :(得分:0)

如果调用viewDidLoad,则“shownClass”尚未初始化,因此为零。

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-Call。当然你应该检查值是否为nil;)

我没有看到你的变量的所有allocClassClass但是发布!?如果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...
    }
}