如何在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,並且在右邊工程中新建一個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,之後我會詳細寫一篇關於新浪微博開發的文章。


發佈了66 篇原創文章 · 獲贊 18 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章