Storyboard and Switch Views? - iphone

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];
}

Related

Navigate from a ViewController to another

I have this design in my storyboard:
As you see the middle ViewController is connected to a Navigation Controller ( I just made the navigation bar invisible). In this middle page I add all the controls programmatically ( using Parse mobile platform login wizard ).
The thing is I want to navigate to the third page, when Login is done successfully.
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES];
ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
printf("%s", [#"Ali" UTF8String]);
}
the message will be printed, but the navigation is failed. Can you help? Perhaps I need to fix something in storyboard.
I'm not sure you should be sending the dismissModalViewControllerAnimated: message. Are you presenting another view controller modally on top of your login view controller?
Anyway, when you have configured a view controller in a storyboard, you can't create the view controller using alloc and init. You need to ask the storyboard to create it. There are a couple of ways to do this.
One way
One way to get the storyboard to create your ViewController2 is to make a push segue in your storyboard.
Open your storyboard.
Control-drag from the login view controller to the ViewController2.
Choose the “push” segue type.
Click on the segue.
Choose View > Utilities > Show Attributes Inspector.
In the Attributes Inspector (right-hand side of the window), set the segue identifier to “didLogIn”.
To execute the segue, do this:
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES];
[self performSegueWithIdentifier:#"didLogIn" sender:self];
}
Another way
Another way to get the storyboard to create your ViewController2 is to give it a storyboard ID and ask the storyboard to instantiate the view controller by ID. Then you can push the view controller.
Before you can ask the storyboard to create it, you must give a “storyboard ID” to the ViewController2 instance in your storyboard.
Open your storyboard.
Select the ViewController2 instance.
Choose View > Utilities > Show Identity Inspector.
In the Identity Inspector (right-hand side of the window), enter “viewController2”. Case is important!
Then, in your code, ask the storyboard to instantiate viewController2:
- (void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissModalViewControllerAnimated:YES]; // Should this be here?
ViewController2 *viewController = [[[ViewController2 alloc] init] autorelease];
[self.navigationController pushViewController:viewController animated:YES];
}
Try this way,
ViewController2 *myViewController=[storyboard instantiateViewControllerWithIdentifier:#"TheNameOfYourController"]
[self.navigationController pushViewController:myViewController animated:YES];
Because a lot of relevant code/settings is missing I would suggest the following:
Use this code:
-(void)logInViewController:(PFLogInViewController *)logInController didLogInUser:(PFUser *)user {
[self dismissViewControllerAnimated:YES completion:NULL];
Class klass = [[NSBundle mainBundle] classNamed:#"ViewController2"];
ViewController2 *viewController = [[klass alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
[viewController release];
NSLog("%#", #"Ali");
}
If that doesn't work check how you suppose to initialize ViewController2.
If all this doesn't help please try to post more relevant code.
To move next viewController using Storyboard Identifier
nextViewController *objnextViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"nextViewControllerIdentifier"];
[self.navigationController pushViewController: objnextViewController animated:YES];

switching view black page

I'm trying to do a switch view. My first view is a storyboard and i want to switch for view2, i have no problem when click the botton in my first view everything is fine. The prolem is when i am trying to go back, the screen is going black and is not going back to the first view. Here the code that i'm using.
ViewController.h
- (IBAction)View2:(id)sender;
ViewController.m
- (IBAction)View2:(id)sender {
View2 *second = [[View2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
And here the code that i am using to go back from the second view to the first.
view2.h
- (IBAction)back:(id)sender;
View2.m
- (IBAction)back:(id)sender {
ViewController *second = [[ViewController2 alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:second animated:YES];
}
Am I doing some kind of error?
Thanks
Your issue is in View2.m
- (IBAction)back:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
should do the trick
Apple has a View Controller Programming Guide that I suggest skimming through
UPDATE
If you need to jump back more that one ViewController, you should use NSNotificationCenter or keep a reference to the viewController you need to jump back to.
In my experiences, the following line does 1 of 2 things (where self is a subclass of UIViewController)
[self dismissModalViewControllerAnimated:YES];
1) If self has presented another view controller, the line above will dismiss all view controllers that have been presented on top of self
2) If self has not presented any other view controller, the line above will dismiss self back to the previous view controller

Can I not 'pushModalViewController'?

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?

How can I go back to the first view from the third view directly?

When I push cancel button in the third view, I want to go back to the first view directly.
I also want to remove the second view.
How can I do that?
This is the code.
// this part is in the first view.
self.second = [SecondController alloc] init];
[self.view addSubview:second.view];
// this part is in the second view.
ThirdController *thirdController = [[ThirdController alloc] initWithStyle:UITableViewStyleGrouped];
self.navigationController = [UINavigationController alloc] initWithRootViewController:thirdController];
[self.view addSubview:navigationController.view];
// this part is in the third view.
- (void)cancel {
[self.view removeFromSuperview]; // this only goes to the second view.
}
EDIT:
Can I use popToViewController in called contoller? My app crashes.
I thought popToViewController can be used only in calling controller.
And popToViewController is used when it was pushed.
I did add not push.
[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:0] animated:YES];
popToViewController:animated: is a UINavigationController method that you use when popping view controllers off the navigation controller stack. It doesn't fit for this scenario.
This user is adding subviews, not pushing them on a navigation controller stack.
As a note, it appears as a matter of design you should be using a navigation controller with the first view as the root controller, then the second pushed on the stack, and the third pushed on the stack. Then all you have to do is [self.navigationController popToRootViewControllerAnimated:YES].
I think this will work if you want to keep your current architecture:
// this part is in the third view.
- (void)cancel {
// remove the second view (self.view.superview) from the first view
[self.view.superview removeFromSuperView];
// can't recall, possibly you still need to remove the third view, but i think removing the superview will do it.
// [self.view removeFromSuperView];
}
If you prefer to try the UINavigationController route, then the easiest path is to create a new project in Xcode and select the type for a Navigation-Based Application or a Master-Detail Application. This will create a UINavigationController in a nib and add it to your window. You can then set the root view controller in Interface Builder to your FirstViewController class.
If you prefer to create the UINavigationController in code, then that is also possible. I show that below, along with the rest of the code you need, regardless of whether you create your UINavigationController in a nib in IB or in code.
I also recommend reading the View Controller Programming Guide for iOS.
In your app delegate or some other code:
-(void)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions [
// I would recommend setting up the UINavigationController and FirstViewController as IBOutlets in your nib, but it can be done in code.
FirstViewController* fvc = [[FirstViewController alloc] initWithNibName:#"FirstView" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:fvc];
[window addSubView:navController.view];
[window makeKeyAndVisible];
[fvc release];
[navController release];
}
In the first view controller:
SecondViewController* svc = [[SecondViewController alloc] initWithNibName:#"SecondView" bundle:nil];
[self.navigationController pushViewController:svc animated:YES];
[svc release];
In the second view controller:
ThirdViewController* tvc = [[ThirdViewController alloc] initWithNibName:#"ThirdView" bundle:nil];
[self.navigationController pushViewController:tvc animated:YES];
[tvc release];
In the third view controller:
-(void)cancel {
// returns to the first view controller
[self.navigationController popToRootViewControllerAnimated:YES];
}
Use
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
to go back to a specific view controller.
Try this:
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];
This will pop to the view at index 1. Hope that Helps!
// this part is in the third view.
- (void)cancel {
self.first = [SecondController alloc] init];
[self.view addSubview:second.view];
}
And I think if you have you don't need to be worried about removing beneath view, later these will removed.

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 :)