TableView To Navigation Controller on the View-Based Application Project? - iphone

I built an application. On the one my views I used TableView. So now I want to change this Table view to a navigation controller.
1- How can I change UITable view to Navigation Controller.؟

The easiest way to achieve this is to create a new 'Navigation-based Application'. This will set up everything you need. By default this will set you up with simple RootViewController, if you want you can change this to be your TableViewController by editing the application delegate
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after app launch
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
All you need to do is copy your TableViewController into the new project and change the RootViewController to be yours

If you have started with one of the template project in XCode find the .xib file for your view and launch InterfaceBuilder by double clicking the .xib file. You can manipulate the view directly by dragging and dropping components as well as adding components from a palette.
I would walk through one of Apples tutorials and get a feel for working with InterfaceBuilder - this tutorial shows adding a view controller.

Thank you everybody . finally i figured out :D , just put this code :
UINavigationController *myNav=[[UINavigationController alloc] initWithRootViewController:[[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil]];
[self presentModalViewController:myNav animated:YES];

Related

Creating a multiview application without storyboards?

I'm a beginner leearning Xcode, and I created a simple single view application for a pretty useless card game (it's not even really a game).
What I want to know is is it possible to import my single view application into an application with storyboards without having to make a new project and retyping all of the code and connections?
Or is it possible to make a multiview application without storyboards, that I could continue off in the same project?
If so, can anyone direct me to a resource to do so? Thanks.
You can make a multiview application without using Storyboard.
Make a new UIViewController with a nib
Apple's example of presented a view controller programmatically
- (void)add:(id)sender {
// Create the root view controller for the navigation controller
// The new view controller configures a Cancel and Done button for the
// navigation bar.
RecipeAddViewController *addController = [[RecipeAddViewController alloc]
init];
// Configure the RecipeAddViewController. In this case, it reports any
// changes to a custom delegate object.
addController.delegate = self;
// Create the navigation controller and present it.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentViewController:navigationController animated:YES completion: nil];
}
If you are using an nib though you would want to allocated as followed:
RecipeAddViewController *addController = [[RecipeAddViewController alloc]
initWithNibName:#"yourNibName" bundle: nil];
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW1
You can easily just copy your source and header files of choice to whichever project you like (beware of enabled/disabled ARC, though). Also, you can mix storyboards and nib files as you wish. Here's a little documentation on nibs that you might find interesting. Basically, you can init a UIViewController with it's nib file like this:
MyViewController *myViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
Yes, you can go with #Alex answer.
And you can also add "Storyboard" in your project.
Steps to add storyboard are as follows:-
1. Add new file => Click on "User Interface" tab on left side => Select storyboard => Choose Device type => and create. A blank storyboard file has been created.
2. Now, drag-drop a UIViewController from list of objects(right side) and reference it with your view controller class.
In the same way, you can add other views using storyboard and create multi-view app.

Create UINavigationController in other xib?

Hello, I have this code in my AppDelegate:
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
Now I want to create a XIB file in which I want to put a UINavigationController and add a UITableView. How do I create this through code without changing the delegate class?
I've tried this but it does not work:
PlacesTableViewController *obj = [[PlacesTableViewController alloc]init];
obj.title = #"Farmacie intorno a te";
UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:obj];
[self.window addSubview:navC.view];
First, the "modern" way to set up your window is to use the window's rootViewController property:
window.rootViewController = someViewController;
The old way, where you add a view controller's view to the window as a subview, still works but the app will log a complaint about wanting the root view controller to be set up by the time the app is done launching.
Second, if you're going to replace the root view controller (in this case you're replacing your viewController with navC) using the old style, you'd want to remove the old view controller's view from the window. Your best bet is to just use the window's rootViewController property, since your code will be expected to use that going forward anyway.

SubView did not load in the first launching of my application

