iOS addChildController的方法

使用默認(rèn)的ViewController作為盛放childController 的容器。創(chuàng)建FirstViewController作為ChildViewController。在這里我只是簡(jiǎn)單的實(shí)驗(yàn)了一下兩個(gè)控制器之間的跳轉(zhuǎn),其中并沒(méi)有加入數(shù)據(jù)。因?yàn)閮蓚€(gè)控制器是分離開(kāi)的。所以很大程度上降低了耦合度。

1 在viewcontroller中

@property(nonatomic, strong) UIViewController *currentVC; 記錄當(dāng)前的控制器 將viewcontroller 作為當(dāng)前的控制器 _currentVC = self;

2 創(chuàng)建點(diǎn)擊按鈕,點(diǎn)擊彈出childViewController?

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

3 將firstViewController聲明為全局變量。以便后續(xù)操作。

4 點(diǎn)擊viewcontroller中的按鈕 響應(yīng)事件如下

- (void)btnClick:(UIButton *)btn {

[self addChildViewController:firstVC];

[self.view addSubview:firstVC.view];

[UIView animateWithDuration:2.0 delay:0.5 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{

firstVC.baseView.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2);

} completion:^(BOOL finished) {

NSLog(@"完成");

}];

}

********重要的點(diǎn)在點(diǎn)擊按鈕時(shí) ,按鈕響應(yīng)方法中 [self addChildViewController:_firstVC];

和[self.view addSubview:_firstVC.view]; 一個(gè)是講firstVC作為當(dāng)前控制器的子視圖控制器。另一個(gè)addSubview方法是講firstVC的視圖添加到當(dāng)前視圖,作為子視圖顯示。在firstVC視圖出現(xiàn)時(shí),我添加了動(dòng)畫(huà)效果。

5 在firstViewControl中 需要有回調(diào)方法。在這里我用的block 在firstViewController中我添加了一個(gè)點(diǎn)擊按鈕。點(diǎn)擊按鈕會(huì)將Block傳到viewController中

- (void)btnClick:(UIButton *)btn {

self.firstBlock();

}

6 在viewcontroller中 接受childViewcontroller傳過(guò)來(lái)的block?

firstVC = [[FirstViewController alloc] init];

__weak FirstViewController *weakSelf = firstVC;

firstVC.firstBlock = ^(){

[weakSelf removeFromParentViewController];

[UIView animateWithDuration:2.0 delay:0.5 usingSpringWithDamping:0.5 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveEaseInOut animations:^{

weakSelf.baseView.frame = CGRectMake(0, -weakSelf.baseView.frame.size.height, weakSelf.baseView.frame.size.width, weakSelf.baseView.frame.size.height);

} completion:^(BOOL finished) {

[weakSelf.view removeFromSuperview];

_currentVC = weakSelf;

NSLog(@"完成");

}];

};

在這里由于用到動(dòng)畫(huà),所以涉及到firstViewController中的frame。在這里就會(huì)牽扯到block的循環(huán)引用問(wèn)題了。 __weak FirstViewController *weakSelf = firstVC; 用__weak修飾自身。weak是弱引用,不會(huì)持有對(duì)象。所以在block里面使用self時(shí)就不會(huì)再造成相互持有。retain cycle了。[weakSelf removeFromParentViewController]; firstVC 從當(dāng)前視圖控制器中移除。在動(dòng)畫(huà)完成時(shí) 將當(dāng)前的currentVC = self;

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *7月8日上午 N:Block :跟一個(gè)函數(shù)塊差不多,會(huì)對(duì)里面所有的內(nèi)容的引用計(jì)數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,711評(píng)論 1 14
  • 1. 什么是 ARC? (ARC 是為了解決什么問(wèn)題而誕生的?) ARC 是 Automatic Referenc...
    milk_powder閱讀 743評(píng)論 0 5
  • 拿俄米啊,我的婆婆 摩押是有禍的地土,可悲的歌 你沒(méi)有了丈夫以利米勒,沒(méi)有了兒子和果腹的生活 你滿(mǎn)滿(mǎn)地出去,空空地...
    守素閱讀 532評(píng)論 11 8
  • 一說(shuō)到存款就心塞塞的 八月份的工資給了中秋節(jié) 九月份的工資給了國(guó)慶節(jié) 下個(gè)月還有雙十一 別問(wèn)我怎么還存不到錢(qián) 發(fā)覺(jué)...
    小一哥閱讀 2,661評(píng)論 0 3
  • 昨日,在夢(mèng)里,我被埋在地下,有人寫(xiě)了一篇計(jì)劃,就是要炸掉那里,營(yíng)救我。但在土里我快窒息了。忽然從夢(mèng)中驚醒,那...
    老大1224閱讀 333評(píng)論 0 0

友情鏈接更多精彩內(nèi)容