Storyboard

iOS9中storyboard最大的變化有三點:

  • 通過storyboard references來連接不同的storyboard.(這點超贊,對于把一個storyboard分割成多個storyboard及其方便,在團(tuán)隊開發(fā)中非常有用)
  • 可以為view controller中添加額外的小的view塊.
  • 可以在navigation bar中添加兩個按鈕.
    如何將已有的storyboard分割成多個并且用到storyboard references呢?很簡單,選中你要分割的storyboard:
Screen Shot 2015-10-28 at 8.04.23 PM.png

然后進(jìn)行這樣的操作: Editor->Refactor to Storyboard輸入你對這個storyboard的命名Checklists并且選擇合適的位置,繼而點擊保存.這樣你就完成了對已選頁面建立了一個新的storyboard.而原來的storyboard變?yōu)榱?

Screen Shot 2015-10-28 at 8.12.27 PM.png

而將這個圖的局部放大就會看到storyboard reference了:

Screen Shot 2015-10-28 at 8.13.05 PM.png

我們可以把這個Referenced ID 清除掉,再在新建的Checklists storyboard中指定initial View Controller,于是就完成了對新建storyboard的使用:

Screen Shot 2015-10-28 at 8.19.58 PM.png

我們也可以在新建頁面中使用storyboard reference,在Object Library中拖個相關(guān)的控件到里面:

Screen Shot 2015-10-28 at 8.21.29 PM.png

然后按住Ctrl連接:

Screen Shot 2015-10-28 at 8.24.01 PM.png

在屬性里選擇你要連接的storyboard即可,如果你填寫Referenced ID 即表示你要連接該storyboard對應(yīng)ID號的界面.

Screen Shot 2015-10-28 at 8.24.09 PM.png

Done:

Screen Shot 2015-10-28 at 8.24.18 PM.png
  • 在頁面增加小的Views,操作如下:
Screen Shot 2015-10-28 at 8.40.20 PM.png

我們可以在這個頁面添加一個控件并命名為notesTextView,我們要在點擊UITableView中的其中一個Cell時展示其中的內(nèi)容,添加相關(guān)方法代碼:

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
 guard let cell = tableView.cellForRowAtIndexPath(indexPath) as? ChecklistItemTableViewCell else { return }
 tableView.beginUpdates()
 if cell.stackView.arrangedSubviews.contains(notesView) { removeNotesView()
 } else { addNotesViewToCell(cell)
 notesTextView.text = checklist.items[indexPath.row].notes }
 tableView.endUpdates()}
func addNotesViewToCell(cell: ChecklistItemTableViewCell) { notesView.heightAnchor
 .constraintEqualToConstant(notesViewHeight)
 .active = true notesView.clipsToBounds = true
 cell.stackView.addArrangedSubview(notesView)}
func removeNotesView() { if let stackView = notesView.superview as? UIStackView {
 stackView.removeArrangedSubview(notesView) notesView.removeFromSuperview()
}
}

效果如下:

Screen Shot 2015-10-28 at 8.58.23 PM.png
  • 你可以在Navigation bar添加兩個按鈕了:

Screen Shot 2015-10-28 at 9.03.14 PM.png

我以前寫過一個使用多個storyboard的小例子.
其實關(guān)鍵代碼沒有多少:

- (id)viewControllerWithIdentifier:(NSString *)identifier inStoryboard:(NSString *)storyboardName {
    
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    if (storyboard) {
        return [storyboard instantiateViewControllerWithIdentifier:identifier];
    } else {
        return nil;
    }
}

用的時候也很簡單:

- (void)setUpViewControllers {
    
    self.oneViewController = [self viewControllerWithIdentifier:@"OneFirst" inStoryboard:@"One"];
    self.oneViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Girl" image:nil selectedImage:nil];
    self.twoViewController = [self viewControllerWithIdentifier:@"TwoFirst" inStoryboard:@"Two"];
    self.twoViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Learn" image:nil selectedImage:nil];
    self.threeViewController = [self viewControllerWithIdentifier:@"ThreeFirst" inStoryboard:@"Three"];
    self.threeViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"iOS" image:nil selectedImage:nil];
    self.fourViewController = [self viewControllerWithIdentifier:@"FourFirst" inStoryboard:@"Four"];
    self.fourViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Storyboard" image:nil selectedImage:nil];
    self.viewControllers = @[self.oneViewController,self.twoViewController, self.threeViewController, self.fourViewController];
    
}

嗯哼,這算是#Girl學(xué)iOS100天#系列的第一篇,希望自己能夠堅持!
加油!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容