I need help figuring out how to change out the view in my application. I have a wonderfully working view that I have finished and now I'd like to be able to switch the view to a brand new, blank white screen to display.
I have these files:
HelloAppDelegate.h,
HelloAppDelegate.m,
HelloViewController.h, and
HelloViewController.m
Then, I added a new View Controller so now I have two more files:
SecondViewController.h and
SecondViewController.m
In my first view (HelloViewController), I have a button. When the user presses this button, I'd like SecondViewController to show up. So, in my HelloViewController.m, I have an action method
-(IBAction)switchToSecondView:(id)sender {
}
In this method, how can I go about initializing my second view and displaying it?
Thanks in advance!
If you want to do something like flipping view, making it modal and then returning back to the main view do following:
Define a delegate to indicate that secondary view finished its work
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish;
#end
In main view do following:
- (void)flipsideViewControllerDidFinish {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
In secondary view do following:
- (IBAction)done {
[self.delegate flipsideViewControllerDidFinish];
}
Related
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
I'm not sure how to do this. So I originally had a ViewController that had one .xib, with one main view. I present it like this:
DogViewController *dvc = [[DogViewController alloc] initWithNibName:#"DogViewController" bundle:nil];
dvc.modalPresentationStyle = UIModalPresentationFormSheet;
dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:dvc animated:YES];
[dvc release];
So that works fine. However now from a button press in the DogViewController.xib, I want to dismiss the current form sheet, and show another form sheet with some additional questions before proceeding. So I started by adding another view to in my original .xib of DogViewController, then got stuck in the logic of how to dismiss the first one, and show the second one. I'm assuming I need some outlet to the new view in the same .xib, but from there I'm lost. Thanks.
The way to do this would be to set it up with a UINavigationController as Mathiew mentions. However, if you really want to transition between two views on one view controller, you can refer to this sample code from Apple:
http://developer.apple.com/library/ios/#samplecode/ViewTransitions/Introduction/Intro.html
The code uses ImageViews to demonstrate the effect but I don't see why you can't use views instead :)
You can add a view within the other view in front of all of the other objects and just use its hidden property to control whether it's shown or not.
Why don't you use a navigation controller in your modal view, create another xib and do a [self.navigationController pushViewController:secondViewController animated:YES];
If you have a good reason, you can set a second view outlet secondView and use code like
UIView* superview = [self.view superview];
[self.view removeFromSuperView];
[superview addSubview:self.secondView];
Very simple solution is to hold reference to MainViewController and call methods on it that swap between two view controllers.
Like this:
#implementation MainViewController
- (void)showDogViewController {
[self dismissModalViewControllerAnimated:YES];
DogViewController *dvc = [[DogViewController alloc] initWithNibName:#"DogViewController" bundle:nil];
dvc.modalPresentationStyle = UIModalPresentationFormSheet;
dvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
dvc.mainViewController = self;
[self presentModalViewController:dvc animated:YES];
[dvc release];
}
- (void)showCatViewController {
[self dismissModalViewControllerAnimated:YES];
CatViewController *cvc = [[CatViewController alloc] initWithNibName:#"CatViewController" bundle:nil];
cvc.modalPresentationStyle = UIModalPresentationFormSheet;
cvc.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
cvc.mainViewController = self;
[self presentModalViewController:cvc animated:YES];
[dvc release];
}
}
#end
#implementation DogViewController
- (void)showCatViewController {
[mainViewController showCatViewController]
}
#end
#implementation CatViewController
- (void)showDogViewController {
[mainViewController showDogViewController]
}
#end
My FlipViewController has some setting in form of UISwitches for MainViewController. I am trying to set those settings (based on variables) when FlipViewController is loaded and save those settings when MainViewController is loaded back (from FlipView controller). Something is funky and i can't get it to work consistently.
It looks like when i call FlipViewController from MainViewController, FlipView is loaded before i can set the states of the switches in FlipView. How can i accomplish doing this?
Thanks in advance!
Vatsal
Function within MainViewController
// called when the user touches the info button
- (IBAction)showInfo
{
// create a new FlipsideViewController
FlipsideViewController *controller = [[FlipsideViewController alloc]
initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self; // set the delegate
// set the animation style to a horizontal flip
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// show the flipside of the app
[self presentModalViewController:controller animated:YES];
// set the controls on the flipside
[controller setSwitches]; // set each region's switch
//[controller setSelectedIndex:guessRows - 1]; // set number of choices
controller.wordsDatabase=self.wordsDatabase;
[controller release]; // release the controller FlipsideViewController
} // end method showInfo
Function setting up switches in FlipView Controller (Called in MainView controller)
-(void) setSwitches
{
}
Looks to me like your FlipSideViewController's setSwtiches method is called before you've assigned the controller a reference to the wordsDatabase.
ex:
[controller setSwitches];
controller.wordsDatabase=self.wordsDatabase;
should be:
controller.wordsDatabase=self.wordsDatabase;
[controller setSwitches];
I am currently using this code to bring up an info view for an iPhone app.
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:info animated:YES];
[info release];
}
And this code go back to my main view
-(IBAction) exitInfo:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated: YES];
}
When I run it on the simulator, it goes fine, however, on the actual iPhone, when I press the button that triggers the exitInfo method, it carries out its animation, but once it is completely off the screen and only my main view is visible, the screen will flicker, quickly showing the info view again. How can I fix this?
In my opinion, your viewController shouldn't even know it is modally viewed.
Try putting the code that send the dismissModalViewController message in your main controller and alert your main controller that the user clicked the dismiss button using a delegate.
something like this in your "parent" viewController
-(IBAction)showInfo:(id)sender {
InfoView *info = [[InfoView alloc] initWithNibName:nil bundle:[NSBundle mainBundle]];
info.modalTransitionStyle = UIModalTransitionStylePartialCurl;
info.delegate = self;
[self presentModalViewController:info animated:YES];
[info release];
}
-(IBAction)closedButtonClicked {
[self dismissModalViewControllerAnimated: YES];
}
and in your "Modal" viewController
-(IBAction) exitInfo:(id)sender {
[delegate closedButtonClicked];
}
Of course you will need a protocol like
#protocol MyCustomDelegate
-(IBAction)closedButtonClicked;
#end
and something like this in your "modal" interface
-id<MyCustomDelegate> delegate;
I need to hide a detail view that is shown when clicked on a tableviewitem
- (IBAction)done:(id)sender {
[self.delegate OrderDetailsViewDidFinish:self];
}
It is all connected up with in the, xib and .h but the view doesnot close, it is loaded via this code and loads great:
//Initialize the detail view controller and display it.
OrderDetailsView *dvController = [[OrderDetailsView alloc] initWithNibName:#"OrderDetailsView" bundle:[NSBundle mainBundle]];
dvController.selectedOrder = (#"%#",selectedOrder);
[self presentModalViewController:dvController animated:YES];
[dvController release];
dvController = nil;
the trouble comes when closing it, please not i ahve all the correct .h in the detail view
Thanks
Mason
If you present a controller as a modalViewController then when hiding it you only need this:
- (IBAction)done:(id)sender {
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
When you use modals a Parent-Child relation is created between two controller (the newly presented is the child) so you can call the previous controller with self.parentViewController from this new controller ;)