Split view controller menu in w/o split view controller? - iphone

I am making an iPad app, and am wondering it is possible to get the pop down menu from a UINavigationBar without having to go through the trouble of a split view controller. Is this possible? Tell me if I'm not being specific enough.

Yes, you can do that without much trouble, but you just have to write the code. Just display a UIPopoverController from that UIBarButtonItem on your navBar.
The steps:
Create a UIViewController which manages a table view (or whatever else you want) as your menu view controller.
Add a UIBarButtonItem to your nav bar or toolbar.
Create an IBAction to called something like touchedMenuButton.
Connect that action to that UIBarButtonItem.
In that method, alloc/init that view controller.
alloc/init a UIPopoverController with that view controller.
present that popover from the UIBarButton item
Success!

Related

Trying to add an Edit button to the navigation bar

I had created a xcode project of single view application. I put a tableview and navigation item on my storyboard screen. Now, I bind the array data source to tableview and set the code as below to show edit button on the navigation button.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
However, it seems can't working. I can't find any button on my navigation bar.
Someone can tell me how to make the edit button show?
Thanks!
I put a tableview and navigation item on my storyboard screen.
It sounds like maybe that's the problem. The way to set this up is to start with a UINavigationController as your storyboard's initial controller, and put the UITableViewController in a relation segue from that (as the navigation controller's root view controller). Now if the table view controller says
self.navigationItem.leftBarButtonItem = self.editButtonItem;
the edit button will appear in the navigation bar.
To see an example, start with the Master-Detail template (for iPhone). The Master view controller is structured in the way I'm describing.

Put common code for UINavigationBar in UINavigationController

In my iPhone project, I have a navigation view controller. In each view that is loaded by this controller, I am setting buttons in the UINavigationBar that are doing different things for each view.
However, I want to have the .rightBarButtonItem do exactly the same thing each time (namely, pop up a UIActionSheet). How can I centralize this code and not have to put it in every view controller?
I tried subclassing UINavigationController and setting the .rightBarButtonItem in this subclass' viewDidLoad. However, no button is displayed then. (But when I put the same code in a view controller loaded by the navigation controller, the button is displayed and works fine).
The code I am using to set the rightBarButtonItem is:
self.navigationItem.rightBarButtonItem = ...
Subclass all the UIViewControllers that are pushed onto that UINavigationController and add the same viewDidLoad code.

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.

iPhone - Tweak the UINavigationController to show a UINavigationBar made into IB

I've build a UINavigationBar into Interface Builder, and I have a NavigationController into my app. I'd like to make the one use the other to work. Just to manage the bar into IB and let the controller use it as its view (and adding by itself the Back button if needed), or in another way to do the same thing, let the NavBar use the navcontroller to adjust its display.
Do you see a way to do this ?
If not, I really don't see the use of the NavigationBar proposed into IB.
If you create the view controller in IB, you can give it a navigation item (UINavigationItem), and put your buttons in there. If you only create the view in IB and the controller is the owner (you use initWithNibName:bundle:), then you will either have to create the items programatically or put a outlet named navigationItem in your custom controller and connect it to a navigation item in the nib.

Switching Views within UITabBar View

I have created an UITabView application. Each view selected from the bar is a seperate controller with own nib file. I switch between them succesfully.
In the first view I have two buttons (check out the screenshot). When clicking them I want to switch to another views which are the parts of the current view controller. I use:
[self presentModalViewController:anotherViewController animated:NO];
That switches the view, but hides the UITabBar. How to keep the bar on the screen after the switch?
P.S. Sorry for the blurred image. I am not allowed to share to much info.
Well I think you are misusing the modal view controller. For a problem like this I'll say you should put them in a view controller stack using UINavigationController. Instead of making each tab a UIViewController make it a UINavigationController, then you can push and pop view controllers on it, which still show the tab bar.
See http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UINavigationController_Class/Reference/Reference.html
use: tabBarController.selectedViewController = newViewController
edit: UINavigationController is not needed here.