Jakub
Jakub

Reputation: 13860

Create new views programmatically and manage them

I'm new in Objective-C and I'm start to work on app that will have a few View's. I want to create them 100% programmatically without XIB files. I understand that I have to create my ViewControllers classes for my screens, but I want to ask you how to manage a navigation with it. Let's say that I have one ViewController which contains a tableView. And i want to create a next screen. So I understand that row in tableViewController class calling

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { //... }

But what about next screen? I don't have it yet. Should I create a "ready" ViewController class? How to handle a navigation in this case?

Upvotes: 1

Views: 362

Answers (3)

Yusuf X
Yusuf X

Reputation: 14633

I hate IB, and have gone to some trouble to attempt to make apps without it, however it is so embedded into the workflow, that I found it easier just to grit my teeth and use it. However, there are posts that describe, to varying extent, how to develop without IB, such as:

Upvotes: -1

Arcank
Arcank

Reputation: 456

Make sure you read the doc from Apple: View Controllers Programming Guide

Basically, you will have to embed your table view controller (being a content view controller) in a navigation controller (container view controller).

Then in the -tableView:didSelectRowAtIndexPath:, you will instantiate a new content view controller, and push it on your current table view controller, via the navigation controller with this kind of message:

[[self navigationController] pushViewController:<#myNextViewController#> animated:YES];

Be sure to read also code samples from Apple (some are pretty thin, so it's easy to understand).

Upvotes: 2

Kemenaran
Kemenaran

Reputation: 1441

In your case, when a cell is selected, you would:

  • Create a new instance of your next view controller,
  • Push this new instance on the UINavigationController stack.

So, first, you need to ensure that your first view controller (the one with the table view) is contained in an UINavigationController.

// AppDelegate, in applicationDidFinishLanching:

UIViewController *firstViewController = [[[MyCustomTableViewController alloc]
                                          initWithNibName:nil bundle:nil]
                                         autorelease];
UINavigationController *navigationController = [[[UINavigationController alloc]
                                                 initWithRootViewController:firstViewController]
                                                autorelease];

[self.window setRootViewController:navigationController];

Then, when a cell in your table view is selected, you can write:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIViewController *nextViewController = [[[MyNextViewController alloc]
                                             initWithNibName:nil bundle:nil]
                                            autorelease];

    [self.navigationController pushViewController:nextViewController
                                         animated:YES]

}

Upvotes: 3

Related Questions