How do I connect a TableViewController to my main ViewController? - ios

Right now I have:
TableViewController - displays a table that is populated with images and has UITableViewDelegate methods
ViewController- empty except for default viewDidLoad() fn
When I run my program, the screen is blank, presumably because my ViewController has nothing in it.
How do I make it so that it displays my TableViewController?
I have a hunch that I should use prepareForSegue, but am confused because when does prepareForSegue ever get called? I've read it is called before the viewDidLoad(). In that case, should I add a prepareForSegue function in my ViewController that directs to my TableViewController?

Click the ViewController in your storyboard, delete it, and then select your table view controller in the storyboard (click the little yellow icon on the left where the three icons appear right above the controller so that there is a blue outline around the controller). In the Attributes inspector in the utilities panel on the right in Xcode, check "Is Initial View Controller". Sounds like you don't need the other view controller at all.
If you want to segue to the table view controller from your view controller, add a button or some other UI control to the view controller, control-drag from that control to your table view controller, and then select a segue type. prepareForSegue() is called right before segueing; it's a way for you second view controller (in your case your table view controller) to get data that it may need from the first view controller.

You can set the root view controller in the app delegate didFinishLaunchingWithOptions method. Something like:
TableViewController *tableViewController = [[TableViewController alloc] initWithNibName:nil bundle:nil];
self.window.rootViewController = tableViewController;
[self.window makeKeyAndVisible];
should do the trick.

Related

Does the navigation controls and other controls have to be within the appDelegate?

Is it possible to start your project with a single view controller, then on the second or third view controller implement the navigation controller, then maybe on the forth view controller implement the tab view controller? Or does this type of project need to be a storyboard project?
My dilema at the moment is that I start with just one single view controller that has a round rect button that takes you to the second view controller. From the second view controller, I would like a navigation controller with an embedded table view that will take me back and fourth from the second to the third view controller. I've been trying for hours putting the necessary code into each .h and .m file but I keep hitting brick walls.
Thanks in advance.
a. You can of course present several regular view controllers then add a UINavigationController at a later stage. When you need to present the navigation controller, you can embed your detail view controller inside one as follows:
(code is in the view controller which you want to display the detail view controller from)
DetailViewController *detailVC = [[DetailViewController alloc] init];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];
[self presentViewController:detailNav animated:TRUE completion:nil];
b. It is not allowed to have a UITabBarController inside a UINavigationController (or any other view controller). You can however still use the UITabBar control and manage the rest. For an example of this, please refer to UITabBarController inside a UINavigationController.

Why nothing happen when connecting UITableViewCell to UInavigationcontroller

I have UITableView with Static cells. I control-drag from one of these static cells to another UINavigationcontroller and select "Push". I notice nothing happen, when I run the app and tap on the static cells.
However, when I do the same thing & choose "Modal" it works fine. Did I miss something?
You can only use a "Push" transition when segueing between view controllers within a parent UINavigationController.
Your Navigation Controller should be your app's initial view controller. Also when your table cell is tapped you'll want to segue to a View Controller (probably a custom UIViewController subclass), not a UINavigationController.
Make sure you have set your storyboard up with the following:
A UINavigationController - this should be set as the Initial View Controller for your app. Check that the checkbox "Is Initial View Controller" is checked in the Attributes inspector pane when the navigation controller is selected in the storyboard.
A UITableViewController - this should be set at your navigation controller's Root View Controller (there should be an arrow pointing from the nav controller to the tableview controller that looks like a segue but has a different icon in the middle)
Another View Controller - this is the view controller you want to segue to when clicking on your table cell, probably a customer UIVewController subclass you've created. Note that it should probably NOT be a navigation controller.

Can't instantiate new controller

I would like to load a new ViewController/View upon validation of a login username/password. I have the following:
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
...
...
ViewController *Workflow = [self.storyboard instantiateViewControllerWithIdentifier:#"Workflow"];
[self.navigationController pushViewController:Workflow animated:YES];
Then, in the story board, I added a new view controller and slapped a new uiview on there. I then changed the identifier of the ViewController to "Workflow". However, after clicking this button, nothing happens. Any ideas?
I would first NSLog the index of the button sending you the action, it's extremely useful for long alerts, and delegate methods with switch statements. If nothing is happening, then nothing is usually being done.
It sounds like you could be missing a UINavigationController from your storyboard. Unless your view controllers live within a UINavigationController, pushViewController: will have no effect.
If this is the case, what you need to do is:
Drag an instance of UINavigationController from the objects library onto your storyboard. Delete the root view controller that comes with it.
With the Navigation Controller selected on the storyboard, click the checkbox named "Is Initial View Controller" on the Attributes Inspector.
Hold down the Control key on your keyboard and drag with the mouse from the Navigation Controller to your first view controller (presumably the one where you are displaying the alert view) to create a segue. In the Storyboard Segues box that appears after you release, select "Relationship - Root View Controller"

In an iOS 5 Storyboard, how do you push a new scene to the original view controller from a Popover?

I'm creating a popoverSegue from a new view controller and want to push a third view controller onto the original stack. This is how I'm creating the application:
Create a new Single View Application
Select Use Storyboards
Select the MainStoryboard.storyboard file.
Select the only View Controller, change the Title and Identifier to initialView, then select Editor->Embed In->Navigation Controller
Drag two new View Controller objects from the Objects Library onto the canvas
Change the Title and Identifier of the new View Controllers to: popoverView and newView.
Add a Round Rect Button object from the Object Library to initialView and popoverView.
Add a Label object from the Object Library to `newView.
Control click the button in the initialView and drag to popoverView.
Select the Popover option from the Storyboard Segues menu that appears.
Control click the button in the popoverView and drag to the newView.
Select the Push option fromt the Storyboard Segues menu.
Build & Run.
Click the first button, and the popover appears, but when you click the button within the popover, nothing happens (it should push the new view but doesn't.)
What I want to do is for it to push onto the Navigation Controller stack, but am not sure how to setup the storyboard for that.
Any ideas?
You're expecting the UINavigationController hierarchy to extend itself into a presented popover. It won't. The same goes for presenting modal view controllers. If you were to log self.navigationController in popoverView, you would see that it is nil.
Embed popoverView into it's own UINavigationController. Remember that if you're overriding prepareForSegue:sender: and attempting to configure the popover, you will need to get the topViewController from destinationViewController as the destination is now an instance of UINavigationController.
Sounds like it should work, but if it doesn't try the following:
1. Click the segue that doesn't work and give it an identifier. Let's say it's PopoverToNewSegue.
In your implementation file for the popover view controller, add an action when the button is clicked.
That function should return void and add the following line:
[self performSegueWithIdentifier:#"PopoverToNewSegue" sender:self];
That should get your segue running. I've noticed that segues don't always work like you expect them to, but this one works for me without fail.

iPad: Merge concept of SplitViewController and NavigationController in RootView?

I'm having trouble merging the two concepts of using a SplitViewController in my main view and having the "RootView" controller that controls the left panes popup/sidebar table view.
I want to have the left "RootView" act as a navigation menu, but how do I do this when the RootView is tied through MainWindow.xib into the left pane of the SplitView?
Basically, I want the left navigation to work just like the built-in email applications folder drilldown navigation. Is there an example iPad project out there that uses both SplitView and a NavigationView for the left/Root pane?
After you create a SplitView project, open up the RootViewController.m file and look at the -tableViewDidSelectRowAtIndexPath method. You'll see that the item that you clicked is then set as a property of the DetailViewController.
The design you're looking for would require that you push another view controller onto the navigation stack. So if you imagine the e-mail application, when a user picks a folder, the detailView is not updated, but the next level of the Inbox is pushed onto the stack. When a user selects a message from the inbox, the detail view is updated with the message contents, and the RootViewController just stays where it's at.
in the -tableViewDidSelectRowAtIndexPath method, declare your new view controller
NextViewController *nextView = [[NextViewController alloc] initWithStyle:UITableViewStylePlain];
//This assumes you have another table view controller called NextViewController
//We assign it to the instance variable "nextView"
[self.navigationController pushViewController:nextView animated:YES];
//tells the navigation controller to "slide" the "nextView" instance on top
//if animated:NO it wouldn't slide, it would just "update"
[nextView release];
//release the viewController, it's now retained automatically by the NavigationController
Does this make sense?

Resources