Can I not 'pushModalViewController'? - iphone

I have a UIViewController class and a second which I want to push modally preferably. However I can't seem to call [self pushModalViewController:...], how come?
What requirements do I need to meet to be able to do so?
I am doing this and getting a black view pushed:
vc = [[ViewController alloc] init];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
I have made my view controller in my storyboard and given it a custom class. I am trying to present this view modally via this class as seen in my code.
Any help much appreciated, thanks.

'push' and 'modal' don't belong together in the same thought. You can:
present a modal view controller, preferably using -presentViewController:animated:completion:, which is the modern replacement for -presentModalViewController:animated:
push a view controller onto the navigation stack, assuming that you're using a UINavigationController. To do that from a view controller, use:
[self.navigationController pushViewController:foo animated:YES];

You're actually looking for [self presentModalViewController:myViewController.view animated:YES]

How are you initing this viewcontroller. From the posted code, I assume it is being initialized to a blank view.
Maybe you can init it from a nibname or something?
vc = [[ViewController alloc] initWithNibName:#"NibName" bundle:nil];
[vc setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:vc animated:YES];
Also another thing to note, if this is a custom class and no nib file, is there any code in initWithCoder? How does the viewDidLoad looks like?

Related

Switching Views gives me a SIGABRT Message

I'm having a little predicament switching between views, here.
Alright, so, I have this view controller class in my iPhone project called "BaseViewController," which is the default, which has a button called "GoToNextView." I added another ViewController to the storyboard called "NextViewController," and then I created another custom view controller class called "NextViewController." Under the inspector window for NextViewController on the storyboard I changed its custom class to "NextViewController;" I'm assuming everything should be hooked up, now. When I click on the "GoToNextView" button, though, the application stops with a SIGABRT message.
Here's the code for my button click action in the BaseViewController class.
- (IBAction)Transition_Next:(id)sender
{
nextViewController = [[NextViewController alloc]
initWithNibName:#"SecondView"
bundle:[NSBundle mainBundle]];
[self.view addSubview:nextViewController.view];
}
What might I be doing wrong, here?
Thanks in advance...
You can use:
- (IBAction)Transition_Next:(id)sender
nextViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:nextViewController animated:YES];
}
Instead of your code and it should work! Hope that helps!
You can use [self.navigationController pushViewController:nextViewController animated:NO]; or if you don't have a navigation controller, you may use
[self presentModalViewController:nextViewController animated:NO]; instead of [self.view addSubview:nextViewController.view];

Storyboard and Switch Views?

