Uimodalcontroller: Modal window (one controller modal out of another controller's modal window)
When we display view Controller B in modal mode in view Controller A, a acts as a presenting view controller (eject VC), and B is the presented view Controller (the VC is ejected). The official documentation suggests that the interaction between the two is achieved through delegate, defining delegate in the VC that is ejected, and then implementing the proxy in the popup VC, which makes it easier to interact between the two.
1. Manage the view in the form of a modal window, and the contents of other views cannot be manipulated until the current view is closed.
2. Get different modal windows (main attributes)
@property (nonatomic,readonly) Uiviewcontroller *presentedviewcontroller; The window that the current controller is modal out of.
@property (nonatomic,readonly) Uiviewcontroller *presentingviewcontroller; Modal out of the current controller window
3. Processing modal window (Main method)
-(void) Presentviewcontroller: (Uiviewcontroller *) viewcontrollertopresent animated: (BOOL) flag completion: (void (^) (void)) completion); //Display the modal window you want to display
-(void) dismissviewcontrolleranimated: (BOOL) flag completion: (void (^) (void)) completion); //Close the currently displayed modal window
-(void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) //The method that is called when the current controller modal another window and transmits data
4, the mode of the window to achieve the view of the switch, that can be in the Stroybord through the modal connection to the way a few uicontroller connected to the button event to achieve the switch, You can also use pure code to switch several Uicontroller in storyboard after connecting via button events.
Example: Two view switches (Firstviewcontroller, Secondviewcontroller), modal windows, and output data
Class Association:
Secondviewcontroller Firstviewcontroller
When the controller is connected directly through the modal mode in the storyboard, the Segue property identifier in storyboard needs to be set to modal so that the current controller can modal the modal window of the other controller and transmit the data.
Step one: Drag out two view controllers into storyboard and connect modal mode
Step two: Set the Segue property identifier identifier in the storyboard
Step three: Modal out of window in Firstviewcontroller controller and only forward transfer data
Fourth step: Close the modal window in Secondviewcontroller and accept the data
When the controller in the storyboard is not connected directly through the modal mode, it can be connected by code, and the storyboardid of the identify of the controller in the storyboard to be modal out of the window needs to be set an identity. Then, first create the controller to be modal, that is, according to the storyboard object according to the identity to load the controller to be modal window, finally the modal window and transfer data. When transferring data in this way, if it is more than two control to implement the view switch, because the controller coupling is too strong, there will be wrong error, is not recommended to use. But by setting up the agent to realize the transmission of data, that is, in the Controller of the modal window set up the protocol and proxy, in the modal window controller to implement the agent method, greatly reducing the coupling between the controller.
"1" Toggle view, no proxy for forward and reverse data transfer
Step one: Drag out two view controllers to storyboard, not connected
The second step: the storyboard in the controller to be modal out of the window Secondviewcontroller identify Storyboardid set an identity
Step three: Modal out of the window in the Firstviewcontroller controller, and transmit the data forward
1 #import "ViewController.h"2 #import "SecondViewController.h"3 @interfaceViewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitextfield *TextField;5 6 @end7 8 @implementationViewcontroller9-(void) Settextwithbackinfo: (NSString *) InfoTen { OneSelf. Textfield.text =info; A } -- (void) Viewdidload { - [Super Viewdidload]; the } - //Pure code creation modal display --(Ibaction) forwardclicked: (UIButton *) Sender - { + //first create the controller for the modal display - //loading the controller from the storyboard +Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@"Main"Bundle:nil]; A at //match identifier for storyboard -Secondviewcontroller *SECONDVC = [Storyboard Instantiateviewcontrollerwithidentifier:@"Second"]; - - //forward-passing data -Secondvc.info =Self . Textfield.text; - in //Display modal window - [self PRESENTVIEWCONTROLLER:SECONDVC animated:yes completion:nil]; to } + - @end
Fourth step: Close the modal window in Secondviewcontroller and transfer the data in reverse
"2" Toggle view, data forward and reverse transfer via proxy
The main code is as follows:
Code in the Firstviewcontroller controller:
1 #import <UIKit/UIKit.h>23@interface viewcontroller: Uiviewcontroller45 -(void) Settextwithbackinfo: (NSString *) info; 6 @end
1 #import "ViewController.h"2 #import "SecondViewController.h"3 @interfaceViewcontroller () <SecondViewControllerDelegate>4@property (Weak, nonatomic) Iboutlet Uitextfield *TextField;5 6 @end7 8 @implementationViewcontroller9-(void) Settextwithbackinfo: (NSString *) InfoTen { OneSelf. Textfield.text =info; A } -- (void) Viewdidload { - [Super Viewdidload]; the } - //Pure code creation modal display --(Ibaction) forwardclicked: (UIButton *) Sender - { + //first create the controller for the modal display - //loading the controller from the storyboard +Uistoryboard *storyboard = [Uistoryboard storyboardwithname:@"Main"Bundle:nil]; A at //match identifier for storyboard -Secondviewcontroller *SECONDVC = [Storyboard Instantiateviewcontrollerwithidentifier:@"Second"]; - - //forward-passing data -Secondvc.info =Self . Textfield.text; - in //in order to accept the reverse data, set itself as the proxy of the modal window, it is good to reduce the coupling of modal window -SECONDVC.Delegate=Self ; to + //Display modal window - [self PRESENTVIEWCONTROLLER:SECONDVC animated:yes completion:nil]; the } * $ #pragmaProxy methods for Mark-secondviewcontrollerPanax Notoginseng //data returned by proxy-accepted modal window --(void) SECONDVC: (Secondviewcontroller *) VC Didfinishedwithinfo: (NSString *) Info the { +Self. Textfield.text =info; A } the @end
Code in the Secondviewcontroller controller:
1 #import<UIKit/UIKit.h>2 @protocolsecondviewcontrollerdelegate;3 @interfaceSecondviewcontroller:uiviewcontroller4@property (Weak, nonatomic) Iboutlet UILabel *label;5@property (copy,nonatomic) NSString *info;6 //2. Add Agent Properties7@property (weak,nonatomic)ID<SecondViewControllerDelegate>Delegate;8 @end9 Ten One //1. Defining the Agreement A @protocolsecondviewcontrollerdelegate - --(void) SECONDVC: (secondviewcontroller*) VC Didfinishedwithinfo: (nsstring*) info; the - @end
1 #import "SecondViewController.h"2 3 @interfaceSecondviewcontroller ()4@property (Weak, nonatomic) Iboutlet Uitextfield *TextField;5 @end6 7 @implementationSecondviewcontroller8-(Ibaction) backbuttonclicked: (UIButton *) Sender9 {Ten //Self.presentedviewcontroller The current controller's modal window One //Self.presentingviewcontroller modal out of the current controller window A - - //3. Reverse data transfer via proxy the[Self.Delegatesecondvc:self DidFinishedWithInfo:self.textField.text]; - - - //Close modal window + [Self.presentingviewcontroller dismissviewcontrolleranimated:yes completion:nil]; - + } A at- (void) Viewdidload { - [Super Viewdidload]; - //Do any additional setup after loading the view. - -Self.label.text =Self.info; -}
The final demo results are:
IOS: The first way to view switching: Modal view