如何通过alertview实现保存多个数组项的代码?

时间:2012-05-03 04:17:17

标签: iphone ios5

我已经实现了带有textfield的警报视图的代码,当我点击ok按钮时,textfield输入的值以字符串格式存储为(str)并且我将该值添加到数组中以便我将数组加载到表视图,但这里的问题是每次表视图包含一个项目,但我想存储多个项目从警报输入项目并将其保存为数组项目如何实现通过alertview保存多个数组项目的代码任何一个帮我解决这个问题在iPhone的问题。

myAlert = [[UIAlertView alloc] initWithTitle:@"Enter year" 
                                     message:@"alert message" 
                                    delegate:self 
                           cancelButtonTitle:@"Cancel" 
                           otherButtonTitles:@"Ok", nil];
[myAlert addTextFieldWithValue:@"" label:@"entervalue"];     
alertTextField=[myAlert textFieldAtIndex:0];
alertTextField.keyboardType=UIKeyboardTypeAlphabet;
alertTextField.clearsOnBeginEditing=YES;
alertTextField.clearButtonMode=UITextFieldViewModeWhileEditing;
alertTextField.keyboardAppearance=UIKeyboardAppearanceAlert;
[myAlert show];
[myAlert release];


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
    if ([buttonTitle isEqualToString:@"Ok"]) {

        str = alertTextField.text;

        [savedarray addObject:str];
    }
}

3 个答案:

答案 0 :(得分:2)

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
  NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
  if ([buttonTitle isEqualToString:@"Ok"]) {

    str = alertTextField.text;

    [savedarray addObject:str];
    [self.yourtableview reloadData]; // reload your tableview when add new data in array.
  }
}




- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section        {
return savedarray.count;
 }

- (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];
}
cell.textLabel.text = [savedarray objectAtIndex:indexPath.row];    
return cell;
}

希望,这会对你有帮助..

答案 1 :(得分:0)

如果我要求你这样做:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
 NSString *buttonTitle=[alertView buttonTitleAtIndex:buttonIndex];
 if ([buttonTitle isEqualToString:@"Ok"]) 
 {
     str = alertTextField.text;
     if([savedarray Count] < yourRequiredItemsCount)
     {
      [savedarray addObject:str];
      alertTextField = @"";
      [alertView show];
     }
     else
     {
      [alertView release];                //Release the Alert here.
     }
 }
}

告诉我它是否有帮助!会给你一个替代的解决方案。

答案 2 :(得分:0)

如果您正确分配savedarray,那么发布的代码看起来合理。接下来要检查的是你是否正确使用savedarray作为表的数据源。

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return savedarray.count;
}

- (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];
    }
    cell.textLabel.text = [savedarray objectAtIndex:indexPath.row];    
    return cell;
}

确保将相同的视图控制器设置为tableview的委托和数据源。