Using Navigation Controller in a subview of View based Applicatin - iphone

Some one please help me with this. I am developing an iPhone app and have stared with View Based Application. I have buttons on my root view screen to take user to other views with their own nibs and classes. On one of those views I need to display a table getting data from SQLite database and then display detail of the selected item depending on the selection from the table. At later stage I also need to add forms to add data.
Now, how to add a Navigation Controller in that view for drill down? Can we convert a View Controller to Navigation Controller just by adding a Navigation bar at the top?

sure
in viewbase application, u can create navigation controller
add the code in delegate.h
#interface test24AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
test24ViewController *viewController;
UINavigationController *nav;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet test24ViewController *viewController;
#property (nonatomic, retain) IBOutlet UINavigationController *nav;
#end
add this delegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
nav=[[UINavigationController alloc]init];
[nav pushViewController:viewController animated:YES];
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];
return YES;
}
now it will work, u check using nslog(#"%#", self.navigationController); if the result is null then u have problem still.

Related

How to properly add a Navigation Controller to my Search View with hidden Navigation Bar

When my app is launched the first screen (view) the user sees when my app is launched is a search form without any navigation. Navigation will show up after search process is done and results are ready to be displayed. Where I'm stuck at is the proper way to make it work with the navigation controller.
So, assuming the app name is RealEsateProperties
In RealEsatePropertiesAppDelegate.h:
#import <UIKit/UIKit.h>
#class RealEsatePropertiesViewController;
#interface RealEsatePropertiesAppDelegate : NSObject <UIApplicationDelegate>
{
UINavigationController *ListingNav;
}
#property (nonatomic, retain) IBOutlet UIWindow window;
#property (nonatomic, retain) RealEsatePropertiesViewController *viewController;
// Then I added this line for the navigation
#property (nonatomic, retain) UINavigationController *ListingNav;
#end
and in RealEsatePropertiesAppDelegate.m:
#import "RealEsatePropertiesAppDelegate.h"
#import "RealEsatePropertiesViewController.h"
#synthesize window=_window;
#synthesize window=_viewController;
#synthesize ListingNav;
#implementation RealEsatePropertiesAppDelegate
- (BOOL)application:(UIApplication *)application didFinishLanchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
// Iadded the following 4 lines to try making the navigation thing work without showing any navigation bar on the first screen (that is the search form)
self.ListingNav = [[UINavigationController alloc] initWithRootController:self.viewController];
self.ListingNav.navigationBarHidden = YES;
[self.window addSubView:ListingNav.view];
[self.window makeKeyAndVisible];
return YES;
}
#end
Am I doing anything wrong ?
Thx for helping,
Stephane
You need to create/alloc your RealEsatePropertiesViewController ?
viewController = [[RealEsatePropertiesViewController alloc] init];

Subclassed? UITabBarController wont autorotate

Noobie so bear with me.
I've have been following the O'Rielyy Learning iPhone Programming and various threads on here to build my first iPhone App. So far so good, but the final stumbling block at the projects end is getting the App to autorotate (the beta using only uiwebviews was rejected for not auto-rotating)
I have the mail App delegate, which adds a UITabBarController
// myNewsUKDelegate.h
#interface myNewsUKDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
#end
// myNewsUKDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Add the tab bar controller's view to the window and display.
[self.window addSubview:tabBarController.view];
[self.window makeKeyAndVisible];
return YES;
}
There is are .h and .m files for tabBarController - I added all the UINavigationControllers in IB, which in turn add a UITableView
See image at http://flatearth.co.uk/nib.png (too noob to post images in questions!)
From my reading I understand that the issue is the UITabBarController I added to the main view needs to be 'subclassed' and have this code added.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
The next view down/in/subclassed (whatever the correct terminology is), which has .h and .m files is the FirstViewController which adds the table view, this has shouldAutorotateToInterfaceOrientation already set.
#interface FirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *tableView;
NSArray *userList;
}
#property (nonatomic, retain) IBOutlet UITableView *tableView;
#property (nonatomic, retain) NSArray *userList;
#end
#implementation FirstViewController
#synthesize tableView;
- (void)viewDidLoad {
[super viewDidLoad];
// I tried adding
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// lots of other code ; )
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
So the problem appears to be that when [self.window addSubview:tabBarController.view]; adds the tab bar it doesn't add the shouldAutorotateToInterfaceOrientation returning YES bit.
It appears that I need to add a tabBarController subclass, with the shouldAutorotateToInterfaceOrientation in it. So I read up and tried this, as suggested on the interwebs...
// tabBarController.h
#import <UIKit/UIKit.h>
#interface tabBarController : UITabBarController {
}
#end
// tabBarController.m
#import "tabBarController.h"
#implementation tabBarController
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
#end
and adding
#import "tabBarController.h"
to myNewsUKDelegate.m
But that fails with "error: accessing unknown 'view' class method" at the
[self.window addSubview:tabBarController.view];
line in myNewsUKDelegate.m
Further searching hasn't produced anything helpful and my recent Xcode knowledge has now ran dry : ( Any help appreciated.
From my reading I understand that the issue is the UITabBarController I added to the main view needs to be 'subclassed' and have this code added.
No, you don't need to do that. The tab bar controller determines if it supports a specific interface orientation or not by asking all its child controllers if they support this orientation. In your case, these seem to be navigation controllers, which in turn ask their current child controller if it supports the orientation.
In other words, you have to make sure that all your custom view controllers return YES for the desired interface orientation.
You don't need a subclass, you need a Category on UITabBarController. Basically you create a file called UITabBarController + Autoresize.h (and .m)
In the .h:
#import <UIKit/UIKit.h>
#interface UITabBarController (Autoresize)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
#end
in the .m:
#import "UITabBarController + Autoresize.h"
#implementation UITabBarController (Autoresize)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
//do custom checks here if you only want to autorotate it in certain views or whatever
}
#end
but as the other poster pointed out, ALL the parent views of the view you wish to rotate must support rotation.

