如何在xcode中使用storyboard

来源:互联网 发布:erp软件界面 编辑:程序博客网 时间:2024/04/28 18:08

  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,并且在右边工程中新建一个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,怎么获取它呢?很简单,首先,MainStoryboard中选中新建的tableview controller,设置其identifierTopicTableViewController,如图:


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

-(void)presentTimelineViewController:(BOOL)animated

{

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

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

。。。


[self.navigationControllerpushViewController:topicViewControlleranimated:animated];

}

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


原创粉丝点击