寫在前面的話
近期接到這樣一個需求,需要為app內機構詳情頁提供2種不同的布局,效果圖如下,
機構詳情頁的2種布局.png
拿到該需求后,你都有哪些思路?
1、創建2個
UIViewController
, 界面xib
實現,邏輯代碼貼貼貼。2、創建1個
UIViewController
,純代碼實現。3、創建1個
UIViewController
, 不同場景加載不同的storyboard
或者xib
實現。我們采取第三種方法實現,那就引出了今天的問題,iOS控制器
ViewControlle
加載都有幾種方式?
代碼實現
通過alloc
或者new
方法實現。
故事板加載
在Main.storyboard
實現如下截圖
故事板加載控制器.png
代碼實現部分
#import "AHTestViewController.h"
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
AHTestViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"AHDemo2"];
在跳轉到機構詳情頁時只需按照不同場景加載不同故事板即可.
if (item.organ_style.integerValue==1){
OrganOtherDetailViewController *VC = [[OrganOtherDetailViewController alloc]initWithType:detailone];
VC.item =self.orgaDataArr[indexPath.section];
[self.navigationController pushViewController:VC animated:YES];
}else if (item.organ_style.integerValue==2){
OrganOtherDetailViewController *VC = [[OrganOtherDetailViewController alloc]initWithType:detailtwo];
VC.item =self.orgaDataArr[indexPath.section];
[self.navigationController pushViewController:VC animated:YES];
}
xib實現
新建一xib
文件,在xib
文件中做如下設置
xib加載控制器.png
代碼實現部分
#import "AHTestViewController.h"
AHTestViewController *vc = [[AHTestViewController alloc]initWithNibName:@"AHTest" bundle:nil];