iOS Development UI Chapter-modal Brief Introduction
First, Brief introduction
In addition to push, there is another kind of controller switching mode, that is modal
Any controller can be modal in the form of an exhibition?
Modal default effect: The new controller drills up from the bottom of the screen until it covers the previous controller.
Second, the code description
Create a new project to add windows and controllers to the application agent.
YYAPPDELEGATE.M file
1//2// YYAPPDELEGATE.M 3// 01-modal 4//5// Created by Apple on 14-6-9.6// Copyright (c) 2014 ITC Ase. All rights reserved. 7//8 9 #import "YYAppDelegate.h" #import "YYViewController.h" @implementation YYAppDelegate13-(BOOL) APPL Ication: (uiapplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions15 {//1 . Create window and set window's Frame17 self.window=[[uiwindow alloc]initwithframe:[[uiscreen Mainscreen] bounds]];18 2. Set window background color to black self.window.backgroundcolor=[uicolor blackcolor];20 // Create a navigation controller as a child controller yyviewcontroller *one=[[yyviewcontroller alloc]init];24 self.window.rootviewcontroller=one;25 //3. Sets the window as the main windows and displays the [Self.window makekeyandvisible]; return yes;29}30 @end
Open the modal window
YYVIEWCONTROLLER.M file
1//2// YYVIEWCONTROLLER.M 3// 01-modal 4//5// Created by Apple on 14-6-9.6// Copyright (c) 2014 Itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYtwoViewController.h" @interface Yyviewcontroller () 13//When clicked , jump to the second interface-(ibaction) Jump2two: (UIButton *) sender;15 @end17 @implementation YYViewController19-(void) Viewdi dLoad21 { [super viewdidload];23//Do any] additional setup after loading the view from its nib.24}25 26 27-( ibaction) Jump2two: (UIButton *) Sender { //create a new modal and pop Yytwoviewcontroller *two=[[ Yytwoviewcontroller alloc]init];30 //In a navigation controller wrapper on both, so that the pop-up modal window has a navigation bar to put back the button to uinavigationcontroller *nvc= [[Uinavigationcontroller Alloc]initwithrootviewcontroller:two32 ]; [self PRESENTVIEWCONTROLLER:NVC animated:yes completion:^{34 NSLog (@ "Popup a modal Window"); }];36 Panax Notoginseng}38 @end
Removing modal views
YYTWOVIEWCONTROLLER.M file
1//2// YYTWOVIEWCONTROLLER.M 3// 01-modal 4//5// Created by Apple on 14-6-9.6// Copyright (c) 201 4 years itcase. All rights reserved. 7//8 9 #import "YYtwoViewController.h" @interface Yytwoviewcontroller () @end14 @implementation Yytwovi EwController16-(void) viewDidLoad18 { [super viewdidload];20] //Add a Back button to the navigation bar Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "Back" style: Uibarbuttonitemstyleplain target:self Action: @selector (change)];23}24-(void) Change26 {+ //write click-To-Back button click event 28 //Click the Back button to remove the current modal window// [Self.navigationcontroller dismissviewcontrolleranimated:yes completion:^{30// NSLog (@ "Remove modal window"); ];32 33//If a controller is shown in modal form, you can call the controller and the controller's sub-controller to let the controller disappear. [Self Dismissviewcontrolleranimated:yes completion:^{35 NSLog (@ "Remove"), }];37}38 @end
Third, the attention point
(1) Modal features: When the modal window pops up (from bottom to top), the rear view is not point (2) The view that pops up the controller (only one view can be popped in this way)
Create a new modal and eject yytwoviewcontroller *two=[[yytwoviewcontroller alloc]init]; With the navigation controller wrapped on both, so that the popup modal window has a navigation bar can be put back button uinavigationcontroller *nvc=[[uinavigationcontroller alloc] Initwithrootviewcontroller:two ]; [Self PRESENTVIEWCONTROLLER:NVC animated:yes completion:^{ NSLog (@ "pops up a modal window"); }];
(3) Remove the view of the controller (two ways are possible)
Write click-To-Back button click events //Click the Back button to remove the current modal window// [Self.navigationcontroller Dismissviewcontrolleranimated:yes completion:^{// NSLog (@ "Remove modal window");/ }]; If a controller is shown in modal form, you can call the controller and the controller's sub-controller to let the controller disappear [self dismissviewcontrolleranimated:yes completion:^{ NSLog (@ "Remove"); }];
(4) Prompt in the actual development, if the relationship between the controller is closely related to the navigation controller, if the relationship between the controller is not very close with modal four, the internal mechanism (1) Pop-up, window above only a child view. (2) Although the current interface is displayed in front of our eyes Twoview, but the window's root controller is still njviewcontroller, it does not switch the window's root controller, but simply to change the view shown above the window. (3) The removed view is not destroyed because the controller has not been destroyed, so the controller's corresponding view is not destroyed. (4) in the modal popup (after the full display), in the method passed in a parameter, the default is a controller strongly reference it. (5) When removed, the modal is released as soon as the dismiss method of the controller is called to close the window. (6) The modal window usually pops up with a navigation bar, and the quickest way for the interface to have a navigation bar is to wrap it with a navigation controller. (7) If a controller is shown in modal form. You can call the controller and the controller's sub-controller to make the controller disappear.
V. Transmission of data
Project file Structure and storyboard
code example:
YYVIEWCONTROLLER.M file
1//2// YYVIEWCONTROLLER.M 3// 02-modal window data transfer 4//5//Created by Apple on 14-6-9.6/ Copyright (c) 20 14 Itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import "YYtwoViewController.h" @interface Yyviewcontroller () @end1 5 @implementation YYViewController17-(void) viewDidLoad19 { [Super viewdidload];21}22-(void) Didreceivem emoryWarning24 { [super didreceivememorywarning];26}27]/*30 If the relationship between controllers is more tightly UINavigationController31 If the relationship between the controller is not very close can be used Modal32 */33 34//through Segue Jump before, will call this method, in this method to pass the data to the modal window 35-( void) Prepareforsegue: (Uistoryboardsegue *) Segue Sender: (ID) sender36 {PNS //Get target controller Uinavigationcontroller *nav=segue.destinationviewcontroller;39 Yytwoviewcontroller *two= ( Yytwoviewcontroller *) nav.topviewcontroller;40 //Transmission data (email protected) "Top of text";}43 @end
YYtwoViewController.h file
1//2// YYtwoViewController.h 3// 02-modal window data transfer 4//5//Created by Apple on 14-6-9.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import <uikit/uikit.h>10 @interface yytwoviewcontroller:uiviewcontroller12 @property (nonatomic, copy) NSString *name;13 @end
YYTWOVIEWCONTROLLER.M file
1//2// YYTWOVIEWCONTROLLER.M 3// 02-modal window data transfer 4//5//Created by Apple on 14-6-9.6// Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYtwoViewController.h" @interface Yytwoviewcontroller () @property (weak, nonatomic) Iboutlet UILabel *nametext;13 @end15 @implementation YYtwoViewController17-(void) VIEWDIDLOAD20 { [Super Viewdi dload];22 self.nametext.text=self.name;23 //Add a return button to the navigation bar Self.navigationitem.leftbarbuttonitem=[[uibarbuttonitem alloc]initwithtitle:@ "Back" style: Uibarbuttonitemstyleplain target:self Action: @selector (Black)];26}27-(void) black29 { //Remove modal window [ Self Dismissviewcontrolleranimated:yes completion:^{32 NSLog (@ "successfully removed! "); }];34}35 @end
Transferred from: http://www.cnblogs.com/wendingding/p/3778853.html
iOS Development UI Chapter-modal Brief Introduction