Iphone 需要帮助添加用户';s向NSArray发送文本

Iphone 需要帮助添加用户';s向NSArray发送文本,iphone,objective-c,Iphone,Objective C,当用户单击表格的+时,我会提示用户输入文本。它们会显示一个带有textField提示的UIAlert,然后将文本保存为字典中的字符串 到目前为止,我有以下代码: - (void)viewDidLoad { NSArray *myData = [NSMutableArray arrayWithContentsOfFile:@"mydata"]; if (myData == nil) { myData = [NSMutableArray array];

当用户单击表格的+时,我会提示用户输入文本。它们会显示一个带有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中)

例如,用户键入“手臂日”作为提示,然后它保存到数组中,他可以从我存储在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

在.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];
        }
    }

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

[myArray addObject:entered];

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

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

因此,您可以在头文件中声明:

@property(非原子,保留)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];

希望这能帮到你

这是行不通的,praveen还没有读过你的代码-你需要将数组声明为属性或使其成为全局变量。@lxt-当我说“使其成为类的一部分”时,这意味着你将其声明为具有属性retain的类变量,以便可以在方法中访问它。你看过答案了吗?显然,这个家伙在他的didload方法中有这个变量,但不知道如何在警报视图回调中使用它。谢谢lxt,我感谢你的回答。但是我在viewDidLoad中使用了if方法,并希望保留它,这就是为什么我使用数组作为局部变量。您仍然可以在
viewDidLoad
中保留数组赋值。全局变量仅仅意味着可以通过类中的任何方法访问数组。如果使用局部变量,它只能在
viewDidLoad
中使用,这对您没有任何好处。我认为应该分配myData,否则它将不会存储数据。好的,谢谢。最后一件事,这不应该填充我的表吗<代码>cell.textLabel.text=[self.routineArray objectAtIndex:indexPath.row]
应该可以-我不知道routineArray是什么,但只要它包含可以工作的NSStrings。在myData中保存任何数据之前,必须分配nsmutable数组。并且它们在myData=[NSMutableArray arrayWithContentsOfFile:@“myData”]中不应为星形@伊克巴尔·汗:是的,你是对的,请看我更新的答案,谢谢。我试过了,但当我登录时,我从我的数组中得到了空值it@Faisal:您将NSLog放在哪里了?这是我的NSLog:NSLog(@“%@,[myArray objectAtIndex:0])。我将它放在方法中,用于:-(void)alertView:(UIAlertView*)alertView将使用ButtonIndex(NSInteger)解除ISSISSbuttonIndex@Faisal:创建“routineArray”的代码在哪里?啊,可能是错误地删除了它。让我把它添加回viewdidload.ok,这样我就得到了NSLog来输出数组,它看起来是正确的,但是表没有用任何数据更新。这是我的单元格文本代码:
cell.textlab.text=[self.myArray objectAtIndex:indexath.row]因此,当我从NSLog(@“%@,[myArray objectAtIndex:0])检查的NSCode时;我只得到输入到文本字段的第一件东西。如果我输入第二个或第三个内容,NSlog仍然只显示第一个。请使用以下修改的函数-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return 1;}-(NSInteger)tableView:(UITableView*)tableView numberofrowsinssection:(NSInteger)section{return[myArray count];}