problem in UINavigation controller

hi to all
i am trying to do a simple navigation based application.this is my code
#import <UIKit/UIKit.h>
#class RootViewController;
#interface jeeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
RootViewController *viewcontroller;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet RootViewController *viewcontroller;
#end
.m file
#import "jeeAppDelegate.h"
#import "RootViewController.h"
#implementation jeeAppDelegate
#synthesize window;
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController=[[UINavigationController alloc]initWithRootViewController:viewcontroller];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
For this code i got "Application tried to push a nil view controller on target <UINavigationController: 0x4b4f5c0>." thanks in advance..
This is fairly simple - at no point have you initialised the view controller that you're attempting to push onto the navigation stack.
i.e.: Before you call..
navigationController=[[UINavigationController alloc]initWithRootViewController:viewcontroller];
...you need to ensure that viewcontroller actually exists, in the sense that you need to alloc and init it.
For example:
// Create our main menu view controller
MainMenuViewController *mainMenuVC = [[MainMenuViewController alloc] init];
// Create our navigational controller and init it with the main menu view controller
navController = [[UINavigationController alloc] initWithRootViewController:mainMenuVC];
[mainMenuVC release];
In the above mainMenuVC is a custom view controller that I've created.
Also, please note that once you've added your view controller to the navigation controller, you can release it as the navigation controller with retain it.

Passing info from Facebook to UITabBarController

When my app first starts, it shows a main page to log in to Facebook. Then, it goes to the UITabBarController. The code that I have in my app delegate is the following:
//this is the .h
#interface NMeAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainViewController *controller;
UITabBarController *tabBar;
}
#property (nonatomic, retain) IBOutlet UITabBarController *tabBar;
#property (nonatomic, retain) MainViewController *controller;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
//this is the .m of the app delegate
#import "NMeAppDelegate.h"
#implementation NMeAppDelegate
#synthesize window;
#synthesize tabBar;
#synthesize controller;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
controller = [[MainViewController alloc] init];
[window addSubview:tabBar.view];
[window addSubview:controller.view];
[window makeKeyAndVisible];
return YES;
}
Inside of MainViewController, I actually have a UserInfo object, which has all of the information that I need for the UITabBarController. The problem is that after getting this info in the UITabViewController, how do I pass this UserInfo to the UITabBarController` or possible the ViewController that is inside the UITabBarController so they were able to access this UserInfo? How to do this?
I hope my question makes sense.
you need to have an instance of your UserInfo object available to the tab bar controller. probably pass it into a instance variable of type UserInfo in your UItabBarController/each view controller whenever you transition into the specified controller.
edit: you should really have this passed into the view Controller it needs to be in (since it doesn't appear you have a custom UITabBarController subclass), or you could use a shared UserInfo variable in your app delegate to keep up with the information.
But as the commenter said, the question is not very clear at all and i could be completely misunderstanding what you want to do.
Hope that helps.

Calling pushViewController on a UINavigationController does nothing (no crash)

I have a UINavigationController as one of the views inside a tab bar control. It looks fine, and I have a UIBarButtonItem that is supposed to load a subview. I have the button wired up to an IBAction that calls pushViewController but when I do this nothing happens. It doesn't crash or anything.. it just doesn't do anything. I've tried: using different view controllers as the subview (no luck). Does anybody have any suggestions? Here is my code:
Header file:
#import <UIKit/UIKit.h>
#import "FSSettings.h"
#import "MeasureSelector.h"
#import "Dashboard.h"
#interface DashboardNavigationController : UIViewController {
IBOutlet UINavigationController *navController;
IBOutlet UINavigationBar *navBar;
IBOutlet UIBarButtonItem *measureButton;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UINavigationBar *navBar;
#property (nonatomic, retain) IBOutlet UIBarButtonItem *measureButton;
- (IBAction) showMeasureScreen:(id)sender;
#end
And the .m file containing the action:
// Displays the measure screen
- (IBAction) showMeasureScreen:(id)sender
{
NSLog(#"Loaded measure screen");
MeasureSelector *msel = [[MeasureSelector alloc] initWithNibName:#"MeasureSelector" bundle:nil];
[[self navigationController] pushViewController:msel animated:YES];
NSLog(#"Done.");
}
When I click the button nothing happens (but I do see the log messages). I can do this over and over with no ill effects, however.
The navigationController property of UIViewController refers to the nav controller of which the UIViewController is part of the hierarchy. If I understand the scenario correctly, DashboardNavigationController manages the view that is the container for the UINavigationController, so it makes sense that this property would be nil.
Use the outlet you created to access the nav controller from outside of the nav controller's hierarchy.