UINavigationController pushViewController from tableViewController subclass - iphone

I am seeing a strange app behaviour when I try to push some view controller from uitableviewcontroller subclass.
Let me explain it first. I have created main nib which is linked to rootViewController (appDelegate) that is inside navigationController. In that nib a have added a UITableView and a custom UITableViewController subclass News_TableViewController like it's shown in the screenshot:
When I try to execute the code bellow I get nothing:
My_WebView *webView = [[My_WebView alloc] initWithNibName:#"My_WebView" bundle:nil];
[self.navigationController pushViewController:webView animated:YES];
Then I checked self.navigationController object but i gives me NULL:
NSLog(#"OBJ: %#",self.navigationController);
How is that I am not geting the reference to the navigationController despite of my custom class actually lives under navigationController ?
Thanks

As you've mentioned, News_TableViewController is a UITableViewController, hence a UIViewController. I think you did'nt initialize the navigationController! A recommended way is to init the navigationController in your appDelegate class' delegate method as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
RootViewController *rootViewController = [[RootViewController alloc]init];
_navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
where RootViewController is the News_TableViewController class.

Related

Application will enter background -> go to root view menu

I am developing an application which has about 8 views and use navigation controller to navigate through. The first view is a main menu.
What i want is (of each view) to pop to the main view if the user press the home button (App did enter background).
I know the AppDelegate methods applicationDidEnterBackground and applicationWillEnterForeground.
And i know the method popToRootViewControllerAnimated called from the navigation controller.
I have tried to use popToRootViewControllerAnimated in applicationDidEnterBackground.
Like:
[self.window.rootViewController.navigationController popToRootViewControllerAnimated:YES];
But this does not work.
Can you please let me know what is the best option for this job?
i think you try NSNotificationCenter like this:
inside applicationDidEnterBackground and applicationWillEnterForeground put this
[[NSNotificationCenter defaultCenter] postNotificationName:#"popToRoot" object:nil];
and in your rootViewController's viewDidLoad (that always appears on app launch) add this:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(popToRootViewControllerAnimated) name:#"popToRoot" object:nil];
Then create a method in your rootViewController:
- (void)popToRootViewControllerAnimated
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
Whenever the application will start first time, NSNotificationCenter will initialize for name popToRoot and prepare a method popToRootViewControllerAnimated for this.
And when application will go to background, NSNotificationCenter will pass a massage #"popToRoot" to rootViewController's popToRootViewControllerAnimated method and viewcontroller will pop to rootview
have you tries it like this :-
[self.navigationController popToRootViewControllerAnimated:YES];
replace your navigationController name with navigationController here.
Edit:-
in AppDelegate.h file
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navMain;
}
#property (nonatomic, retain) UINavigationController *navMain;
in AppDelegate.m file
#synthesize navMain;
-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.navMain = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navMain;
[self.window makeKeyAndVisible];
return YES;
}
-(void)applicationDidEnterBackground:(UIApplication *)application
{
NSLog(#"applicationDidEnterBackground");
[self.navMain popToRootViewControllerAnimated:YES];
}
try edited anwser
crate a propery for UINavigationController in your AppDelegate class. In applicationDidEnterBackground: method call the popToRootViewController method by using the UINavigationController Property. For Suppose your propery name is navigationController then
[self.navigationController popToRootViewControllerAnimated:YES];
First: you should check wether the rootviewcontroller is a navigationController. Because self.window.rootViewController.navigationController is often nil.
Why?
Because the navigationController of a navigationController is 'nil'. Mostly, I set my rootViewController to be a navigationController
Secondly:
You shouldn't do animated stuff when your application is about to quit. You should do it not-animated
popToRootViewControllerAnimated:NO

UITableViewController within UISplitViewController

I'm trying to load a specific UITableViewController as the Detail view of a UISplitViewController when the user selects a cell within the Master view; but having some problems. When i select the cell, it just shows a blank window with out a table view controller.
However if i change the App Delegate to just load the UITableView controller without using the UISplitViewController it works fine, so i know it's a problem with the way i've coded the UISplitViewController.
The view hierachy:
UISplitViewController
-->
UINavigationController
--> UITableViewController (DetailViewController)
UINavigationController
--> UIViewController (ColorViewController)
The user selects a cell in ColorViewController and that should change the DetailViewController.
In AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.viewController = [[ViewController alloc] init];
self.window.rootViewController = [self.viewController splitViewController];
[self.window makeKeyAndVisible];
return YES;
}
In ViewController.m:
- (UIViewController *) splitViewController {
// Create the navigation-run root view
ColorViewController *rootVC = [ColorViewController controller];
UINavigationController *rootNav = [[UINavigationController alloc] initWithRootViewController:rootVC];
// Create the navigation-run detail view
DetailViewController *detailVC = [DetailViewController controller];
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:detailVC];
// Add both to the split view controller
svc = [[UISplitViewController alloc] init];
svc.viewControllers = [NSArray arrayWithObjects: rootNav, detailNav, nil];
svc.delegate = detailVC;
return svc;
}
In ColorViewController.m:
#interface ColorViewController : UITableViewController
...
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *controller = (UIViewController *)self.splitViewController.delegate;
TableViewController *tvc = [[TableViewController alloc] init];
[controller.view addSubview:tvc.view];
}
What's the best way to change the Detail view within a UISplitViewController? Should i just add the replacement view to UIViewController like above or is there a better way? Maybe that's the cause of my problem?
Since you have a navigation controller handling the detail side of your split view, your strategy should be to use it to push your new table view controller. When I've done this, I've kept references to both navigation controllers as properties in the app delegate so that I can use one or the other for push operations.
I'm not sure what the real purpose of your ViewController class is but if you move its splitViewController method into the app delegate it would be easier to change the navigation controllers from local variables to properties in a place where they can be easily accessed.
I'm going to answer my own question..
For some reason, in the Master view (ColorViewController), the detail object TableViewController *tvc needs to be declared as an instance variable, not as a variable within a method.
After doing that, it works and displays the table properly.
I don't really understand why though, if anyone would like to try to explain.
Cheers,

how to add uinavigation controller in a view based application

I wanted to add a navigation controller to a view based application . how can we do this both programmatically and using xib file..
If you need to incorporate a navigation controller in your uiviewcontroller you need to initialize it as it follows
UIViewController *yourViewController = ...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self presentModalViewController:navController animated:YES];
//you need to release the controller
[navController release];
If you are in the UIApplicationDelegate method
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
You can't do a presentModalViewController:navController animated... then you need to add the navController.view to the window
UIViewController *yourViewController = ...
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self.window addSubview:navController.view];
//don't do a release of navController because is not retained by addSubview
UINavigationController *navcontroller = [[UINavigationController alloc] initWithRootViewController:viewController];//here viewController is to which you want to make the navigation
[self.view addSubView:navController.view];
You can just drag a "Navigation Bar" from your objects in the bottom right corner of Interface Builder. This basically does what Sachin says in his answer but you still have to programmatically create the functionality of the navigation controller. I.e pushing new views to the stack and poping them off.
In my opinion it's easiest to do it entierly in the code.
If you want to have a navigation controller as the root view for your main window. Then you can do so by using the following code.
#interface yourAppDelegate_iPad : NSObject <UIApplicationDelegate> {
UINavigationController *navigationController;
}
#property (nonatomic, retain) UINavigationController *navigationController;
#end
#implementation yourAppDelegate
#synthesize navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController = [[UINavigationController alloc] initWithRootViewController:yourRootViewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
You can do this by using the xib as follows
Open the MainWindow.xib
Drag and drop a UINavigationController to it.
Create and connect the outlets.
Open attributes for the navigation controller and set the root
view.

iPhone - pushViewController Issue

