Back button in iphone app

拈花ヽ惹草 提交于 2019-11-29 18:12:38

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!