如何引用文本字段并将其输入到可变数组中

时间:2012-02-29 17:36:12

标签: objective-c object nsmutablearray uitextfield

所以我需要在保存到nsmutablearray的文本字段中输入任何内容(这必须多次发生!每次必须保存为另一个对象)。但无论我做得多清楚都没有用,因为它说“文本”不在结构或联合中。继承我的代码。感谢任何输入!

问题出现在enteredClassText方法中。

#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 arrayWithObject:sender.text];
        [classesEntered = [NSMutableArray alloc] init];
        [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

2 个答案:

答案 0 :(得分:0)

按下按钮时,您应该输入输入的内容,将其转换为字符串,然后将该字符串保存到数组中。这应该很容易实现。以下是一些示例代码:

@interface

NSMutableArray *theArray;

@implementation

- (BOOL)textFieldShouldReturn:(UITextField *)textField {

NSString *theStringToAdd = textField.text;
[theArray addObject:theStringToAdd];

}

请注意我没有测试过这段代码!

答案 1 :(得分:0)

问题在于:

- (IBAction)enteredClassText:(id)sender {
    NSMutableArray *classesEntered = [NSMutableArray arrayWithObject:sender.text];
    [classesEntered = [NSMutableArray alloc] init];
    [classesEntered release];   

}

要解决此问题,请尝试将其引用为

[(UITextField*)sender text]

所以你最终的代码看起来像这样:

 - (IBAction)enteredClassText:(id)sender {
    NSMutableArray *classesEntered = [NSMutableArray arrayWithObject:[(UITextField*)sender text]];
    [classesEntered = [NSMutableArray alloc] init];
    [classesEntered release];   

}

这告诉程序假设发件人将是UITextView。