I'm writing an iPhone app and I start with a main view and when I click a button it switches to my second view using this code...
- (IBAction)addNewCourse
{
AddCourses *addCourseController = [[AddCourses alloc] initWithNibName:#"AddCourses" bundle:nil];
[self presentModalViewController:addCourseController animated:YES];
}
Now I am having problems switching BACK to the original view. I use this code...
- (IBAction) back
{
[self.parentViewController dismissViewControllerAnimated:YES completion:nil];
}
but it doesn't seem to work. Any help would be greatly appreciated. Thanks
Try this code:
[self dismissViewControllerAnimated:YES completion:nil];
Related
I am trying to create an app that has a navigation bar at the bottom of the first view controller. When the user presses next they are taken to the second view controller in the storyboard. I would like to know if it is possible to make the program automatically take the user back to the first view controller after 10 seconds? I am very new to xcode so any help implementing this in a simple fashion would be greatly appreciated
just create a function and call that after time delay and push in that
in second viecontroller
- (void)viewDidLoad
{
[super viewDidLoad];
[self performSelector:#selector(yourMethod) withObject:nil afterDelay:10.0];
}
-(void)yourMethod
{
//[self.navigationController popViewControllerAnimated:YES];
[self dismissViewControllerAnimated:YES completion:nil];
}
hope this will help you
Add the below to viewDidLoad in your second view controller
- (void)viewDidLoad {
[super viewDidLoad];
[self performSelector:#selector(viewWillDisappear:) withObject:nil afterDelay:2.0];
//I have given 2 seconds. you can change as your wish
}
Add this method too below.
-(void) viewWillDisappear:(BOOL)animated{
[self dismissViewControllerAnimated:YES completion:nil];
}
Hope it works..
I am trying to switch views in my application. I have the following code to take me from the Main View to the first level view:
-(IBAction)levelOneButton
{
levelOneView *testView = [[levelOneView alloc] initWithNibName:nil bundle:nil];
testView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:testView animated:YES completion:NULL];
}
Which works GREAT! Then the Code to take me to the next level is the exact same but slightly different!
-(IBAction)nextLevelButton
{
LevelTwoView *levelTwo = [[LevelTwoView alloc] init];
levelTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:levelTwo animated:YES completion:nil];
}
Now when I want to go back to the Main menu from here is where the issue occurs. I have the following code which only dismisses the level2 and shows level1 again.
- (IBAction)goBackButton {
[self dismissViewControllerAnimated:YES completion:NULL];
}
Like I said when this code is used it just releases the current view and takes you back to the last view instead of taking you back to the main menu. After realizing it was not working I tried the following code to take me back to the main menu.
- (IBAction)goBackButton {
ViewController *mainMenu = [[ViewController alloc] init];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mainMenu animated:YES completion:nil];
}
When this code is run, I get presented with a black screen.
My question remains how may I go back to the view ViewController? Thank you so much!!
Try this when dismissing the second level view controller.
[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES]
MainMenuViewController presents BonusViewController modally. I want to Dismiss BonusViewController then display a new BonusViewController, effectively "resetting" BonusViewController.
Im using notifications to call this method in MainMenuViewController
-(void)resetBonus{
[self dismissViewControllerAnimated:YES completion:nil];
[self presentViewController: BonusViewController animated:NO completion:nil];
}
I expected BonusViewController to be auto-detected as I was typing it in the presentViewController call above but it was not and none of my viewControllers show up as I type which Im assuming means im doing this all wrong. Do I have to initialize the VC or allocate it before I can present it like this? Or can I even do this at all since im using storyboards?
I also tried this though I believe this method is deprecated
-(void)resetBonus{
[self dismissModalViewControllerAnimated:YES];
[self presentModalViewController: BonusViewController animated:NO ];
}
Did what Sumanth suggested but I get this message now:
so now I m doing :
#import "BonusViewController.h
....
-(void)resetBonus
{
BonusViewController *bonus = [[BonusViewController alloc]init];
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController: bonus animated:NO ];
}
the errors are all gone but when BonusViewController is presented the display is solid black, i can hear the sounds going but cant see anything on the screen
you should allocate and initialize the viewcontroller and present that using presentModalViewController write like this
BonusViewController *bonus = [[BonusViewController alloc]init];
[self dismissModalViewControllerAnimated:YES];
[self presentModalViewController: bonus animated:NO ];
Also dont forget to write #import "BonusViewController.h" in .h file
BonusViewController *objBonus = [[BonusViewController alloc]init];
[self dismissViewControllerAnimated:YES completion:NULL];
[self presentViewController:objBonus animated:YES completion:NULL];
You need to create and object of the viewcontroller and you are directly passing the viewcontroller which is not possible
#import "BonusViewController.h"
-(void)resetBonus
{
BonusViewController *BonusViewController = [[BonusViewController alloc]init];
[self dismissViewControllerAnimated:NO completion:nil];
[self presentViewController: BonusViewController animated:NO completion:nil];
}
Set Both of animated is NO
EDIT : #import "BonusViewController.h"
Im sure the answers given were correct but I could not get them to work properly. For some reason BonusViewController seems to be displayed as I can hear the sounds going but the screen is black and nothing is displayed visually.
I ended up doing the below for now in the intrest of time but this isnt the smooth effect I was really going for.
I have to re-visit this when I have more time to figure out a better way to do it.
-(void)endBonus
{
[self dismissViewControllerAnimated:NO completion:^{
[self performSelector:#selector(resetBonus) withObject:nil afterDelay:1];
}];
}
-(void)resetBonus
{
[self performSegueWithIdentifier: #"segueToBonus" sender: self];
}
I have a tab bar controller with a view inside a navigation controller. One of the buttons on this pops up a modal view. I set my starting view as the delegate for the modal, and call the following:
- (void)dischargeSaveComplete:(dischargeView *)controller
{
[self dismissViewControllerAnimated:YES completion:nil];
[self.navigationController popViewControllerAnimated:YES];
}
It correctly dismisses the modal view, but it doesn't call the back button. Do I need to do something else since it's inside a tab bar controller?
I tried set both to animation no as seen below, and it doesn't work either.
- (void)dischargeSaveComplete:(ehrxEncounterDischargeView *)controller
{
[self dismissViewControllerAnimated:NO completion:nil];
[self.navigationController popViewControllerAnimated:NO];
}
Found the solution based on one of the answers, because I was in a tab bar controller, I had to call the popviewcontroller from the first view as seen below:
- (void)dischargeSaveComplete:(ehrxEncounterDischargeView *)controller
{
[self dismissViewControllerAnimated:YES completion:^(void)
{
demoView *e = [self.parentViewController.tabBarController.viewControllers objectAtIndex:0];
[e.navigationController popViewControllerAnimated:YES];
}];
}
You want 2 animations to follow one another, which is not allowed as you did it. You either have to cancel one of the animation or place popViewController inside the completion block for your first animation.
[self dismissViewControllerAnimated:YES completion:^(void) {
[self.navigationController popViewControllerAnimated:YES];
}
];
u can try delay in performing second action
[self.navigationController performSelector:#selector(popViewControllerAnimated:) withObject:#"YES" afterDelay:1];
hope it works.. happy coding :)
I read a lot of documentation about this before i decided to ask.
So I have navigationController in my app. When user enters the first time in my app I do this
RegViewController *opr = [[RegViewController alloc] init];
self.regController = opr;
[self.navigationController presentModalViewController:self.regController animated:NO];
[opr release];
And it works fine. But when I click OK button in RegViewController I call this method
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self.navigationController dismissModalViewControllerAnimated:YES];
//[self.navigationController.parentViewController dismissModalViewControllerAnimated:YES];
}
But this code doesn't want to dismissModalViewController
Can somebody help me with thios issue please. Thanks
try to change your call to (remove the navigationController):
[self presentModalViewController:self.regController animated:NO];
and in your button (remove the navigationController):
[self dismissModalViewControllerAnimated:YES];
You are telling to your navigationController dismiss your modal, but your viewcontroller should do it.
Navigation controllers are used to navigation (go and back) purposes. And probably, your navigationController is nil.
In your RegViewController, try to dismiss self like so:
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self dismissModalViewControllerAnimated:YES];
}