怎样在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,之后我会具体写一篇关于新浪微博开发的文章。


原文地址:https://www.cnblogs.com/bhlsheji/p/4198552.html