国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > 建站經驗 > 正文

開發IOS關于子UIViewController和父,UIViewController相互調用方

2019-11-02 14:29:07
字體:
來源:轉載
供稿:網友

   今天在做iphone開發時碰到了一個常用的需求,即在一個viewController中添加另外一個viewController,同時能保證這兩個ViewController之間能夠相互交互且相互調用方法和函數,在網上查了很多資料,很多開發者說需要使用objective-c變態的 delegate,可是我感覺delegate是使用在兩個同級之間的UIView比較好,至于能不能使用在父子關系而且是 UIVeiwController我也不太清楚,也沒有親自實驗過,通過查看SDK的API及其他資料我使用了自己的方法實現了我想要的需求,但是我不知道我的這種方法會不會有致命性的問題,或者會不會有很大的弊端,如果有高人存在的話還望指點一下,我只是一個初學者,下面我將我的方法貼上來:

  首先,定義兩個UIVeiwController,姑且先命名為ViewControllerParent(父容器)和ViewControllChild(子容器)吧,我們可以通過UIView的

  insertSubview方法將子容器添加到父容器中,這點在這里先不用說了

  其次,我們先來看一下通過父容器調用子容器中的方法及函數,我先在子容器ViewControllChild和父容器ViewControllerParent中分別寫了如下方法:

  //該方法是彈出一個警告框

  -(void)AlertWindow:(NSString *)transValue{

  UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:transValue message:transValue delegate:self

 

六個字網名[www.la240.com/html2017/1/21/]
 cancelButtonTitle:@"OK" otherButtonTitles:nil];

  [alertView show];

  [alertView release];

  }

  由于父容器ViewControllerParent要插入ViewControllChild,因此在ViewControllerParent一定已經定義了ViewControllChild,如下:

  @synthesize ViewControllChild;

  if (self.ViewControllChild==nil) {

  ViewControllChild *ViewControll=[[ViewControllChild alloc] initWithNibName:@"ViewControllChild" bundle:nil];

  self.ViewControllChild=ViewControll;

  [ViewControll release];

  }

  所以當父容器調用子容器的方法只需要做下面一步即可:

  [self.ViewControllChild AlertWindow:@"我是從子容器中彈出來的"];

  這就實現了父容器調用子容器中方法;

  最后,看一下如何在子容器中調用父容器的方法,我的思路是這樣的,在insertSubview時我設置該子容器的父Controller,最后在子容器中通過父Controller來調用方法,因此我在子容器ViewControllChild中添加了一個這樣的方法:

  //設置當前窗口的父容器

  -(void)SetParentView:(UIViewController *)viewController{

  [self setParentViewController:viewController];

  }

  在ViewControllerParent實現insertSubview前加入下邊代碼:

  [self.ViewControllChild SetParentView:self];

  該代碼實現了設置父容器

  這樣在子容器ViewControllChild中通過以下代碼就可以調用父容器的方法或者函數了:

  [self.parentViewController AlertWindow:@"我是從父容器中彈出來的"];

  以上便是實現ViewController相互交互的方法和思路,如果有什么錯誤或者弊端還希望大家能夠提出來我們共同探討

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 财经| 泗洪县| 鹿泉市| 江西省| 临沂市| 获嘉县| 台中市| 观塘区| 诸暨市| 连州市| 涞源县| 嵊州市| 印江| 偏关县| 麦盖提县| 海门市| 平遥县| 兰州市| 邵阳市| 红原县| 阳春市| 镇沅| 绍兴县| 会东县| 山东| 偃师市| 辉南县| 辉南县| 探索| 松江区| 大余县| 祥云县| 孟州市| 彰化县| 雅安市| 岑巩县| 新蔡县| 商都县| 五台县| 寻乌县| 两当县|