导航控制器

来源:互联网 发布:mac flamingo口红 编辑:程序博客网 时间:2024/05/01 18:50

要想控制器变成导航控制器,需要创建一个NavViewController,重写window将根视图控制器设为导航控制器,再将其他ViewController添加到NavViewController。

AppDelegate.h


CustomerViewController中可以设置导航控制器,(CustomerViewController:UINavigationController

如改变状态栏的颜色,直接调用

- (UIStatusBarStyle)preferredStatusBarStyle{

   //将状态栏的颜色变为白色

    returnUIStatusBarStyleLightContent;

}

也可以隐藏状态栏

- (BOOL)prefersStatusBarHidden{

    returnYES;

}

RootViewController.m

- (instancetype)init

{

    self = [superinit];

    if (self) {

        self.navigationItem.title =@"主页";

//        self.title = @"主页";

    }

    returnself;

}

 //为导航栏按钮添加图片

//UIBarButtonItem *rightImageBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back_btn"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:nil action:nil];

#pragma mark - 工具栏

    UIBarButtonItem *cameraItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCameratarget:nilaction:nil];

    UIBarButtonItem *bookItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarkstarget:nilaction:nil];

    UIBarButtonItem *refershItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefreshtarget:nilaction:nil];

    //间隔符,根据按钮的多少来显示间隔多少

    UIBarButtonItem *spaceItem = [[UIBarButtonItemalloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpacetarget:nilaction:nil];

//    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

//    [spaceItem setWidth:100];//固定了距离

    self.toolbarItems =@[cameraItem,spaceItem,bookItem,spaceItem,refershItem];

    /**< 返回按钮 需要在上一个控制器中添加*/

    UIBarButtonItem *backItem = [[UIBarButtonItemalloc] initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:nilaction:nil];

    self.navigationItem.backBarButtonItem = backItem;

}


- (void)barButtonProcess:(UIBarButtonItem *)sender{


    NSInteger index = sender.tag -100;

    switch (index) {

        case0:

            [selfshare];

            break;

            

        default:

            break;

    }

}

- (void)share{

    //分享

    /*

     UIAlertController在功能上是和UIAlertView以及UIActionSheet相同的,UIAlertController以一种模块化替换的方式来代替这两货的功能和作用。是使用对话框(alert)还是使用上拉菜单(action sheet),就取决于在创建控制器时,您是如何设置首选样式的。

 UIAlertControllerStyleAlert:警告框模式,从中间弹出

 UIAlertControllerStyleActionSheet:从底部弹出

     */

    //标题,信息,默认样式

    UIAlertController *alertVc = [UIAlertControlleralertControllerWithTitle:@"分享"message:@"我的分享"preferredStyle:UIAlertControllerStyleActionSheet];

    /*

     创建UIAlertAction的实例,您可以将动作按钮添加到控制器上。UIAlertAction由标题字符串、样式以及当用户选中该动作时运行的代码块组成。通过UIAlertActionStyle,您可以选择如下三种动作样式:常规(default)、取消(cancel)以及警示(destruective)

     */

    //添加按钮

    UIAlertAction *action = [UIAlertActionactionWithTitle:@"新浪微博"style:UIAlertActionStyleDefaulthandler:nil];

    UIAlertAction *action1 = [UIAlertActionactionWithTitle:@"微信"style:UIAlertActionStyleDestructivehandler:nil];

    UIAlertAction *action3 = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:nil];

    [alertVc addAction:action];

    [alertVc addAction:action1];

    [alertVc addAction:action3];

    [self presentViewController:alertVcanimated:YEScompletion:nil];

}

- (void)buttonPressed:(UIButton *)sender{

    TwoViewController *detailVc = [[TwoViewControlleralloc] init];

    //通过导航控制器来推动,A -> B,A用pushViewControllerB->A,B用popToRootViewControllerAnimated

    [self.navigationControllerpushViewController:detailVcanimated:YES];

}

页面间的跳转

//返回上一个控控制器

    //self.navigationController.viewControllers[1]拿到栈里面的控制器,否则就是拿到的新建的控制器

    //指定到某一个控制器

//    [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];

    //返回到跟控制器

//    [self.navigationController popToRootViewControllerAnimated:YES];

    //返回到上一个控制器

      [self.navigationControllerpopViewControllerAnimated:YES];


          

0 0
原创粉丝点击