开发笔记:导航控制器(二)(故事板)push modal custom

来源:互联网 发布:dnf超级技能源码 编辑:程序博客网 时间:2024/05/19 12:27

在苹果公司的官方demo 可以看出 已经更新了用故事板模式 布局UI ,  故事板模式给程序员带来了很大的方便,

在页面之间的跳转通过拖拽,也变的非常简单,也更加清晰的看见应用程序是如何布局的,对与其他开发人员维护带来了很大的方便,

废话不多少 直接主题

一  创建导航控制器

拖拽一个视图的导航控制器放在故事板中

然后按住ctrl 键 向你的根视图拖拽过去,这时候我们发现有几个连接的选项 push,modal ,custom , root view controller。

很显然我们应该连接的是 root view controller。 这样我们就创建完了导航控制器!真的很简单。


二 push

push 到下个页面也超级简单!在导航栏上拖拽个buttonitem 按钮  然后点击buttonitem 按住ctrl 拖拽到你想跳转的页面上,连接push。

这样就ok了,so easy吧 。 

我们跳转的时候发现 back 还是英文的!

现在我们用鼠标点击下导航栏 我们发现在右边出现了Navigation item 的设置栏

如下图 我们可以设置标题 ,提示 ,返回按钮 。


如果你想发回按钮是自己定制的换个图片貌似没有看见相关的设置 !那也无妨,添加少量代码还是可以的

在第一章我写到的方法 这样就可以改变返回按钮的样式了


    UIImage *imge = [UIImageimageNamed:@"导航_返回.png"];

    UIBarButtonItem *leftButton = [[UIBarButtonItem  alloc]initWithImage:imgestyle:UIBarButtonItemStyleDonetarget:selfaction:@selector(pushActions)];

    self.navigationItem.leftBarButtonItem = leftButton;

    self.view.backgroundColor = [UIColorgreenColor];


三 mode

这个同理也是拖拽过去连接mode 就ok 了

返回要用代码

- (IBAction)dicMissAction:(id)sender

{

    [self    dismissViewControllerAnimated:NOcompletion:nil];

}


点击连接线路中间的圆环在这里我们可以设置mode 的动画效果和 标识符 和 格式



四 custom

同理拖拽,连接custom

然后再创建一个 用来自定义跳转的Segue Class函数

如图 subclass 选择  UIStoryboardSegue


然后,点击连线中间的圆圈 设置标识符 关联上创建的类!




这两样都设置完就要写代码自定义了

代码也是很简单的  在Segue Class函数 里添加头文件和自己定义的代码

你可义在里面定义自己想要的动画!我只是简单写了个push!


#import "CustomViewController.h"

#import "ViewController.h"

@implementation CustomNavigation

-(void)perform

{

     ViewController *rootView =self.sourceViewController//源视图

     CustomViewController *coustomViews =self.destinationViewController;//目标视图

    

    [rootView.navigationController   pushViewController:coustomViewsanimated:YES];

}


导航控制四种连接方式都说完了!

也顺便说下 传值吧


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    

    

    

       NSString *name = @"leng";

         //topush 为圆环内的标识符

      if ([segue.identifier     isEqualToString:@"topush"])

      {

          id destination = segue.destinationViewController;

          //theName 为要给赋值那个字符串的名字

          [destination setValue:name forKey:@"theName"];

      }


}


还有就是我们经常用的tableview 里 didSelectRowAtIndexPath 跳转!但是传值的时候先走的prepareForSegue 

说以我们就不能在didSelectRowAtIndexPath 处理数据了,他的执行顺序是

willSelectRowAtIndexPath --> prepareForSegue --> didSelectRowAtIndexPath


- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath

 {

      return  indexPath;  

}


demo 源码 http://download.csdn.net/detail/lengshengren/6582289


原创粉丝点击