开发者

Need Help Adding User's Text To NSArray

开发者 https://www.devze.com 2023-02-18 23:15 出处:网络
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 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]);
    }
}

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

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).

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

Update with new code:

#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;
}

Update 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];
        }
    }
    N开发者_StackOverflowSLog(@"%@",[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;
}


Suggest you to save the user text in NSMutableArray.

Declare NSMutableArray variable in your .h file,

  NSMutableArray *myData;

In Source file .m:

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

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

Add text in Array.

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


Neither of the current answers are very helpful, because they haven't looked at your code! Just doing addObject won't help you, in fact your code probably won't compile if you add it.

Your array is a local variable, so it's only available inside the viewDidLoad method. If you try to add anything to it inside yourwillDismissWithButtonIndex method without changing how you're declaring your array you'll cause a compiler error. The first step is to make your myData variable available to all your methods. You can use a property to achieve this - if you're unsure how to declare a property you should think about perhaps stepping back and reading up a bit more on Objective-C before diving in.

So, in your header file you might declare:

@property (nonatomic, retain) NSMutableArray *myArray;

And in your implementation file:

@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];

Hope that helps you out!


Make your array as part of your class and use its reference in your method to add it to array.

[myArray addObject:entered];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号