怎樣在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,之后我會具體寫一篇關於新浪微博開發的文章。



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM