zoukankan      html  css  js  c++  java
  • 怎样在xcode中使用storyboard

      StoryBoardiOS 5的新特征,目的是取代历史悠久的NIB/XIB,对于已经习惯了xib文件的孩子们来说,StoryBoard还不是那么熟悉。经过两天的研究,有了一些心得,在此分享。

    一、怎样使用storyboard简单实现Push页面,过程例如以下:

    1、创建一个带有storyboardsingleview application应用程序如图。


    创建好的应用程序已经自己主动创建好了一个和MainStoryboard连接好的ViewController

    2、在MainStoryboard中,选中ViewController并拖入tableview以及tableviewCell,而且设置tableviewCellstyleBasicIdentifierCell,假设希望是自己定义cell的则须要选择custom,例如以下图,之后能够插入一个NavigationController


    不要忘记连接datasource和delegate。


    如今能够编码了,在ViewController.m

    #pragmamark - UITableViewDataSource


    -(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return1;

    }


    -(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    staticNSString*CellIdentifier = @"Cell";

    UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)

    {

    cell= [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault

    reuseIdentifier:CellIdentifier];

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;

    }

    cell.textLabel.text=@"话题";

    returncell;

    }


    3、如今实现简单的push功能:

    再次打开MainStoryboard文件,新拖入一个TableViewController,而且在右边project中新建一个TopicTableViewControllerh文件和m文件,选中MainStoryboard中的TableViewController,将其class设置为TopicTableViewController,同上设置好tableviewcell


    *右键选择前一个viewcontrollercell,连接push到新拖入的TableView Controller,例如以下图:



    这个时候执行就能正确push到新的tableview页面了。



    假设你希望在push页面的时候做些什么操作的话,能够在ViewController.m文件里编码:

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

    {

    if([[segueidentifier]isEqualToString:@"showSomething"]){

    //dosomething you want

    UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:nilmessage:@"test"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];

    [alertViewshow];

    }

    }

    记住一定要设置pushsegue,在这里我设置为showSomething

    执行能够看到在push页面的同一时候弹出了testalert框,如图:



    二、获取指定storyboard中的object

    前面的步骤依照第一、二步完毕,然后第三步完毕到*符号之前,这个时候看到的就是一个单独的新建的tableview controller,怎么获取它呢?非常easy,首先,MainStoryboard中选中新建的tableview controller,设置其identifierTopicTableViewController,如图:


    接着,在你须要使用它的函数里,例如以下:

    -(void)presentTimelineViewController:(BOOL)animated

    {

    UIStoryboard*storyboard = [UIStoryboardstoryboardWithName:@"MainStoryboard"bundle:nil];

    TopicTableViewController*topicViewController = [storyboardinstantiateViewControllerWithIdentifier:@"TopicTableViewController"];

    。。。


    [self.navigationControllerpushViewController:topicViewControlleranimated:animated];

    }

    好了,基本上对Storyboard有了一些了解了吧。看到我的測试应用程序名字是什么吗?对,SinaWeibo,之后我会具体写一篇关于新浪微博开发的文章。


  • 相关阅读:
    Sqlserver 实际开发中表变量的用法
    Python Day 20 面向对象 (面向对象的组合用法,面向对象的三大特性
    Python Day 19 面向对象(初识面向对象)
    Python Day 18 常用模块(模块和包)
    Python Day 17 常用模块(常用模块一 时间模块,random模块,os模块,sys模块,序列化模块)
    Python Day 15 函数(递归函数、二分查找算法)
    Python Day 14 函数(内置函数,匿名函数(lambda表达式))
    Python Day 13 函数(迭代器,生成器,列表推导式,生成器表达式)
    Python Day 11 + Python Day 12 函数(函数名的应用,闭包,装饰器)
    Python Day 10 函数(名称空间,作用域,作用域链,加载顺序等; 函数的嵌套 global,nonlocal)
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4263071.html
Copyright © 2011-2022 走看看