【问题标题】:How do you reference a textfield and enter it into a mutable array如何引用文本字段并将其输入可变数组
【发布时间】:2012-02-29 17:36:12
【问题描述】:

所以我需要将在文本字段中输入的任何内容保存到 nsmutablearray 中(这必须发生多次!并且每次都必须保存为另一个对象)。但无论我做什么显然都不起作用,因为它说“文本”不在结构或联合中。这是我的代码。感谢您的任何意见!

问题在于输入的ClassText 方法。

#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

【问题讨论】:

    标签: objective-c object nsmutablearray uitextfield


    【解决方案1】:

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

    @interface
    
    NSMutableArray *theArray;
    

    @implementation
    
    - (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    NSString *theStringToAdd = textField.text;
    [theArray addObject:theStringToAdd];
    
    }
    

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

    【讨论】:

    • 不应该是:[theArray addObject:theStringToAdd]; ?
    • 如果您想关闭键盘,请不要忘记 [textField resignFirstResponder]。
    【解决方案2】:

    问题出在这里:

    - (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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多