0

好的,所以我想将一个视图控制器中表格单元格中包含的名称传递给另一个视图控制器中的数组。我想通过一个也包含在第一个表的单元格中的按钮来执行此操作。

以下是描述表格单元格的地方:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

// add "add" button
UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeContactAdd];
addFriendButton.frame = CGRectMake(250.0f, 5.0f, 75.0f, 30.0f);

[cell addSubview:addFriendButton];

[addFriendButton addTarget:self
                    action:@selector(addFriend:)
                    forControlEvents:UIControlEventTouchUpInside];

if(!cell)
{
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;

}

这里是描述按钮方法的地方:

- (IBAction)addFriend:(id)sender
{
    NSLog(@"Add friend.");



}

第一个视图控制器的名称 - 执行传递的控制器 - 称为 ViewController - 而第二个视图控制器 - 接收传递信息的控制器 - 称为 MyMealViewController。我希望将传递的信息存储在其中的数组称为 myMenuArray。

我认为这基本上是所有相关信息。如果您需要我提供任何其他信息 - 或者如果您需要澄清我提出的问题 - 为了使我的问题可以回答,请告诉我!

4

1 回答 1

0

您可以使用

cell.textLabel.text = [array objectAtIndex:indexPath.row];

[[addFriendButton addTarget:self
                        action:@selector(addFriend:) toTarget:self      withObject:cell.textLabel.text]
forControlEvents:UIControlEventTouchUpInside];

- (void)addFriend:(NSString *)theFriendName 
{
   NSLog(@"Add friend.");
// Now here you make the instance of the new ViewController and you set the text you are     adding.
MyViewController *theVC = [MyViewController alloc] initWithNibName:@"MyViewController"     bundle:nil];
[theVC setNewFriend:theFriendName];
[self presentModalViewController:theVC animated:YES];

}
于 2012-12-03T03:11:57.947 回答