I have a simple single View application with a button in the main view which sends me off to another view with an independent xib file. On this second view I have a button that should send me back to the main view. I'm not using a navigation controller.
Can anyone tell me how to program this "back" button so it will display the main view again?
Thanks so much in advance.
If you called the second viewcontroller like this:
[self presentModalViewController:secondViewController animated:YES];
You can dismiss it like this:
[self dismissModalViewControllerAnimated:YES];
You may want to consider using a delegate. Creating a delegate will be a little bit of work, but will lead to a much more flexible platform where you can then send information to the first view controller based on things that were changed or updated in the secondary view controller. This is more complicated than TommyG's answer of presenting the second view controller as a modal view, but it is probably more flexible at the same time. You need to create a generic protocol (I prefer to create them in a separate objc header file because I'm anal) that looks something like this:
//In YouPressMyButtons.h
#import <Foundation/Foundation.h>
@protocol YouPressMyButtons <NSObject>
- (void)secondViewBackButtonPressed:(id)sender;
@end
You then want to make the first view controller (the main view) conform to the protocol and implement the secondViewBackButtonPressed: method in the header file of the first view controller (the main view) like this:
//In FirstViewController.h
@interface FirstViewController : UIViewController <YouPressMyButtons>
- (void)secondViewBackButtonPressed:(id)sender;
@end
In the first view controller implementation file you need to actually create the method somewhere and tell it to dismiss the second view like this:
//In FirstViewController.m somewhere down at the bottom
//(or on top if you prefer it that way;)
- (void)secondViewBackButtonPressed:(id)sender {
//Dismiss your second view controller in here
return;
}
Then in the second view controller all you need to do is create the delegate and call this method when you button is pressed. In the second view controller header file you need to create the delegate like this:
//In SecondViewController.h
@interface SecondViewController : UIViewController {
__weak id <FCPViewSwap> delegate; //I am assuming you are using ARC like me :)
}
@property (weak, nonatomic) id<YouPressMyButtons> delegate;
- (IBAction)backButtonPressed:(id)sender;
@end
Lastly, in the second view controller implementation file you just need to call the delegate method and it will send you back to the first view controller method that you created above like this:
//Somewhere in SecondViewController.m
- (IBAction)backButtonPressed:(id)sender {
NSLog(@"Back button pressed. Forwarding to parent controller.");
[self.delegate secondViewBackButtonPressed:sender];
}
I haven't seen many good explanations of creating delegates and using them to send information between view controllers, so I thought I would explain it as best as I could here. This is one of the most powerful ways to send any information you want between view controllers. You can expand the method declared in the protocol to have as many variables passed through as you want. You can also add methods and properties to the protocol to make it more useful. Just keep in mind that you need to implement every method and property in the protocol unless you separate them into @required and @optional sections (see Apple's documentation for a good explanation on protocols and using the @optional specifier). Apple Documentation on Protocols. Hope this helped!
If you have just two views, consider using "utility app" template in xcode
来源:https://stackoverflow.com/questions/10272843/back-button-in-iphone-app