简体   繁体   中英

Multiple View Controllers using storyboard

I'm new at creating iphone app. I'm trying to create a simple listapp. Basically it will have bunch of list categories and then once the category is clicked it will open a table with bunch of list, you can add items on that list.

I'm using storyboard and I have a few view controllers.

Its compiling with no errors, I can add a category on the first table view controller but when I click the category and tries to add an item I get this error - Thread 1: signal SIGABRT

I'm probably guessing because I didn't initialize the rest of the viewControllers on appdelegate.m

Here's the code that I have for the appDelegate.m

  #import "AppDelegate.h"
#import "ListViewController.h"
#import "List.h"


@implementation AppDelegate {
    NSMutableArray *items;
}

@synthesize window = _window;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    items = [NSMutableArray arrayWithCapacity:20];
    List *item = [[List alloc] init];
    item.title = @"Grocery List";
    [items addObject:item];

    item = [[List alloc]init];
    item.title = @"Project List";
    [items addObject:item];

    item = [[List alloc] init];
    item.title = @"Events List";
    [items addObject:item];

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = [[tabBarController viewControllers]objectAtIndex:0];
    ListViewController *listViewController = [[navigationController viewControllers]objectAtIndex:0];
    listViewController.lists = items;

    return YES;
}


@end

I'm actually a bit confuse on how am I going to initialize the rest of the viewcontrollers on appDelegate.m.

Please help me and thanks in advance

In general, you should not initialize your view controllers in AppDelegate.

In most cases, you can do initialization of your view controllers in their viewDidLoad method.

To apply this to your example above, change your application:didFinishLaunchingWithOptions: method back to the default:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
}

Then go to ListViewController.m file and put all your initialization code for items in viewDidLoad there so that it looks something like this:

- (void)viewDidLoad
{
    [super viewDidLoad];

    items = [NSMutableArray arrayWithCapacity:20];
    List *item = [[List alloc] init];
    item.title = @"Grocery List";
    [items addObject:item];

    item = [[List alloc]init];
    item.title = @"Project List";
    [items addObject:item];

    item = [[List alloc] init];
    item.title = @"Events List";
    [items addObject:item];

    self.lists = items;

}

I'm assuming your ListViewController is a UITableViewController subclass (or a UIViewController with a UITableView in it). You would then implement the UITableViewDelegate and UITableViewDataSoure protocols to populate your tableview. (Take a look at the Table View Programming Guide for iOS to understand how it all works)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM