interact the navigation controller bar button with embed container view - iphone

I've created a UIViewController, and insert a container view in it, which embed a UITableViewController. As the image described above.
When user click the Table View Cell, I'd like to add a UIBarButton on the Navigation bar.
But, how can I manage this? I can rise the DatePicker when click on Table View Cell, but when I call self.presentingViewController in table view controller implementation file, it returns (null), same as when I call self.parentViewController

You're probably trying to access the parent controller too early. If you log self.parentViewController in viewDidLoad, it will return null, but it should return the correct controller from viewDidAppear, and certainly from the didSelectRowAtIndexPath method. Using parentViewController in this context is correct, not presentingViewController.

I'd suggest to implement the UITableViewController Delegates and Datasource Methods in the ViewController itself.
That way you don't have to worry about accessing the ViewController containing the UITableView.

Related

Manuel segue 'Show' does not call ViedDidload when returning to parent viewController?

I have a viewController containing a UITableViewController. This UITableViewController is populated by an array. When User presses populated cells they segue with a segue set to 'Manuel Segue : Show' this creates a back button UINavigation bar item. Inside this viewController the user is able to add Items to the array populating the parent UITableViewController. The problem is that when i segue back using the UINavigationbarItem it does not call viewDidload on the UITableViewController, there by not updating the UIViewTableCells. I have to close application to make it call viewDidload... How do i make it call ViewDidload when returning from the Manuel segue show? All help appreciated.
The pushing view controller is not unloaded when another view controller is shown above. As viewDidLoad: is only called once in the view lifecycle, this would then not be called when the segue is unwound.
Updating your tableview in viewWillAppear: or viewDidAppear: would cause this to be called whenever this view is displayed.
viewWillAppear would perhaps be better if you don't want to show the user the table reloading when not needing to asynchronously load data.
Read more on iOS view controller lifecycle here:
https://developer.apple.com/library/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html#//apple_ref/doc/uid/TP40015214-CH6-SW3

Replace Window rootViewController with another view controller using Storyboard

I am new to Storyboard-ing, been using IB mostly. I have an initial temporary View Controller (let's call it BootstrapViewController) which job is solely to load data from a webservice, then hand over control to a UITabViewController. The data is required to determine what tab items to show.
In non-Storyboard world, I just do:
// Do whatever you need to UITabViewController, then call this
[UIApplication sharedApplication].keyWindow.rootViewController = myTabVC;
Is this possible using Storyboard? I tried creating a named segue from BootstrapViewController to the tab view controller, then calling performSegueWithIdentifier, no luck.
You can do it exactly the same way you have always done it, by setting the window's rootViewController property. You can even load the tab bar out of a storyboard. Use UIStoryboard's instantiateViewControllerWithIdentifier: to load a particular view controller from a storyboard.

pushViewController from Prototype Cell

Quite simply, how do I push a new view controller from a prototype cell within a storyboard? I have a tableView reading data from a plist file, and its one default prototype cell on the storyboard. Then, I have another view controller in the storyboard I need to link to the tapping of a cell on the table view. How can this be accomplished?
Simplest thing would be to Ctrl-click on the prototype cell and drag the line onto the view controller that you want to transition to. It should prompt you for the type of transition—"push" transitions cause the new view controller's view to slide in from the right; "modal" transitions cause the new view to slide in from the bottom. Assuming you're using a navigation controller, the navigation bar should configure the back button automatically. You shouldn't need to write any code to achieve the behavior described in your question.
In your storyboard, link your prototype table view cell to your view
controller with a segue. Make sure to give the segue an identifier in
its Attributes Inspector.
In your TableViewController class,
implement - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
In the prepareForSegue: method, get the NSIndexPath of the row the user tapped with [self.tableView indexPathForSelectedRow]
Use the indexPath to get whatever data you need from your plist. Configure the view controller by using the destinationViewController property of the UIStoryboardSegue parameter passed into prepareForSegue:.

How do I add a Navigation Bar to a UITableViewController in Interface Builder?

Interface builder does not let me click and drag a Navigation Bar onto a Table View Controller!!! It is super frustrating.
All I want is a table view with an edit button (done in interface-builder). If this is not possible, then how do I add a navbar progammatically?
From the outline view, make sure your Table View Controller is selected.
Then go to the Editor menu, and click on the Embed In submenu, and choose Navigation Controller and voila. You have your navigation controller pointing to your tableview controller with a relationship built in.
For a table view with an edit button at the top, use a UINavigationController, with a UITableView as the rootView. That means you're going to make a custom UITableView subclass for your table view, and use that as the rootView of your UINavigationController instance. (Programatically, it's set with UINavigationController's -(id)initWithRootViewController. It's also settable through IB.)
Then, in your UITableView subclass, uncomment the following line:
- (void)viewDidLoad {
[super viewDidLoad];
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
and voilĂ , your UINavigationController's view shows up as a table view with an edit button on the right side of the navigation bar.
Since the controller is at the top of the stack, there's no "back" button on the left, so you can use self.navigationItem.leftBarButtonItem for whatever UIBarButtonItem you create.
I agree that it's difficult to figure out how to do things like this in Interface Builder, but luckily it is possible to add a Navigation Bar and Bar Button Item to a Table View this way. Here's how to do it:
Drag a blank View (an instance of UIView) from the Library to the area near the top of the Table View. As you drag near the target area, Interface Builder will highlight it in blue to show you where to drop the View. Let go, and the View will be added as a subview of the Table View's header view.
Drag a Navigation Bar from the Library and drop it on the blank View you just added.
Drag a Bar Button Item from the Library and drop it onto the Navigation Bar.
EDIT
The problem with the above approach is that, as Bogatyr points out, the Navigation Bar will then scroll along with the Table View. Apple recommends using a custom subclass of UIViewController that owns both the Navigation Bar and an instance of UITableView resized to fit. Unfortunately, that means you would have to implement the UITableViewController behavior needed by your UIViewController subclass yourself.
Another approach that seems to work well is to create a custom subclass of UIViewController that owns a blank background view containing the Navigation Bar as well as a blank content view (an instance of UIView) that fits under the Navigation Bar. Your custom subclass would have an outlet pointing to an instance of UITableViewController in the same nib file.
This has the advantage of allowing all the view components to be created and configured in Interface Builder, and doesn't require implementing UITableViewController methods from scratch. The only detail you'd need to take care of in the Table View Controller's parent would be to add Table View as a subview of the parent's content view in viewDidLoad.
The parent could implement the action methods for the Navigation Bar's button items, and implement the delegate pattern if necessary.
From iOS6 onwards, you can use container view. So what you have to do is take View controller, add the navigation bar to it, then add a Container View to same view controller. It will automatically, add the new view controller link to your container view. Now simply delete that, and your table view controller in the story board. Now embed the table view controller to container view by control drag. Hope it helps.
First add a navigation controller and put the table view controller (as root view controller) onto the navigation controller. This is how it is done in Code because I don't use IB.
Why in the world you can't drag a navigationItem into a .xib file with File's Owner set to a subclass of UIViewController and hook the navigationItem up to the UIViewController's navigationItem outlet is beyond me. It seems like a real hole in IB / XCode integration. Because you can certainly drag an instance of ViewController to a xib file, and drag a navigationItem into the ViewController, and then set the title and barbuttonitems that way.
So if you want to define your UITableViewController subclass object's navigation bar in IB, you have to create your TableVC object in a xib file (not the one .xib file that contains the tableview for your UITableViewController, though!). You then either hook the TableVC object up to be an outlet of another object (like your application delegate), which works if you need just one instance of your TVC throughout the lifetime of your app, or if you want to dynamically create instances of your TableVC in code you load this extra .xib file manually via loadNibNamed:owner:options method of the NSBundle class.
These steps worked for me in iOS 9:
Add a View Controller to the Storyboard. Make UITableViewController as base Class.
Add a Navigation Bar object onto view controller at the top.
Add a Table View below Navigation bar.
Add a Table View Cell into Table View.
Add constraints.
This is the other easy way ;
Choose your TableViewController screen on storyboard.
Click Size Inspector symbol on the right menu and find Simulated Size
Change Fixed to Free Form
You can add navigation bar easily.

How do I have a Tableview go to another tableview

Right now I have an indexed tableview that goes to a detail view but i want it to go to another tableview then a detail view.
Any thoughts?
Use a UINavigationController with the first table view as its root view controller.
Implement the tableView:didSelectRowAtIndexPath: method of the table view's delegate to create the second table view (from a nib or programmatically, doesn't matter, as long as it's an instance of UITableViewController), then call the navigation controller's pushViewController:animated: method with the new controller.
Then, from the second controller, present your detail view as you are doing so already.
See also:
Using Navigation Controllers
Navigating a Data Hierarchy with Table Views
SimpleDrillDown sample project (may require registration)
The same way you would go to a detail view, but instead of transitioning to a view transition to a TableView.