日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

模态视图(转)

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 模态视图(转) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

轉載請注明出處,原文網址:http://blog.csdn.net/m_changgong/article/details/8127894?作者:張燕廣

模態視圖不是專門的某個類,而是通過視圖控制器的presentViewController方法彈出的視圖,我們稱為模態視圖。

  • 模態視圖出現的場景一般是臨時彈出的窗口,譬如:登錄窗口;
  • 模態視圖彈出時通過對視圖對象的modalTransitionStyle來設置動畫效果;
  • 在彈出的視圖中使用dismissViewControllerAnimated方法關閉窗口。

實現的功能:1)通過彈出一個ModalView(模態視圖),實現多視圖;2)主界面上點擊按鈕彈出Info界面,在該界面上點擊返回,返回到主界面。

關鍵詞:多視圖 MultiView模態視圖?ModalView

1、創建一個Empty Application工程,命名為:MultiView-ModalView,如下圖

2、選中工程中的Group MultiView-ModalView,然后按住CMD(Windows鍵)+N,新建視圖控制器MainViewController,如下圖

3、依照上步操作,新建視圖控制器InfoViewController。

4、編輯MainViewController.xib,添加一個Label和Button,如下圖

5、編輯InfoViewController.xib,添加一個Label和Button,如下圖


6、修改MainViewController.h,如下

[cpp]?view plaincopy
  1. <span?style="font-family:Microsoft?YaHei;font-size:18px;">//??
  2. //??MainViewController.h??
  3. //??MultiView-ModalView??
  4. //??
  5. //??Created?by?Zhang?Yanguang?on?12-10-26.??
  6. //??Copyright?(c)?2012年?MyCompanyName.?All?rights?reserved.??
  7. //??
  8. ??
  9. #import?<UIKit/UIKit.h>??
  10. #import?"InfoViewController.h"??
  11. @interface?MainViewController?:?UIViewController??
  12. @property(nonatomic,retain)InfoViewController?*infoViewController;??
  13. ??
  14. -(IBAction)showInfoView:(id)sender;??
  15. @end</span>??

將操作showInfoView與MainViewController.xib中的button的Touch Up Inisde進行關聯。

7、修改MainViewController.m,主要是實現showInfoView方法,如下

?

[cpp]?view plaincopy
  1. <span?style="font-family:Microsoft?YaHei;font-size:18px;">//??
  2. //??MainViewController.m??
  3. //??MultiView-ModalView??
  4. //??
  5. //??Created?by?Zhang?Yanguang?on?12-10-26.??
  6. //??Copyright?(c)?2012年?MyCompanyName.?All?rights?reserved.??
  7. //??
  8. ??
  9. #import?"MainViewController.h"??
  10. ??
  11. @interface?MainViewController?()??
  12. ??
  13. @end??
  14. ??
  15. @implementation?MainViewController??
  16. @synthesize?infoViewController;??
  17. ??
  18. -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??
  19. {??
  20. ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??
  21. ????if?(self)?{??
  22. ????????//?Custom?initialization??
  23. ????}??
  24. ????return?self;??
  25. }??
  26. ??
  27. -?(void)viewDidLoad??
  28. {??
  29. ????[super?viewDidLoad];??
  30. ????//?Do?any?additional?setup?after?loading?the?view?from?its?nib.??
  31. ????//設置背景顏色??
  32. ????self.view.backgroundColor?=?[UIColor?grayColor];??
  33. }??
  34. ??
  35. -(void)dealloc{??
  36. ????[infoViewController?release];??
  37. }??
  38. ??
  39. -(IBAction)showInfoView:(id)sender{??
  40. ????if(infoViewController?==?nil){??
  41. ????????infoViewController?=?[[InfoViewController?alloc]initWithNibName:@"InfoViewController"?bundle:nil];??
  42. ????????//NSLog(@"infoViewController?is?nil");??
  43. ????}else{??
  44. ????????//NSLog(@"infoViewController?is?not?nil");??
  45. ????}??
  46. ????infoViewController.modalTransitionStyle?=?UIModalTransitionStyleCoverVertical;??
  47. ??????
  48. ????//[self?presentModalViewController:infoViewController?animated:YES];//備注1??
  49. ????[self?presentViewController:infoViewController?animated:YES?completion:^{//備注2??
  50. ????????NSLog(@"show?InfoView!");??
  51. ????}];??
  52. ??????
  53. ????//presentedViewController??
  54. ????NSLog(@"self.presentedViewController=%@",self.presentedViewController);//備注3??
  55. }??
  56. ??
  57. -?(void)viewDidUnload??
  58. {??
  59. ????[super?viewDidUnload];??
  60. ????//?Release?any?retained?subviews?of?the?main?view.??
  61. ????//?e.g.?self.myOutlet?=?nil;??
  62. ????infoViewController?=?nil;??
  63. }??
  64. ??
  65. -?(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation??
  66. {??
  67. ????return?(interfaceOrientation?==?UIInterfaceOrientationPortrait);??
  68. }??
  69. ??
  70. @end</span>??

?

?

備注1、備注2:備注中的方法已經廢棄,被備注2中的presentViewController代替;參數completion實現一個回調,當MainViewController的viewDidDisappear調用之后,該回調會被調用。

備注3:在MainViewController中調用self.presentedViewController,返回的是由MainViewController?present出的視圖控制器,在這里即是:infoViewController。

8、修改InfoViewController.h,如下

[cpp]?view plaincopy
  1. <span?style="font-family:Microsoft?YaHei;font-size:18px;">//??
  2. //??InfoViewController.h??
  3. //??MultiView-ModalView??
  4. //??
  5. //??Created?by?Zhang?Yanguang?on?12-10-26.??
  6. //??Copyright?(c)?2012年?MyCompanyName.?All?rights?reserved.??
  7. //??
  8. ??
  9. #import?<UIKit/UIKit.h>??
  10. ??
  11. @interface?InfoViewController?:?UIViewController??
  12. ??
  13. -(IBAction)backMainView:(id)sender;??
  14. @end??
  15. </span>??

將操作backMainView與InfoViewController.xib中的button的Touch Up Inisde進行關聯。

9、修改InfoViewController.m,主要是實現方法backMainView,如下

[cpp]?view plaincopy
  1. <span?style="font-family:Microsoft?YaHei;font-size:18px;">//??
  2. //??InfoViewController.m??
  3. //??MultiView-ModalView??
  4. //??
  5. //??Created?by?Zhang?Yanguang?on?12-10-26.??
  6. //??Copyright?(c)?2012年?MyCompanyName.?All?rights?reserved.??
  7. //??
  8. ??
  9. #import?"InfoViewController.h"??
  10. ??
  11. @interface?InfoViewController?()??
  12. ??
  13. @end??
  14. ??
  15. @implementation?InfoViewController??
  16. ??
  17. -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil??
  18. {??
  19. ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];??
  20. ????if?(self)?{??
  21. ????????//?Custom?initialization??
  22. ????}??
  23. ????return?self;??
  24. }??
  25. ??
  26. -?(void)viewDidLoad??
  27. {??
  28. ????[super?viewDidLoad];??
  29. ????//?Do?any?additional?setup?after?loading?the?view?from?its?nib.??
  30. ????//設置背景顏色??
  31. ????self.view.backgroundColor?=?[UIColor?greenColor];??
  32. }??
  33. ??
  34. -?(void)viewDidUnload??
  35. {??
  36. ????[super?viewDidUnload];??
  37. ????//?Release?any?retained?subviews?of?the?main?view.??
  38. ????//?e.g.?self.myOutlet?=?nil;??
  39. }??
  40. ??
  41. -(IBAction)backMainView:(id)sender{???
  42. ????NSLog(@"self.parentViewController=%@",self.parentViewController);??
  43. ????//[self.parentViewController?dismissViewControllerAnimated:YES?completion:nil];//備注4??
  44. ??????
  45. ????/*?
  46. ?????If?this?view?controller?is?a?child?of?a?containing?view?controller?(e.g.?a?navigation?controller?or?tab?bar?
  47. ?????controller,)?this?is?the?containing?view?controller.??Note?that?as?of?5.0?this?no?longer?will?return?the?
  48. ?????presenting?view?controller.?
  49. ?????*/??
  50. ????NSLog(@"self.presentedViewController=%@",self.presentedViewController);??
  51. ????//[self.presentedViewController?dismissViewControllerAnimated:YES?completion:nil];?//備注5??
  52. ??????
  53. ????NSLog(@"self.presentingViewController=%@",self.presentingViewController);??
  54. ????//[self.presentingViewController?dismissViewControllerAnimated:YES?completion:nil];//備注6??
  55. ??????
  56. ????//?Dismiss?the?current?modal?child.?Uses?a?vertical?sheet?transition?if?animated.?This?method?has?been?replaced?by?dismissViewControllerAnimated:completion:??
  57. ????//?It?will?be?DEPRECATED,?plan?accordingly.??
  58. ????//[self?dismissModalViewControllerAnimated:YES];//備注7??
  59. ????[self?dismissViewControllerAnimated:YES?completion:nil];//備注8??
  60. }??
  61. ??
  62. -?(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation??
  63. {??
  64. ????return?(interfaceOrientation?==?UIInterfaceOrientationPortrait);??
  65. }??
  66. ??
  67. @end</span>??


備注4:不能正常工作,該代碼不能實現返回到MainViewController的功能,因為MainViewController并不是InfoViewController的父視圖控制器(父子試圖控制器以后會講到),該方法的注釋如下:

/*
? If this view controller is a child of a containing view controller (e.g. a navigation controller or tab bar
? controller,) this is the containing view controller.? Note that as of 5.0 this no longer will return the
? presenting view controller.
*/

備注5:不能正常工作,代碼也不能實現返回到MainViewController的功能,備注3中已解釋過self.presentedViewController,在此處一定返回空。

備注6:可以正常工作,改代碼可以實現返回到MainViewController的功能, self.presentingViewController返回的視圖控制器是指present出當前視圖控制器(即:infoViewController)的視圖控制器,當然是MainViewController。

備注7、8:可以正常工作,改代碼可以實現返回到MainViewController的功能,備注7中的方法已經廢棄,已被備注8中的方法代替;現在要考慮的問題是:為什么[self dismissViewControllerAnimated:YES completion:nil]與[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]實現了同樣的功能?

類UIViewController的dismissViewControllerAnimated方法有一段注釋如下:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

什么意思呢?MainViewController把InforViewController 展示出來了,同樣也要負責把InforViewController退出,如果直接在InforViewController中發出(調用)dismissViewControllerAnimated消息,這個消息會自動轉給MainViewController,所以,在InforViewController中執行[self dismissViewControllerAnimated:YES completion:nil]與[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]兩種調用,效果是一樣的,調用前者就等同于調用后者。建議用后者,更容易理解。

10、編譯、運行,效果如下

轉載于:https://www.cnblogs.com/hereiam/p/3813555.html

總結

以上是生活随笔為你收集整理的模态视图(转)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。