I'm new to iPhone development. I tried to build a simple application with a window and a navigation controller as a sub-view of this window. The problem is this: the sub view did not load when I launch the application. I just have a windows with black screen. To load the view controller, I have to quit the application and launch it a second time, then I have my sub view with the navigation controller. I added a button directly in the window to make sure that the black screen is not a problem, but I saw the button at startup.
This is the code I have in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
[self.window addSubview:_navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Do you have any solution for this problem?
Thank you.
You need to make sure that you have your view hierarchy setup. The window's rootViewController will be the UINavigationController. The UINavigationController controls a hierarchy of viewControllers, so when you instantiate it, you need to assign a rootViewController. Often times this is a subclass of a UITableView.
Because you are alloc/initing the window, I'm assuming that you do not have a XIB/NIB with the UINavigationController and an associated rootViewController like a UITableViewController. Also, rather than adding the view of your navigation controller, you need to assign the rootViewController, to the window. Since iOS4 this is the preferred way of doing things. See here as well. Try this code:
YourViewController *yourViewController = /* code for alloc/initing your viewController */
_navigationController=[[UINavigationController alloc] initWithRootViewController:yourViewController ]
self.window.rootViewController=_navigationController; /* instead of using [self.window addSubview: _navigationController.view] */
[self.window makeKeyAndVisible];
If you are using a XIB/NIB, then you need to make sure the _navigationController is wired up to the XIB file and has a subclass of a viewController wired up as it's rootViewController.
Good Luck

Switching from one view to second view?

I want to know that can in switch between two views in an iPhone application if I have chosen the application as window based application in the Xcode or it is only possible to switch between views in view based application only.
How to design interface for changing the views in such appliocations as I am not able to design the second view in the interface builder after designing the first view.
Your view controller can present any other view controller like this
[firstViewController presentModalViewController:secondViewController animated:YES];
This will take you to second view controller.
To come back to first view controller, in second view controller you say
[secondViewController.parentViewController dismissModalViewControllerAnimated:YES];
Please refer to the documentation here
The main problem i think you have here is the perception of what each project type does.
A Window based application provides just a window and no "default" view controller for you to use.
A View based application provides a window AND a view controller and xib file for you to create your UI.
If you want to see how to add a view to a window based application create an empty view based application and look at the code that is auto added to the didFinishLaunchingWithOptions method in the appdelegate. This is essentially what you need to do with your window based application.
Add a view controller with a xib file for user interface, then look at how the view based application loads this view and displays it (using initWithNibName and then adding the view to the window)
i'd say you need to do more reading: take a look at cocoa fundamentals for iOS - in the documentation and then the view controller programming guide) these are both essential areas of reading. Then have a root around in the standard project types and take a look at how they are set up, this is really useful because you'll see what apple intend you to do when setting up your app
When you have your class which controls the UIWindow, you add objects in that window. One or some of these objects are Navigation Controllers or Views. in you windowController.h, you should define a view:
#property (nonatomic, retain) IBOutlet UIView *mainView;
and in the .m-file, synthesize it:
#synthesize mainView;
Then use it:
MainView *mainView = [[MainView alloc] initWithNibName:nil bundle:nil];
mainView.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mainView animated:YES];
[mainView release];
That is one of the two things that could possibly be your question.
Another question you could ask is: how to switch from my window-based application to a view-based application?
You can just create new classes with corresponding .xib-files, being UIView's. Adapt your appDelegate classes and you should be fine.
Switching from one view to other in view based application
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:firstViewController.view];
or you can also use this.
FirstViewController *firstViewController = [[UIViewController alloc] initWithNibName:#"FirstViewController" bundle:[NSBundle mainBundle]];
[self presentModalViewController:firstViewController animated:YES]; // this is deprecated in ios6.0

iPhone: create new "View-based Application" = no view controller?

I have created a new iPhone "View-based Application" in Xcode. I then added a new "UIViewController subclass" and checked the "with XIB for user interface. Now the issue I have is that after hooking up all the variables and message handlers, I cannot push the new controller onto the stack using the following code:
[self.navigationController pushViewController:self.cabinetController
animated:YES];
All the variables and views are hooked up correctly, so all that I can think of is that its the way I am doing it, by pushing it onto the "navigationController". Is there something I am missing here? (I am very new to iPhone and Apple programming in general, so its probably a very simple oversight).
I realise that not enough information has been supplied ... here is a link to the project. Please note that it is an educational exercise has some creatively names classes.
http://files.me.com/nippysaurus/4yqz8t
In your appDelegate create a UINavigationController instance variable and then use your existing viewController as the rootViewController of the navigation controller.
e.g. in pure code using a UITableViewController (you can use xibs as well which your template app probably does).
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Create root view and navigation controller
UITableViewController *rootViewController = [[[UITableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
self.navigationController = [[[UINavigationController alloc] initWithRootViewController:rootViewController] autorelease];
// Not necessary if you're using xibs
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Add the nav controller's root view to the window
[window addSubview:navigationController.view];
[window makeKeyAndVisible];
return YES;
}
You need to change your view controller to a navigation controller, with its root view controller set as the current view controller.
If you examine your self.navigationController, you will realize it is nil. Messaging nil doesn't hurt, so no error message here.
Add another layer with a UINavigationController, and add your RandomShitViewController (nice name btw.) as its root view controller.
The navigation controller handles the push / pop part, your old controller manages its view.