【问题标题】:Need Help Adding User's Text To NSArray需要帮助将用户的文本添加到 NSArray
【发布时间】:2011-03-23 07:56:00
【问题描述】:

当用户单击我的表格的 + 时,我会提示他们输入文本。它们会显示一个带有 textField 提示的 UIAlert,然后将文本保存为字典中的字符串。

到目前为止,我有以下代码:

- (void)viewDidLoad
{
    NSArray *myData = [NSMutableArray arrayWithContentsOfFile:@"mydata"]; 

    if (myData == nil)
    {
      myData = [NSMutableArray array];  
    }

    [super viewDidLoad];

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)showPrompt
{
    AlertPrompt *prompt = [AlertPrompt alloc];
    prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];

    [prompt show];
    [prompt release];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        NSLog([NSString stringWithFormat:@"%@", entered]);
    }
}

NSlog 显示输入的文本,但我如何将其保存在我的数组中?

另外,我应该将文本保存为字符串、包含字符串的字典还是包含字符串的数组本身?因为在每个其他字典中都会有一个字典或数组,其中包含用户可以选择的对象(他们可以选择的对象存储在单独的 plist 中)。

例如,用户在提示中输入“Arms Day”,然后将其保存到数组中,他可以从我存储在 data.plist 中的所有手臂练习中进行选择

使用新代码更新:

#import "RoutineTableViewController.h"
#import "AlertPrompt.h"

@implementation RoutineTableViewController
@synthesize routineArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    /*
    NSArray *myData = [NSMutableArray arrayWithContentsOfFile:@"mydata"]; 

    if (myData == nil)
    {
      myData = [NSMutableArray array];  
    }
    */

    [super viewDidLoad];

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)showPrompt
{
    AlertPrompt *prompt = [AlertPrompt alloc];
    prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];

    [prompt show];
    [prompt release];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        NSLog([NSString stringWithFormat:@"%@", entered]);
        [self.routineArray addObject:entered];    }
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // do delete
        // get count of rows in UITableView and hide table if it's empty
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [routineArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [self.routineArray objectAtIndex:indexPath.row];

    return cell;
}

更新 3:

@implementation RoutineTableViewController
@synthesize myArray;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    myArray = [[NSMutableArray alloc] init]; 
    myData = [[NSMutableArray arrayWithContentsOfFile:@"mydata"] retain]; 

    if (myData == nil)
    {
        myData = [NSMutableArray array];  
    }

    [super viewDidLoad];

    UIBarButtonItem * addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showPrompt)];
    [self.navigationItem setLeftBarButtonItem:addButton];
    [addButton release];

    self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

-(void)showPrompt
{
    AlertPrompt *prompt = [AlertPrompt alloc];
    prompt = [prompt initWithTitle:@"Add Workout Day" message:@"\n \n Please enter title for workout day" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Add"];

    [prompt show];
    [prompt release];
}

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        NSLog([NSString stringWithFormat:@"%@", entered]);
        //if(myData && entered)
        {
            [myArray addObject:entered];
        }
    }
    NSLog(@"%@",[myArray objectAtIndex:0]);
    [tableView reloadData];
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        // do delete
        // get count of rows in UITableView and hide table if it's empty
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [myArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];

    return cell;
}

【问题讨论】:

  • @Faisal:创建“routineArray”的代码在哪里?
  • 啊,可能会误删。让我把它重新添加到 viewdidload 中。
  • 好的,所以我让 NSLog 输出数组,看起来是正确的,但表没有更新任何数据。这是我的单元格文本代码:cell.textLabel.text = [self.myArray objectAtIndex:indexPath.row];
  • 所以当我检查来自 NSLog(@"%@",[myArray objectAtIndex:0]); 的 NSCode 时我只得到我输入到文本字段的第一件事。如果我输入第二个或第三个东西,NSlog 仍然只显示第一个。
  • 请使用以下修改后的函数 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [myArray count]; }

标签: iphone objective-c


【解决方案1】:

建议您将用户文本保存在NSMutableArray。

在 .h 文件中声明 NSMutableArray 变量,

  NSMutableArray *myData;

在源文件 .m 中:

 myData = [[NSMutableArray arrayWithContentsOfFile:@"mydata"] retain]; 

    if (myData == nil)
    {
      myData = [NSMutableArray array];  
    }

在数组中添加文本。

if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        NSLog([NSString stringWithFormat:@"%@", entered]);
        if(myData && entered)
        {
              [myArray addObject:entered];
        }
    }

【讨论】:

  • 您必须先分配 nsmutable 数组,然后才能在 myData 中保存任何数据。他们不应该在 myData = [NSMutableArray arrayWithContentsOfFile:@"mydata"];
  • @iqbal Khan:是的,你是对的,请看我更新的答案,谢谢
  • 我试过了,但是当我 NSLog 时,我的数组为空
  • @Faisal:你把 NSLog 放在哪里了?
  • 这是我的 nslog:NSLog(@"%@",[myArray objectAtIndex:0])。我把它放在方法中: - (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
【解决方案2】:

当前的答案都不是很有帮助,因为他们没有查看您的代码!只是做addObject 对你没有帮助,事实上,如果你添加它,你的代码可能无法编译。

你的数组是一个局部变量,所以它只能在viewDidLoad 方法中使用。如果您尝试在willDismissWithButtonIndex 方法中添加任何内容而不更改声明数组的方式,则会导致编译器错误。第一步是使您的myData 变量可用于您的所有方法。您可以使用一个属性来实现这一点 - 如果您不确定如何声明一个属性,您应该考虑在深入研究之前退一步并阅读更多关于 Objective-C 的内容。

所以,你可以在你的头文件中声明:

@property (nonatomic, retain) NSMutableArray *myArray;

在你的实现文件中:

@synthesize myArray
- (void)viewDidLoad
{
    self.myData = [NSMutableArray arrayWithContentsOfFile:@"mydata"]; 

....


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex != [alertView cancelButtonIndex])
    {
        NSString *entered = [(AlertPrompt *)alertView enteredText];
        [self.myArray addObject:entered];

希望能帮到你!

【讨论】:

  • 谢谢lxt,感谢您的回答。但是我在 viewDidLoad 中有 if 方法并想保留它,这就是我使用数组作为局部变量的原因。
  • 您仍然可以将数组分配保留在viewDidLoad 中。全局变量仅意味着可以通过整个类中的任何方法访问该数组。如果你使用局部变量,它只能在 viewDidLoad 中使用,这对你没有任何好处。
  • 我认为应该分配 myData 否则它不会存储数据。
  • 好的,谢谢。最后一件事,这不应该填充我的桌子吗? cell.textLabel.text = [self.routineArray objectAtIndex:indexPath.row];
  • 应该做 - 我不知道routineArray是什么,但只要它包含可以工作的NSStrings。
【解决方案3】:

将您的数组作为您的类的一部分,并在您的方法中使用它的引用将其添加到数组中。

[myArray addObject:entered];

【讨论】:

  • 这行不通,praveen 没有读过你的代码 - 你需要将你的数组声明为属性或使其成为全局变量。
  • @lxt - 当我说让它成为类的一部分时,这意味着您将其声明为具有属性保留的类变量,以便可以在方法中访问它。你读过答案吗?显然,这家伙在他的 didload 方法中有变量,并且不知道如何在警报视图回调中使用它。
猜你喜欢
  • 1970-01-01
  • 2021-06-22
  • 1970-01-01
  • 2021-06-28
  • 1970-01-01
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 2017-07-01
相关资源
最近更新 更多