-1

我是 iPhone 开发者。

我有一个UITableView,但我无法使它与一个NSMutableArray.

我已经分配并初始化了我的数组:dataArray我还连接了我的 tableviewdataSourcedelegate.

这是我的代码:

SecondViewControllerModal.m

@implementation SecondViewControllerModal

-(IBAction)addArray:(id)sender
{
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil];
    SecondViewController *c=[[SecondViewController alloc] init];
    [c addToArray:content];
}

...

addArray当用户按下按钮时调用)

第二视图控制器.m

-(void)addToArray:(NSArray *)content {

    NSString *setSubject = [content objectAtIndex:0];
    NSString *setLetter = [content objectAtIndex:1];
    NSString *setOrder = [content objectAtIndex:2];

    [dataArray addObject:setSubject];

    [tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [dataArray count];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self->tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier];
    }

    // Configure the cell...
    cell.textLabel.text = [dataArray objectAtIndex:indexPath.row];


    return cell;
}

注意:我正在使用故事板和选项卡式视图应用程序,如果有什么不同,我只需要在 iPad 上运行它。

如果我没有提供足够的详细信息,或者您对代码有任何疑问,请发表评论。

编辑:这就是我的弹出框在 xcode 中的样子:

4

1 回答 1

1

每次按下“圆形矩形”按钮时,您都在创建一个新的second view controller,向其传递一些数据,要求它做某事,然后将其丢弃。

您需要更新addArray:方法以调用现有的.addToArray: second view controller

second view controller您可能应该通过在由于按下条形按钮而显示模式时传递对模式的引用来做到这一点。

在 SecondViewControllerModal.h 中:

@property (weak, nonatomic) SecondViewController *owningSecondViewController;

在 SecondViewController.m 中:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"..."]) {
        SecondViewControllerModal *modalViewController = (SecondViewControllerModal *)segue.destinationViewController;
        modalViewController.owningSecondViewController = self;
    }
}

最后:

- (IBAction)addArray:(id)sender
{
    NSArray *content = [[NSArray alloc] initWithObjects:subjectName.text, setLetter.text, setOrder.text , nil];

    [self.owningSecondViewController addToArray:content];
}
于 2013-05-01T20:40:30.480 回答