Can't set outlets when I push a UIViewController without animating - cocoa-touch

I have a UINavigationController with which I push UIViewControllers onto using pushViewController:animated:. After this, I call a method on that incoming view to set some of its IB outlets. Unfortunately, these only get set when I animate the view.
So this will work:
MyViewController *newView = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:YES];
[newView updateOutletsForObject:myObject];
But this won't:
MyViewController *newView = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.navigationController pushViewController:newView animated:NO];
[newView updateOutletsForObject:myObject];
Does anyone know why this would be happening? Thanks in advance.

It seems to me that the parent view controller shouldn't be setting up the child's outlets. You should be handling IBOutlet connections in -viewDidLoad of the child view controller.

Related

Creating multiple subviews inside view controller

I have a view controller named ViewController.
ViewController displays a UIView that has a button on it, which allows me to advance to a second view controller - SecondViewController.
SecondViewController also has a button on it, which allows me to advance to a third view controller.
However, I am having trouble displaying ThirdViewController. When I tap the button in SecondViewController it throws an error:
Warning: Attempt to present ... on ... whose view is not in the window hierarchy!
I have been to a bunch of other sites and posts that address this issue, but cannot seem to find a working solution. I have implemented quite a few solutions, but none seem to work.
Here is my code:
- (void)secondaryView:(UIView *)secondaryView
{
UIView *theView = secondaryView;
UIViewController *viewController = [[UIViewController alloc] init];
viewController.view.frame = [UIScreen mainScreen].bounds;
[viewController.view addSubview:theView];
viewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:viewController animated:YES completion:nil];
}
secondaryView is a UIView that I am constructing elsewhere in the application. I add it to the viewController then present the viewController.
Is there any way to dynamically create UIViewControllers, add a UIView to each one, and add them to the window hierarchy?
If you can use navigation in your ViewControllers, you can try the below code
In the place where you call the firstViewController,
-(void)callFirst
{
FirstViewController *first = [FirstViewController alloc] init];
UINavigationController *navigationController = [UINavigationController alloc] initWithRootViewController:first];
navigationController.modalPresentaionStyle = UIModalPresenationFormSheet;
[self presentModalViewController:navigationController animated:YES];
}
And in the FirstViewController button action ,you can write
-(void)callSec
{
SecondViewController *sec = [SecondViewController alloc] init];
[self.NavigationController pushViewController:sec animated:YES];
}
And in SecondViewController you can do the same
-(void)callThird
{
ThirdViewController *third = [ThirdViewController alloc] init];
[self.NavigationController pushViewController:third animated:YES];
}
Try this...And in these ViewControllers you can do whatever coding you want to meet the requirement like
-(void)viewDidLoad
{
UIView *newView = [[UIView alloc] initWithFrame:CGRectMake(20,20,400,400)];
newView.backGroundColor = [UIColor redColor];
[self.view addSubView:newView];
}

IOS - Load unknown type of UIViewController

I'm trying to load UIViewController, but not knowing the type until runtime.
So I have some different UIViewControllers, each one with it's own .xib, and with the
following code, the .xib is showed correctly, but the UIViewController is never created, because
it's a child of UIViewController, not an exactly UIViewController:
UIViewController *vc = [[UIViewController alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
It works fine for example in this particular case, but obviously not for the rest:
Controller1 *vc = [[Controller1 alloc] initWithNibName:[[self component] xibName] bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
How can I solve it?
Thanks!
you can put your controller classe name in an NSSTring and create the object like this
UIViewController *vc=[[NSClassFromString(ControllerclassName) alloc] init];
[self.navigationController pushViewController:vc animated:YES];

Pushing UIViewController in navigationcontroller shows empty view

I am facing the problem that when I push my viewcontroller in the navigationcontroller all it does is changing the navigationbar and showing the standard background but no view is to be seen. My code for pushing the viewcontroller into the navigationcontroller looks like:
Viewcontroller1 *viewController = [[Viewcontroller1 alloc] autorelease];
[[self navigationController] pushViewController:viewController animated:YES];
But, if I initialise the viewcontroller by creating IBOutlets for it, it works just fine. But the reason why I am not doing that is because I later on needs to pass some parameters to the class on initialization.
Thanks for your time.
You have to initialise your view like
if you have xib then
Viewcontroller1 *viewController = [[[Viewcontroller1 alloc] initWithNibName:#"Viewcontroller1" bundle:nil]autorelease];
[[self navigationController] pushViewController:viewController animated:YES];
if you haven't xib
Viewcontroller1 *viewController = [[[Viewcontroller1 alloc] init]autorelease];
[[self navigationController] pushViewController:viewController animated:YES];

UINavigationController as ModalView

I have in my app class UINavigationController (with NIB) and I want to open this as a ModalView.
I do that so: (ColorInfo is just this UINavigationController class)
ColorInfo *InfoScreen = [[ColorInfo alloc] initWithNibName:#"ColorInfo" bundle:[NSBundle mainBundle]];
[InfoScreen setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:InfoScreen animated:YES];
It displays me an empty UINavigtionController element. Tell me please how could I open ColorInfo (as UINavigationController) in ModalView, where I will be able to navigate through?
I thought I can complete ColorInfo with all pushViewController methods and then open it over another screen in ModalView.
Tell me how to reach this effect.
From what you've stated you've inherited UINavigationController in ColorInfo which is not what you want. You want ColorInfo to be the root view of a UINavigationController like so
ColorInfo *InfoScreen = [[ColorInfo alloc] initWithNibName:#"ColorInfo" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:InfoScreen];
[self presentModalViewController:nav animated:YES];
I recommend you read the navigation controller documentation for further clarity.
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
Use this with InfoScreen controller and DON'T subclass UINavigationController ...
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:InfoScreen];
[navController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:InfoScreen animated:YES];

ModalViewController with embedded nav controller - Unable to dismiss

I present a modalViewController that is actually a navigation controller with one view, and a custom navigation bar. The modal view appears fine as expected, but when I attempt to remove it from view using [self dismissModalViewControllerAnimated:YES], I am hitting a "-[UINavigationController modalViewController]: message sent to deallocated instance". Can't seem to figure this out. Any ideas?
Instantiating the ModalViewController:
// Make a navigation controller and add the view inside it
MyViewController *evc=[[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
//UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:evc];
UINib *nib = [UINib nibWithNibName:#"UINavigationBarWithBackgroundImage" bundle:nil];
UINavigationController *nvc = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0];
[nvc setViewControllers:[NSArray arrayWithObject:evc]];
evc.delegate=self;
[evc release];
[self presentModalViewController:nvc animated:YES];
[nvc release];
and trying to remove it. This is where the error comes in:
[self dismissModalViewControllerAnimated:YES];
Not sure about this, but try it anyway:
Remove
[nvc release]
and see if
[self dismissModalViewControllerAnimated:YES];
now works.
Is there a reason you are loading two seperate nibs to show this modal? You do not need to load a nib containing a navigation controller to get this working.
Try something like this:
// Make a navigation controller and add the view inside it
MyViewController *evc= [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:evc];
evc.delegate=self;
[self presentModalViewController:navController animated:YES];
[evc release];
[navController release];