I have an iPad app that uses the storyboard board feature and then I have a separate .xib file for another view. I can switch to the separate view and its fine:
-(IBAction)SecondView:(id)sender{
SecondView *Second = [[SecondView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:Second animated:NO];
}
But when I am in the Second View and try going back I do everything the same just with the first view controller, but It just goes to a black screen:
-(IBAction)FirstView:(id)sender{
FirstView *First = [[FirstView alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:First animated:NO];
}
What do you guys think? Am I doing something wrong? What is the best way to switch views?
initWithNibName:bundle is for loading nib or xib files.
If you are loading from a storyboard, you need to use
FirstView *First= [self.storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER"];
IDENTIFIER is defined in your storyboard, it's in the Utilities, Attributes Inspector on the right side.
HOWEVER your real problem is that you shouldn't be loading from the storyboard at all. you should just be calling
[self dismissModalViewControllerAnimated:YES];
That call will clean up the presentModalViewController:animated: that you used to put the modal view controller up in the first place.
You presented SecondView using presentModalViewController:animated:, so you need to dismiss it using dismissModalViewControllerAnimated:.
- (IBAction)FirstView:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}

How can I go back to the first view controller?

I want to go back to the first view controller. So, I used [self.navigationController popToRootViewControllerAnimated:NO] from the third view. But, it goes back to just the second view. Do I have to use popToViewController: animated: instead?
I pushed the third view like this:
[self.view addSubview:secondController.view]; // from the first view
[self.navigationController pushViewController:thirdController animated:YES]; // from the second view
Remove the second view, before using [self.navigationController popToRootViewControllerAnimated:NO]:
UIView *v = [self.navigationController.viewControllers objectAtIndex:1];
[v removeFromSuperview];
EDIT:
I am doing like this and works ok (I use this on my third view on the stack):
NSMutableArray *allControllers = [[NSMutableArray alloc] initWithArray:self.navigationController.viewControllers];
[allControllers removeObjectAtIndex:1];
[self.navigationController setViewControllers:allControllers animated:NO];
[allControllers release];
[self.navigationController popViewControllerAnimated:YES];
It looks like your navigationController was initiated when pushing the thirdController. Your secondController was not 'pushed' by the navigationController, it was added as a subview, which is quite different. So, when you push the thirdController from the secondController, it thinks your rootController is the secondController.
You have two options here:
Change the way you are presenting the secondController to actually
have the navigationController push it, or
Remove the secondController from view before the thirdController is presented.
You may be able to popToViewController, as you mentioned...I'm not positive if that will work, but it's possible.

presentModalViewController and navigationController

I want to know the differences between the
[self presentModalViewController:controller animated:YES];
and
[self.navigationController pushViewController:controller animated:YES];
I have used both but still do not know or noticed the difference. when should use one of them ?
Thanks..
Basic difference :
pushViewController only works in navigation controllers
presentModalViewController works for all view controllers
navigationController is the instance of your UINavigationController, which is used by all the controller in your navigation stack (UIViewController).
Presenting a modal view is presenting a view on top of another view. You perform those typically for "tasks" that need to be started and completed in a self contained way. Read further on modal views on the apple developer guides.
Pushing a view on to the navigation controller is different where there is a logical need for navigation in the app. Say a drill down table as in the setting app of the iDevices, where there are main settings then you drill down to sub settings etc.
Whatever your questions are, if they are conceptual and generic as this I'd strongly advise you to google up "X programming guide" which will take you to the proper Apple programming guide :) X = view controller in your case
http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
if base class has it's own NavigationController then you can write:
[self.navigationController pushViewController:objMyViewController animated:YES];
if your base class has only UIViewController then use:
MyViewController * objMyViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
UINavigationController * navController = [[UINavigationController alloc] initWithRootViewController:objMyViewController];
navController.navigationItem.leftBarButtonItem = nil; // make nil if you want
// to use it in next View
[self presentModalViewController:navController animated:YES];
now, MyViewController has navigation so you can -- Push -- another viewController by writing function as bellow in MyViewController.
-(IBAction)btnNext_click {
SecondViewController * objSecondViewController = [[SecondViewController alloc]initWithNibName:#"SecondViewController" bundle:nil];
[self.navigationController pushViewController:objSecondViewController animated:YES];
}

presentmodalviewcontroller navigationcontroller

I am creating a Navigation based iPhone application.
In that I have called a UiViewController using presentModalViewController. After that, the ViewController becomes visible. From that ViewController I need to call another ViewController using the sample presentModalViewController. Is this possible or not?
What do you mean by "call another uiviewcontroller"? (It really helps if you can be more detailed in your question.) If you mean, "slide in another view controller", then:
MyNewViewController *myNewViewController = [[MyNewViewController alloc] initWithNibName:#"MyNewViewController" bundle:nil];
[navigationController pushViewController:myNewViewController animated:YES];
[myNewViewController release];
...where:
MyNewViewController is the new view controller class that you want to slide in (the above code assumes you have an XIB file for the view controller class).
navigationController points to the current navigation controller. You'll have to replace it with something like [self navigationController], depending where you are in the view hierarchy.
U might be using following line to present a view controller.
//assume name of viewController which u want to present is "myViewController"
[self.navigationController presentModalViewController:myViewController animated:YES]
If you want to push an other ViewController or present an other ViewController then u will need to replace above line with following lines.
//[self.navigationController presentModalViewController:myViewController animated:YES];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:myViewController];
navigationController.navigationBarHidden = YES; //if u want to show navigation bar then remove this line
[self presentModalViewController:navigationController animated:YES];
After using above code you can present or push other view controllers within presented view controller.
Hope it will solve your problem :)