1. 源代码下载链接:01-modal
Demo示例程序源代码01-modal.zip
    37.8 KB
  2. //
  3. //  MJAppDelegate.h
  4. //  01-modal
  5. //
  6. //  Created by apple on 13-12-11.
  7. //  Copyright (c) 2013itcast. All rights reserved.
  8. //

  9. #import<UIKit/UIKit.h>

  10. @interfaceMJAppDelegate : UIResponder <UIApplicationDelegate>

  11. @property(strong,nonatomic) UIWindow *window;

  12. @end
  13. // MJAppDelegate.m

    Map
  14. //
  15. //  MJAppDelegate.m
  16. //  01-modal
  17. //
  18. //  Created by apple on 13-12-11.
  19. //  Copyright (c) 2013itcast. All rights reserved.
  20. //

  21. #import"MJAppDelegate.h"
  22. #import"MJOneViewController.h"

  23. @implementationMJAppDelegate

  24. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  25. {
  26.    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  27.    
  28.    self.window.rootViewController = [[MJOneViewController alloc] init];
  29.    
  30.     [self.window makeKeyAndVisible];
  31.    returnYES;
  32. }

  33. - (void)applicationWillResignActive:(UIApplication *)application
  34. {
  35.    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  36.    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
  37. }

  38. - (void)applicationDidEnterBackground:(UIApplication *)application
  39. {
  40.    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
  41.    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  42. }

  43. - (void)applicationWillEnterForeground:(UIApplication *)application
  44. {
  45.    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
  46. }

  47. - (void)applicationDidBecomeActive:(UIApplication *)application
  48. {
  49.    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  50. }

  51. - (void)applicationWillTerminate:(UIApplication *)application
  52. {
  53.    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  54. }

  55. @end
  56. // MJOneViewController.h

    Map
  57. //
  58. //  MJOneViewController.h
  59. //  01-modal
  60. //
  61. //  Created by apple on 13-12-11.
  62. //  Copyright (c) 2013itcast. All rights reserved.
  63. //

  64. #import<UIKit/UIKit.h>

  65. @interfaceMJOneViewController : UIViewController
  66. - (IBAction)jump2;

  67. @end
  68. // MJOneViewController.m

    Map
  69. //
  70. //  MJOneViewController.m
  71. //  01-modal
  72. //
  73. //  Created by apple on 13-12-11.
  74. //  Copyright (c) 2013itcast. All rights reserved.
  75. //

  76. #import"MJOneViewController.h"
  77. #import"MJTwoViewController.h"
  78. #import"MJThreeViewController.h"

  79. @interfaceMJOneViewController ()

  80. @end

  81. @implementationMJOneViewController

  82. //- (IBAction)jump2 {
  83. //    MJTwoViewController *two = [[MJTwoViewController alloc] init];
  84. //   
  85. //    // modalTransitionStyle设置模态控制器展示的形式
  86. //    /*
  87. //     UIModalTransitionStyleCoverVertical = 0, 垂直覆盖(从底部钻上来)
  88. //     UIModalTransitionStyleFlipHorizontal, 水平翻转
  89. //     UIModalTransitionStyleCrossDissolve,  淡入淡出
  90. //     UIModalTransitionStylePartialCurl     翻页(展示部分界面)
  91. //     */
  92. ////    two.modalTransitionStyle = UIModalTransitionStylePartialCurl;
  93. ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490807.html   
  94. //    //modal形式展示其他控制器(模态窗口)
  95. //    [self presentViewController:two animated:YES completion:^{
  96. //        NSLog(@"----展示完毕");
  97. //    }];
  98. //}

  99. /*
  100.  给一个控制器顶部增加一个导航栏的最快方法:
  101.  1>给这个控制器包装一个导航控制器(UINavigationController
  102.  */

  103. - (void)jump2
  104. {
  105.     MJThreeViewController *three = [[MJThreeViewController alloc] init];
  106.    
  107.     UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:three];
  108.    
  109.     nav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
  110.    
  111.     NSLog(@"前:one = %p, window的子控件%@",self.view, [UIApplication sharedApplication].keyWindow.subviews);
  112.    
  113.     [selfpresentViewController:nav animated:YEScompletion:^{
  114.         NSLog(@"后:nav=%p, %@", nav.view, [UIApplication sharedApplication].keyWindow.subviews);
  115.     }];
  116. }
  117. @end
  118. // MJTwoViewController.h

    Map
  119. //
  120. //  MJTwoViewController.h
  121. //  01-modal
  122. //
  123. //  Created by apple on 13-12-11.
  124. //  Copyright (c) 2013itcast. All rights reserved.
  125. //

  126. #import<UIKit/UIKit.h>

  127. @interfaceMJTwoViewController : UIViewController
  128. - (IBAction)cancel:(id)sender;

  129. @end
  130. // MJTwoViewController.m

    Map
  131. //
  132. //  MJTwoViewController.m
  133. //  01-modal
  134. //
  135. //  Created by apple on 13-12-11.
  136. //  Copyright (c) 2013itcast. All rights reserved.
  137. //

  138. #import"MJTwoViewController.h"

  139. @interfaceMJTwoViewController ()

  140. @end

  141. @implementationMJTwoViewController

  142. - (void)viewDidLoad
  143. {
  144.     [superviewDidLoad];
  145.    
  146.     UIButton *btn = [UIButton buttonWithType:UIButtonTypeContactAdd];
  147.     btn.frame = CGRectMake(0,44,40,40);
  148.     [self.view addSubview:btn];
  149. }

  150. - (IBAction)cancel:(id)sender {
  151.    //关闭当前的模态控制器
  152.     [self dismissViewControllerAnimated:YES completion:nil];
  153. }
  154. @end
  155. // MJThreeViewController.h

    Map
  156. //
  157. //  MJThreeViewController.h
  158. //  01-modal
  159. //
  160. //  Created by apple on 13-12-11.
  161. //  Copyright (c) 2013itcast. All rights reserved.
  162. //

  163. #import<UIKit/UIKit.h>

  164. @interface MJThreeViewController : UIViewController

  165. @end
  166. // MJThreeViewController.m

    Map
  167. //
  168. //  MJThreeViewController.m
  169. //  01-modal
  170. //
  171. //  Created by apple on 13-12-11.
  172. //  Copyright (c) 2013itcast. All rights reserved.
  173. //

  174. #import"MJThreeViewController.h"

  175. @interface MJThreeViewController ()

  176. @end

  177. @implementationMJThreeViewController

  178. - (void)viewDidLoad
  179. {
  180.     [superviewDidLoad];
  181.    
  182.     UIView *abc = [[UIView alloc] init];
  183.     abc.frame = CGRectMake(0,0,100,100);
  184.     abc.backgroundColor = [UIColor yellowColor];
  185.     [self.view addSubview:abc];
  186.    //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490807.html
  187.    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消"style:UIBarButtonItemStyleBordered target:selfaction:@selector(cancel)];
  188. }

  189. - (void)cancel
  190. {
  191.     [selfdismissViewControllerAnimated:YEScompletion:nil];
  192. }

  193. @end

相关文章: