需要帮助将用户文本添加到NSArray [英] Need Help Adding User's Text To NSArray

查看:42
本文介绍了需要帮助将用户文本添加到NSArray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am prompting users for text when they click the + for my table. They are shown a UIAlert with textField prompt and the text is then saved as a string in a dictionary.

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

I have the following code so far:

- (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显示输入的文本,但是如何将其保存在阵列中?

The NSlog shows the entered text but how do I get it to save in my Array?

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

Also, should I save the text as a string, a dictionary with a string in it, or an array itself with a string in it? Because there is going to be a dictionary or array within each of these other dictionaries with objects that the user can choose from (the objects they can choose from are stored in a separate plist).

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

For example, User types in "Arms Day" for the prompt, then it saves to the array and he can choose from all the exercises for the arm that I have stored in a 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;
}

推荐答案

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

Suggest you to save the user text in 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];
        }
    }

这篇关于需要帮助将用户文本添加到NSArray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