iphone - 从 AppDelegate 将 NSMutableArray 添加到第二个选项卡中的 TableView

标签 iphone ios objective-c xcode ios6

按照以下教程我没有遇到任何问题:http://www.raywenderlich.com/1797/how-to-create-a-simple-iphone-app-tutorial-part-1 .本质上,本教程教人们创建一个简单的 TableView ,其中填充了 NSMutableArray 中的数据,然后深入到每个项目的 DetailView。

然后,我尝试通过创建 TabViewController 并将创建的 TableView 作为该 Controller 的“选项卡项 2”来增强我所拥有的功能。

这破坏了表的自动填充。

用于填充AppDelegate.m中的表的代码

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    Person *person1 = [[Person  alloc] initWithName:@"Monica"
                                            balance:420
                                         thumbImage:[UIImage imageNamed:@"01.jpg"]
                                          fullImage:[UIImage imageNamed:@"01_t.jpg"]];
    Person *person2 = [[Person  alloc] initWithName:@"Ross"
                                            balance:630
                                         thumbImage:[UIImage imageNamed:@"02.jpg"]
                                          fullImage:[UIImage imageNamed:@"02_t.jpg"]];
    Person *person3 = [[Person  alloc] initWithName:@"Rachel"
                                            balance:420
                                         thumbImage:[UIImage imageNamed:@"03.jpg"]
                                          fullImage:[UIImage imageNamed:@"03_t.jpg"]];

    NSMutableArray *persons = [NSMutableArray arrayWithObjects:person1, person2, person3, nil];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController *initialViewController = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:@"RootViewController"];
    self.window.rootViewController = initialViewController;
    TableViewController * peopleTableViewController = [storyboard instantiateViewControllerWithIdentifier:@"PersonTableViewController"];
    [peopleTableViewController setPersons: persons];

    // Override point for customization after application launch.
    return YES;
}

'persons' 是 Person 实例的数组,它是 TableViewController 的属性。以下是基于 TableViewController.m 中的“persons”数组的表格人口:

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

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


    //For each person
    Person *person = [self.persons objectAtIndex:indexPath.row];
        cell.textLabel.text = person.name;
        cell.imageView.image = person.thumbImage;
        cell.detailTextLabel.text = [person getPrice];

    return cell;
}

最后, Storyboard位于此处:http://i.stack.imgur.com/9axs8.png

重申一下,这一切都编译得很好,但在测试过程中,打开选项卡项目 #2 时表为空(其中 TableView Controller 按照 Storyboard存在)。在我添加选项卡之前,这是有效的。

我对 Objective C 还很陌生,所以如果您需要任何其他信息,请告诉我。

最佳答案

您的问题是您正在实例化新的 View Controller ,而不是使用 Storyboard中已有的 View Controller 。当此应用程序启动时,选项卡栏 Controller 及其内容 View Controller 都将被实例化。另外,由于 Controller 之一(选项卡 2 中的 Controller )是导航 Controller ,因此其 Root View Controller TableViewController 也将被实例化。因此,您只需要获取对此 TableView Controller 的引用,而不是创建新的:

    UITabBarController *tabVC = (UITabBarController *)self.window.rootViewController;
    UINavigationController *nav = tabVC.viewControllers[1];
    TableViewController *peopleTableViewController = nav.topViewController;
    [peopleTableViewController setPersons:persons];

这应该可行,但我认为最好将 Person 对象和 Persons 数组的创建移至 TableView Controller 的 viewDidLoad 方法。

关于iphone - 从 AppDelegate 将 NSMutableArray 添加到第二个选项卡中的 TableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15050795/

相关文章:

ios - 如何从 iOS 上的硬件键盘检测按键向上/按键按下事件?

iphone - 记录额外生命物体的仪器

ios - 将字符串转换为 NSDecimalNumber

iphone - Game Center 对接会 GKTurnBasedMatch 有明显滞后(约 1 分钟)

iOS -- 使用分配和内存监视器(无物理内存)来使用和理解仪器

ios - 如何使用 ARC 释放 block

objective-c - iOS:发送短信

iphone - 安装应用程序时的额外参数/配置

ios - NSMutableArray 索引 9223372036854775807 越界

iphone - 是否可以通过编程方式获取 iPhone 名称顺序首选项?