iOS developiOS MGO 专栏iOS开发

Storyboard

2015-10-28  本文已影响1991人  Girl_iOS

iOS9中storyboard最大的变化有三点:

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

然后进行这样的操作: Editor->Refactor to Storyboard输入你对这个storyboard的命名Checklists并且选择合适的位置,继而点击保存.这样你就完成了对已选页面建立了一个新的storyboard.而原来的storyboard变为了:

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中拖个相关的控件到里面:

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对应ID号的界面.

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

Done:

Screen Shot 2015-10-28 at 8.24.18 PM.png Screen Shot 2015-10-28 at 8.40.20 PM.png

我们可以在这个页面添加一个控件并命名为notesTextView,我们要在点击UITableView中的其中一个Cell时展示其中的内容,添加相关方法代码:

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 Screen Shot 2015-10-28 at 9.03.14 PM.png
我以前写过一个使用多个storyboard的小例子.
其实关键代码没有多少:
- (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学iOS100天#系列的第一篇,希望自己能够坚持!
加油!

上一篇下一篇

猜你喜欢

热点阅读