pushViewController pushes nil - objective-c

I am trying to push a view controller to the screen. The push is successful but when I later try to access any property of the pushed view controller including textboxes and labels, they are all nil.
How do I avoid this?
I am doing it this way because I need my code to decide which view controller to push. Any help is much appreciated.
myVC *vC = [self.storyboard instantiateViewControllerWithIdentifier:#"myVC"];
[self.navigationController pushViewController:vC animated:YES];

After the code you've wrote, you will need to either approach your properties like this:
vC.property = assignedValue;
Make sure you have synthesized your properties, or initialised them with a setter in the vC's viewDidLoad

If you are using Storyboard , You can actually use prepareForSegue for transferring data between ViewControllers.
And be sure your properties are declared in .h file. You can't access .m file properties.
Hope this helps.

Related

Can't modal to next viewcontroller

I feel like I am just missing something simple here. I am trying to modal to my next view controller. I imported the next view controller in my first .m file first. After I did that I wrote this code
CRHViewController *nextViewController = [[CRHViewController alloc]init];
[self presentModalViewController:nextViewController animated:NO];
Also, I am working with storyboard and not nibs.
What happens when I run this is as soon as it goes to modal to the next viewcontroller it just goes black.
Am I missing something simple? Does anyone have an suggestions to fix this problem?
Probably you're not initialising it correctly. For testing purposes I would try to display CRHViewController as first root viewController from AppDelegate and find if it is initialising at all. Then check if it gets to its methods:
initWithNibName
loadView
awakeFromNib
viewDidLoad
viewWillAppear
viewDidAppear
In this order. Its 90% certain that one of them fails. Check if it gets to every method you have implemented correctly in this order.
If your CRHViewController is in your storyboard, then you should be instantiating it with:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"yourStoryboardName" bundle:nil];
[storyboard instantiateViewControllerWithIdentifier:#"myIdentifier"];
You should give your view controller an identifier in IB to pass in as the identifier parameter in the above method.

Accessing an instance method of a ViewController From Another view controller

Let's say I have a view controller called vc1, which a synthesized property called property1, and i wants to access it from another view controller (vc2) and change it from vc2.
Now the methods created by the #syntisize to change and get properties are instance methods, so how can I get to them fro another view controller (do view controllers have instances in the app, and if so, what are they?)
Just to be clear I am using storyboards, so I never really instantiate the view controllers...
VC1.m:
-(void) yourMethod {
...
}
VC2.m
YOURViewController * vc2 = [[YOURViewController alloc]init];
[vc yourMethod];
[vc release];
Make sure to import your YOURViewController in your other view .m file
Something like that should work.
Or if you're having problems, try this tutorial here:
Tutorial on How-To Pass Data Between Two View Controllers
Hope this helps :)
While you can do it the way you describe, I think the common technique (assuming VC1 has a segue to VC2) is a bit different, where VC2 will have a property that will be set by prepareForSegue. See Configuring the Destination Controller When a Segue is Triggered in the View Controller Programming Guide.
You will need to link the storyboard views with the viewcontrollers so the view for vc1 would use the class vc1 etc for the rest (I assume you have done this because this is important when coding for different views)
Then all you need to do is where ever you are calling the properties so lets say the viewDidLoad method, declare the view controller like this:
- (void) viewDidLoad {
vc1 *viewController;
// Now you change the variable I'll presume its a UILabel so I'll change its text [viewController.property1 setText:#"I changed a different views UILabel"];
}
Let me know whether this works... Its worked for me before so should work

Access a Navigation controller's main view programmatically

I've got a Tab Controller with a navigation controller which has a view, as seen in the image below:
I need to retrieve the Switches Controller from within my AppDelegate so I can do some things with it at runtime.
I believe I can retrieve the NavigationController itself by doing this:
UINavigationController *navController = [tabBarController.viewControllers objectAtIndex:0];
Not really sure how to access my SwitchesController from there though. Any suggestions?
Why not make an ivar with an IBOutlet? It's probably the most flexible solution as you can now change the ordering of your viewController's without breaking your build.
Use one of the visibleViewController, topViewController properties of UINaviationController, or call:
[navigationController.viewControllers objectAtIndex:index];
Docs: http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Hope it helps.

Subclassing in objective c and viewWillAppear message delegates?

I might be confused here and asking the wrong question.
If I use a class like the UISplitViewController inside the appdelete.m, will the only message i will receive is the message the UISplitViewController calls and not any VIEW message? for example:
in my myappdelegate.m
....
UISplitViewController *mySplitViewController = [[UISplitViewController alloc] init];
mySplitViewController.viewControllers = [NSArray arrayWithObjects:leftside,rightside,nil];
...
mySplitViewController.delegate = self;
....
[windows addSubView:mySplitViewController.view];
....
-(void) viewWillAppear:(BOOL) animated {
}
in myappdelegate.h I included UISplitViewControllerDelegate
I expected viewWillAppear to fire but it is not. I assume if I had subclass UISplitViewControler it would have fire. right?
BTW: I am doing this without using IB. Do I need to set the target for the mySplitViewController?
What I want to do is setup the orientation of the splitviewcontroller when it rotates.
the viewWillAppear method and other view related methods will be called on the view or view controller themselves, not on the delegate.
That means that if you make a subclass of UISplitViewController called SplitViewControllerSubClass, the view... methods will be called on the instance of SplitViewControllerSubClass, not on the delegate object.
But considering you are creating the views and displaying them programmatically, you already know exactly when the view will appear (i.e., right before you add it to the window), so I believe you could do whatever setup you want at that point.

Presenting a view controller modally - iPad

I found this code to display a modal view:
- (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]
initWithNibName:#"RecipeAddView" bundle:nil];
addController.delegate = self;
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:addController];
[self presentModalViewController:navigationController animated:YES];
// The navigation controller is now owned by the current view controller
// and the root view controller is owned by the navigation controller,
// so both objects should be released to prevent over-retention.
[navigationController release];
[addController release];
}
My question is how do I implement this code (I'm going to place it in a buttonPress method)
Do I need to define anything in my header file? The bit that confuses me is that apple on provides this and no header file so i cant tell if anything should be there?
The code refers to RecipieAddViewController what do I repleace this with, "UIViewController" ?
What do I put as the delegate in the headerfile ? do I need to set this up anywhere else ? like with a property?
Is there anything else I need to do once I have copid this code in my buttonPress method to make it work?
Thanks and sorry for all the questions.
My question is how do I implement this code (I'm going to place it in a buttonPress method)
Define the method as an IBAction like -(IBAction)add:(id)sender and in interface builder bind a button's touch up inside event to the view controller object's add: action outlet.
Do I need to define anything in my header file? The bit that confuses me is that apple on provides this and no header file so i cant tell if anything should be there?
Nope. All this stuff needs is UIKit.h You usually need to change your header to add methods, add instance variables, or include custom classes. You may need a #import RecipeAddViewController.h somewhere (in your header or your implementation file) in order to use that class, however. This is true for any custom class you write that you want to use in another file.
The code refers to RecipieAddViewController what do I repleace this with, "UIViewController"?
Replace that with the view controller class you want to push. UIViewController itself is rarely useful naked. It's made to subclassed. So you create a new class that inherits from UIViewController, import it's header, create and instance of it, and push it on the navigation controller.