Как добавить текстовое поле в NSMutableArray - PullRequest
0 голосов
/ 27 февраля 2012

Я пытаюсь представить то, что было введено в текстовое поле, в массив 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 голосов
/ 27 февраля 2012

Если viewDidLoad называется «указал класс», он еще не инициализирован и поэтому равен нулю.

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

Important Raises an NSInvalidArgumentException if anObject is nil.

Если вы хотите сохранить это, оставив представление, добавьте addObject-Вызов в методе viewDidUnload.Определенно вы должны проверить, является ли значение ноль;)

Я не вижу каких-либо выделений для вашей переменной указано Class, но выпуск !?Возможно, переменная не существует, если вызывается viewDidUnload.

РЕДАКТИРОВАТЬ

Вы инициируете NSMutableArray, добавляете объект в этот массив и после этого вы освобождаете этот объект,Поэтому данные отсутствуют.Вы должны сохранить свой массив с ним вы можете использовать содержимое позже.Ключевое слово: NSUserDefaults;)

Проверьте также значения nil:

- (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...
    }
}
...