I have a root view controller which should load another view controller as soon as it is done loading (i.e. in the viewDidLoad method).
I am using the UINavigationController in order to push a new view controller onto the stack:
In my rootviewcontrollerappdelegate:
-(void) viewDidLoad{
LoginViewController* lvc = [[LoginViewController alloc]init];
[self.navigationController pushViewController:lvc animated:NO];
}
I have textfields and buttons in the view controller to be loaded. The above doesn't seem to work however...It loads just a blank grey screen and no UINavigation bar is present. If I comment out the second line (pushViewController line), then I see the navigation bar. So I think it is loading something, but the items in the view controller being loaded are not being shown...Any ideas why?
Check if navigationController is pointing to nil. If it does, try
[self.view addSubview:self.pushViewController.view]
I had the same problem and found the above solution here:
UIViewController -viewDidLoad not being called
Unless you're doing something tricky, you should be calling alloc on the LoginViewController class rather than a variable. Also, if you've set up LoginViewController in Interface Builder (as opposed to programmatically), you'll need to load it from an NIB:
LoginViewController *lvc = [[[LoginViewController alloc] initWithNibName:nil bundle:nil] autorelease];
[self.navigationController pushViewController:lvc animated:NO];
Have a look at initWithNibName:bundle: in the docs.
Not entirely sure what you are trying to achieve but when you instantiate LoginViewContoller it should probably look like this
LoginViewController* lvc = [[LoginViewController alloc]init];
Judging by the nature of your naming for your view controller, is your LoginViewController the first view controller for your UINavigationController?
If that is what you're trying to do, you should instead initialise your navigation controller with the LoginViewController as the root controller instead of pushing it onto the navigation stack.
UINavigationController has a method to do this:
- (id)initWithRootViewController:(UIViewController *)rootViewController
EDIT:
Well, one way you can go about it is like this.
In your application delegate .h file, you should have declared a UINavigationController.
#interface MyAppDelegate : NSObject <UIApplicationDelegate>
{
UINavigationController *navController;
}
#property (nonatomic, retain) UINavigationController *navController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
In your App Delegate didFinishLaunching:withOption: you can create an instance of your LoginViewController there, and use that to init your UINavigation controller as the root view controller
#import "LoginViewController.h"
#implementation MyAppDelegate
#synthesize navController;
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
LoginViewController *loginController = [[LoginViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:loginController];
[loginController release];
[[self window] setRootViewController:navController];
[navController release];
[self.window makeKeyAndVisible];
return YES;
}
I probably have a typo here or there but that's one way I would go about doing it.

Interchanging presentModalViewController and pushViewController

I am using a sample ViewController that is displayed in the sample program as a presentModalViewController. However, I want to use pushViewController on the UIViewController instead. The problem is that when I switch to pushViewController, the ViewController no displays properly. Functions in the ViewController are called, but I don't see anything. I change back to presentModalViewController and everything works.
The question is what do I need to do to make pushViewController work?
FCVC *fcVC;
NSArray *array = [[NSBundle mainBundle] loadNibNamed:#"fcVC"
owner:self
options:nil];
fcVC = [array objectAtIndex:0];
A. [self presentModalViewController:fcVC animated:YES]; // "WORKS"
or
B. [self.navigationController pushViewController:fcVC animated:YES]; // "Doesn't work
Do you have a UINavigationController actually set up? "self" should be a viewController that is loaded inside UINavigationController.
To create a NavigationController in a view-based application you have to set the NavigationController in the delegate class.
Like This
In delegate .h class
MyViewController *viewController;
In delegate .m class
- (void)applicationDidFinishLaunching:(UIApplication *)application {
UINavigationController *nvcontrol =[[UINavigationController alloc] initWithRootViewController:viewController];
[window addSubview:nvcontrol.view];
[window makeKeyAndVisible];
}
Here "MyViewController" should be replaced by your viewcontroller.
All The Best.