Pushing a View on Navigation stack - iphone

How is a ViewController object pushed into view via the navigationController if the object is created in another class than the one which should push the view.
The problem I had is in the class where I created the object and set its values, I had no access to the main navigationController. when I called [self.NavigationController .... It is undefined.
How is this navgation controller accessed? or how can I create a viewController object in one class and set values/push the object into view via another class?

Suppose classA- mainViewController
and classB- userDetailVC
pass the Object to classB before its push on Stack
-(IBAction)buttonClick:(id)sender{
UserDetailVC* userDetailVC=[[UserDetailVC alloc] initWithNibName:#"UserDetailVC" bundle:nil];
[userDetailVC autorelease];
//imp- set the object over here
NSDictionary* tempDict=[[NSDictionary alloc] initWithDictionary:self.userInfoDict];
userDetailVC.userInfoDict=tempDict;
[self.navigationController pushViewController:userDetailVC animated:YES];
}

Related

Trying to go from one view controller to another in a story board

I'm writting my first app using a storyboard. Before I had the following code to go from one view controller to another. But..this code needs a NibName. How would I do this going from screens created in a storyboard?
if (mDisplayCard==nil)
{
mDisplayCard = [[cDisplayCard alloc]
initWithNibName:#"cDisplayCard"
bundle:[NSBundle mainBundle]];
}
// [ mDisplay SetUp];
[self.navigationController pushViewController: mDisplayCard animated:YES];
A line like the following will instantiate a new view controller from a storyboard:
UIViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"<identifier-from-storyboard>"];
It is assumed that your current view controller was instantiated from the same storyboard, so that self.storyboard is not nil. For the view controller that you want to instantiate, you'll have to make sure that you set a storyboard ID for it, which is what you'll put in for the identifier.

XCode5 self.navigationController pushViewController:animated: doesn't open the new viewcontroller

I'm not sure why but in Xcode 5 working on a project of IOS6.1 I have a button connected to a IBAction in which I'm trying to navigate to a new view controller.
I've tried two different codes to create the viewController and then push it to the navigation in both cases the view controller is not nil and both cases the viewController doesn't appear.
first try: with story Id - I've set the story id of the view controller to imageCapture and set the class to VSImageCaptureViewController
VSImageCaptureViewController* imageCaptureViewController = (VSImageCaptureViewController*)([self.storyboard instantiateViewControllerWithIdentifier:#"imageCapture"]);
[self presentViewController:imageCaptureViewController animated:NO completion:nil];
second try: with the name of the viewcontroller
VSImageCaptureViewController *imageCaptureViewController = [[VSImageCaptureViewController alloc] initWithNibName:#"VSImageCaptureViewController" bundle:nil];
[self.navigationController pushViewController:imageCaptureViewController animated:YES];
can you see something wrong or do you think I forgot to initialize something
Check to see if self.navigationController is nil.
If it is nil that means that you are not running within the context of a UINavigationController (the system sets this property for you when the UIViewController is added to a nav stack).
If this is the case then you have not properly set up a UINavigationController.
Note that you can not set the navigationController property yourself. The systems sets it for you when the UIViewController is added to a UINavigationController's stack (and sets it to nil when it is removed from the stack).
To set this up you will usually create a UINavigationController instance right after you create your main view controller.
UIViewController *mainViewController = ...;
UINavigationController *mainNavController = [[UINavigationController alloc] initWithRootViewController:mainViewController];
// now present the mainNavController instead of the mainViewController
If you are using storyboards you would drag out a UINavigationController instance and replace the default root view controller with an instance of your mainViewController.

Set view controllers property before pushViewController

In my app I've added a label to a view, connected it to an outlet but nothing shows up when I first assign this outlet from another view controller and then call pushViewController to display it. Here's the code before pushing next view that display the label:
CustomViewController *vc = [[CustomViewController alloc] init];
vc.lbl_price.text = self.label_price.text; // lbl_price is defined as a property in CustomViewController and label_price is defined in current view controller
[self.navigationController pushViewController:vc];
In the CustomViewController viewDidLoad method I added this instruction to see if it should work
NSLog(#"Price=%#",lbl_price); // it actually prints out what was previously assigned
But it doesn't show into the label!
Any idea why ?
Stephane
Even if view controller is created its view hierarchy may not (and so all subviews will still be nil), for optimization reasons it may not be loaded until you try to actually access controller's view. You have two options to solve your problem:
Store all values in separate non-UI variables and assign them to UI components with controller is going to appear:
// Before push controller
vc.myPriceText = self.label_price.text;
// In controller's viewWillAppear:
self.lbl_price.text = self.myPriceText;
Make [vc view] call to force controller to load its view hierarchy:
CustomViewController *vc = [[CustomViewController alloc] init];
[vc view];
vc.lbl_price.text = self.label_price.text;
[self.navigationController pushViewController:vc];

Set another class' property

I want to set an NSString property of a main view from a nested view. I do so right now by allocating the previous class and accessing the class.property. When I pop the view controller programmatically and NSLog the property from the main view, it's null.
How does this happen?
EDIT:
MainViewController *controller = [[MainViewController alloc] init];
switch (indexPath.row) {
case 0:
controller.category = #"Categorie 1";
break;
default:
break;
}
[controller release];
You mentioned you are going to pop the viewController so I assume you are trying to set a property of the controller below the navigation stack.
Instead of creating a new object of the class, you should get back the original object that was already created.
NSArray *viewControllers = [self.navigationController viewControllers]; // array of viewControllers currently on the navigation stack.
MainVC *mainVC = (mainVC *)[viewControllers objectAtIndex:viewControllers.count - 2];
[mainVC setProperty:...];
Well first of all, you cannot access a specific instance's properties by simply calling the class. The class has no connection to any specific instance of it.
You need to have an instance variable in your nested view that references the parent. set this up when you create it. Then when you are in the child view controller you can still access the parent.
Something like this in the child:
MyParentViewController *parentVC;

Presenting Modal TableView

I've a tableviewController in a class which I need to present modally from another class. In the modaltableview class, I'm creating tableview in viewDidLoad and apart from this there are tableView delegate methods in this class.
My question is how do I present this class object to show this tableViewController modally?
I've presented the viewController from same class as:
UIViewController *vw = [[UIViewController alloc]init];
[self presentModalViewController:vw];
But what to write for a viewController from another class? Shall I call presentModalViewController from this class or the modaltableViewControler class?
P.S. I'm having a modalViewController already on the current viewController where I've to present the new modalTableView.
Thanx in advance.
In the visible view controller, create and initialise an instance of your modal tableViewController. Then, still in the visible view controller, call [self presentModalViewController:<THE TABLE VIEW CONTROLLER YOU JUST INITIALISED> animated:YES];
Hope this helps!
-jrtc27