MVC的用法和作用

來源:互聯網
上載者:User

標籤:

    最近在學習IOS項目的時候,老師經常提起MVC,在理解的過程中,越來越發現MVC的魅力,MVC:M:Model V:View C:Controller;Model 是用來儲存資料的,View 是用來顯示的 Controller 是協調 Model 和 View 的中間者。

    MVC的作用:

                   1:分工明確:不用全部寫在控制器裡,有利於共同開發,提高了工做的效率。

                   2. 減少耦合性:代碼分工明確,降低了相互關聯。

                   3.可重用性提高:功能分開了,只需要簡單封裝一下,繼承或者匯入標頭檔即可。

       例如:UICollectionView;

            ViewController:  

              TopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

              //給對象資料

             MovieModel *model = self.data[indexPath.row];   //資料給對象

             //獲得資料

            cell.model = model;

            return cell;

            Model:根據Json檔案寫出需要擷取的屬性類型及名稱

              @interface MovieModel : NSObject

              @property (nonatomic,strong) NSDictionary *rating;

              @property (nonatomic,assign) NSInteger collect_count;

              @property (nonatomic,strong) NSString *title;

              @property (nonatomic,strong) NSString *year;

              @property (nonatomic,strong) NSDictionary *images;

            View:

              .h檔案

             @class MovieModel;

             @interface CollectionViewCell : UICollectionViewCell{

                 UIImageView *imgVc;

              }

             @property (nonatomic,strong) MovieModel *model;

               .m檔案

               //需要匯入Model的標頭檔

               - (void) setModel:(MovieModel *) model {

                       _model = model;

                      //利用model來傳值。

                 }

         總結: Model層實現系統中的商務邏輯。 View層用於與使用者的互動。 Controller層是Model與View之間溝通的橋樑,它可以指派使用者的請求並選擇          恰當的視圖以用於顯示,同時它也可以解釋使用者的輸入並將它們映射為模型層可執行檔操作.

 

MVC的用法和作用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.