Iphone dev: Basic static page navigation? - iphone

This is an incredibly basic question, but I'm not sure what's the best method of doing this professionally.
Suppose I want to make an app where the user just taps through multiple pages (a page might just be a screen with a background image and a few buttons). Now and then they might tap back to access a previous page.
I've come from cocos2d, so what's actually the best way to do this with Cocoa Touch? Do I have a seperate view for each page and just keep adding/removing them to the main view controller? Would I load everything at the start or drop a page from memory when the user clicks away?
Please give me a general approach as to how you would do this. Thanks!

The best way to do that is to use a UINavigationController. You can do something like this :
AppDelegate.h
UINavigationController *navigationController;
#property (nonatomic, retain) UINavigationController *navigationController;
AppDelegate.m
#synthesize navigationController;
// In ApplicationDidFinishLaunching
UIViewController *yourMainViewController = [[UIViewController alloc] init];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:yourMainViewController];
self.window.rootViewController = self.navigationController;
in YourMainViewController.m
// When you click on a button
[[self.navigationController pushViewController:yourNewViewController animated:YES];

Related

Is a UINavigationController required to do pushViewController?

I think the answer to my question is "Yes", but i just would like a confirmation from everyone.
I have a UITableView which should "slide" to another UIViewController when i select a row.
I've done this before with other apps (and they've included the UINavigationController, though in a UITabView).
So im wondering:
Is a UINavigationController required in order to use the self.navigationController pushViewController?
If so (which again i suspect to be true), where must I define the UINavigationController?
In the AppDelegate? In the main/primary UIViewController?
Yes, you will need to have a UINavigationController in order to do pushViewController. However, there is no one place where you must define it. If every view controller in your whole application is part of one UINavigationController, then it makes sense to put it in the App Delegate. You can, however, allocate and use a view controller at any time. (Notice that it is a UIViewController subclass.) You can also have multiple UINavigationControllers in your app (which is common, for example, if you have a Tab bar). So you can create UINavigationControllers at any time.
If you are looking for alternate ways of present other view controllers, you do have some different options. For example,
- (void)presentModalViewController:(UIViewController *)modalViewController animated:(BOOL)animated
will allow you to transition from one view controller to another view controller, and can be used without a UINavigationController. But I think the Navigation Controller is very often the best way to move from one view controller to another.
The short answer is: Yes.
UINavigationController was designed with the purpose of managing and animating a stack of view controllers on and off the screen (in conjunction with a few other interface elements such as a navigation bar, or a toolbar). Traditionally, UINavigationControllers are strongly held and initialized by the App Delegate, as they are considered a top-priority root object. An example class showing the proper usage of a UINavigationController might look like this:
#import <UIKit/UIKit.h>
#interface CFIExampleAppDelegate : NSResponder <UIApplicationDelegate>
#property (nonatomic, strong) UIWindow *window;
#property (nonatomic, strong) UINavigationController *navigationController;
#end
#implementation CFIExampleAppDelegate
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:UIScreen.mainScreen.bounds];
self.navigationController = [[UINavigationController alloc]initWithRootViewController:/*Some Controller Instance*/];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
#end
The long answer, as always, is No.
UINavigationController can be trivially reimplemented (and it has, many times) to great effect. It's as simple as subclassing UIViewController and bolting on some kind of stack (like an NSMutableArray).

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

TabBarController + TableViewController + Navigation Controller?

I'm new to iphone development, and I want to make an app combining TabBarController + UITableViewController + UINavigationController. I know this question was widely discussed in some video tutorials and threads, but some of them are for old xcode and sdk versions, and I have problems when working through the tutorials.
I will be glad if someone could help me with an up to date tutorial or source files.
To be clear, I'm trying to have my app based on a tab bar controller with two tabs: the second will be a simple view, and the first should contain a tableviewcontroller and a navigation controller. When I click on a cell, it should move to another "detail" view.
Any help will be great.
Ok this is going be long:
in the AppDelegate.h
allocate a UITabBarController a UINavigationController and 2 UIViewControllers
for example:
UITabBarController *mainTabBar;
UINavigationController *navController;
UIViewController *firstViewController;
UIViewController *secondViewController;
then move to AppDelegate.m and instantiate each of those 4 items like this:
mainTabBar = [[UITabBarController alloc] init];
firstViewController = [[firstView alloc] init];
do that for both views
then if you want to set the title of either of the views, (this will be the title that shows up in the tab bar) do it as follows:
firstViewController.title = #"Welcome";
Then create the UINavigationController for the view that has a UITableView inside it like this:
navController = [[UINavigationController alloc] init];
[navController pushViewController:firstViewController animated:NO];
Now you have a UIViewController and a UINavigationController with a UIViewController inside it.
All thats left is to put your two tabs into the UITabBarController:
mainTabBar.viewControllers = [NSArray arrayWithObjects:firstViewController, secondViewController, nil];
then just put the tab bar controller on screen and you should be good to go:
[window addSubview:mainTabBar.view];
A couple things to remember:
make sure you release everything you called alloc on so your using good memory management.
Make sure you import all the files you intend on using in AppDelegate.h, it should look something like: #import "FirstView.h
Let me know in a comment if you have any questions
The document on Apple's developer's library portal, titled Combined View Controller Interfaces, explains how what you are after can be done both through the use of Interface Builder as well as programmatically. I recently followed the documentation on that page to create a part of an application that did entirely what you want to do.

Navigation Controller and Bar - Memory Managment

first of all... i'm italian, so i'm sorry for my bad english!
so... this is my app:
i'm using a navigation controller: in the first view there are 10 buttons and every button calls a functions like this:
[self pushViewController:nameview animated:YES];
to a different uiviewcontroller! So i have 11 uiviewcontroller!
Every controller is decleared like here
#interface ...
IBoutlet UIViewController *viewcontroller;
...
#property (nonatomic, retain) IBOutlet UIViewController *viewcontroller;
Finally i have to say that i'm working with IB!
My problem is that my app doesn't release memory!
when i'm in a view and i tap on the "backbuttonitem" (created by IB, not by me) the last view doesn't became released (again, sorry for my bad english)... and if an user see all 10 view, the app receive a warning massage (low-memory)!
How can i release the last view saw before the popviewcontroller action?
thanks :D
When you push a view controller onto a navigation controller you need to release it as the navigation controller now owns it. For example you would do the following:
UIViewController *controller = [[UIViewController] initWithNibName:#"Nib" bundle:nil];
[self pushViewController:controller animated:YES];
[controller release];
Then when you use popViewControllerAnimated: the navigation controller will take care of making sure the view controller is released from memory.

Adding my first view to a window based application

I'm beginner with iPhone and want to know how to add a view in a window based application.
I crate the application, then I added the ViewControler+XIB to my project.
I include the .h file in both AppDelegate-Files.
Then I create a object of the View "StartViewController" with
StartViewController *startView;
and add the property in the AppDelegate.h file:
#property (nonatomic, retain) StartViewController *startView;
In the AppDelegate.m I add:
#synthessize startViewController
and in the application method:
[window addSubview:startViewController.view];
But it doesn't appear when I start the application, what I forgot?
You're not allocating or initializing it anywhere that I can see. From what you're saying, it seems like you need to put in a
startView = [[StartViewController alloc] initWithNibName:#"xib filename without dot xib" bundle:nil];
If this doesn't sound right, it may instead be that you aren't setting Interface Builder up right. How is the main window linking to this XIB / view controller of yours? Do you have it referenced as a view?
I'm beginner with iPhone and want to know how to add a view in a window based application
STEP 1:
Open Xcode Create New Project Select Window Based Application.Name is Some x
that is display two files xappdeligate.h and xappdelegate.m.
Step 2:
in project click right button add ui view controller name is FirstViewController that is displays FirstViewController .h , FirstViewController .m
Step 3:
In xappdeligate.h we have to write
FirstViewController *viewController;
define Property:
#property(nonatomic,retain)FirstViewController *viewController;
Step: 4
In xappdelegate.m we have to synthesize to them
#synthesize viewController;
allocate thememory:
viewController = [[FirstViewController alloc]init];
add view to the window:
[self.window addSubview:FirstViewController.view];
Step: 5
We should compulsary release the memory for this in
-(void)dealloc
{
[window release];
[viewController release];
}
this is simple way to add view to the window.
Ur's Raffi 37...
Just connect the view with interface builder by dragging.
and still if you can't connect , here is link where a number of applications are provided to learn iPhone applications.