Storyboard搭建最基本的iOS app骨架

来源:互联网 发布:php curl header 设置 编辑:程序博客网 时间:2024/06/05 10:59

     用Storyboard搭建的iOS app骨架如图1。



                                                                                                                              图1


1、把Storyboard中的控制器都删掉,拉取一个TabBarController放入Storyboard中,如果我们想对该TabBarController自定义些东西,可以在代码中生成一个TabBarController的子类,然后在Storyboard中设置该TabBarController的class,如图2。


                                                                                   图2

2、设置Is Initial View Controller。

3、我们拉取4个NavigationController。

4、把拉取的这4个NavigationController分别设置成TabBarController的viewControllers。

5、删掉这4个NavigationController默认的root view controller或不删都可以,我是删掉了,分别加上4个View controller。

6、NavigationController和View controller对应,设置关系root view controller。

7、现在我们要从HomePageViewController Push到DetailViewController。

8、设置Push时的连线,设置Identifier,该Identifier很重要,是Push到特定页面的标志,如图3。因为用到了TabBarController,所以在NavigationController Push时勾选Hide Bottom Bar on Push,如图4,下沉停顿?试试self.tabBarController.tabBar.translucent = NO


                                                                    图3


                                                                                           图4

9、做一个弹窗的效果,拉取一个View controller(PopViewController),用addChildViewController将该View controller(PopViewController)加到NavigationController上,当然也可以加到TabBarController上,大致代码如下。

- (IBAction)doShowPopVC:(id)sender {    // 在DetailViewController上addChildViewController    self.popVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"PopViewControllerSBID"];    self.popVC.view.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];    [self.navigationController addChildViewController:self.popVC];    [self.navigationController.view addSubview:self.popVC.view];        // block毁掉关闭PopViewController    __weak DetailViewController *weakSelf = self;    [self.popVC setPopBackWithBlock:^{        [weakSelf.popVC removeFromParentViewController];        [weakSelf.popVC.view removeFromSuperview];        weakSelf.popVC.view = nil;        weakSelf.popVC = nil;    }];}

setPopBackWithBlock是block回调

////  PopViewController.h//  UISkeletonStoryboard////  Created by kun shen on 2017/3/13.//  Copyright © 2017年 kun shen. All rights reserved.//#import <UIKit/UIKit.h>typedef void (^PopBackBlock)();@interface PopViewController : UIViewController@property (copy, nonatomic) PopBackBlock popBackBlock;- (void)setPopBackWithBlock:(PopBackBlock)popBackBlock;@end

////  PopViewController.m//  UISkeletonStoryboard////  Created by kun shen on 2017/3/13.//  Copyright © 2017年 kun shen. All rights reserved.//#import "PopViewController.h"@interface PopViewController ()- (IBAction)doBack:(id)sender;@end@implementation PopViewController- (void)viewDidLoad {    [super viewDidLoad];    // Do any additional setup after loading the view.}- (void)didReceiveMemoryWarning {    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {    // Get the new view controller using [segue destinationViewController].    // Pass the selected object to the new view controller.}*/#pragma mark -#pragma mark Block handle- (void)setPopBackWithBlock:(PopBackBlock)popBackBlock {        self.popBackBlock = popBackBlock;}#pragma mark -#pragma mark Button functions- (IBAction)doBack:(id)sender {        if (self.popBackBlock) {        self.popBackBlock();    }}@end


10、做一个登录界面,弹出一个带导航栏的模态视图LoginViewController,这里让LoginViewController作为一个NavigationController的root view controller。

11、连线,类型是Modally,设置Identifier,如图5。


                                                                     图5


工程样例


0 0