0

我有一个关于最初在 UISplitView 中加载辅助视图的快速问题。我目前在我的 masterVC.swift 中有代码,如果有的话,用数组中的第一个对象填充 detailsVC。这很好用,问题是当数组为空时,我可以预见地得到fatal error: unexpectedly found nil while unwrapping an Optional value.

所以我的问题是,当没有对象时,我如何设置一个单独的“没有对象发现”样式的屏幕来回退?

谢谢,这是我的 MasterVC viewDidLoad 中的代码

let initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
    if objects.count != 0 {
        self.tableView.selectRowAtIndexPath(initialIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)

        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            self.performSegueWithIdentifier("showDetailsSegue", sender: initialIndexPath)
        }
    } else {
        // I'm guessing the code would go here.
    }
4

1 回答 1

0

好的,想通了,这就是我需要做的。

  1. 创建一个新的 UIViewController,我只是将其命名为“NoObjectVC”并将其连接起来。
  2. 在情节提要中,将其作为关系segue Detail 视图控制器连接到SplitView Controller,嵌入在导航控制器中。
  3. 从 MasterVC 创建一个 segue,我称之为“noObjectSegue”
  4. 在 MasterVC 的 prepareForSegue 函数中添加以下内容:

    else if segue.identifier == "noObjectSegue" {
     let navigationController = segue.destinationViewController as! UINavigationController
     let controller = navigationController.topViewController as! NoObjectVC
     controller.title = "No Objects Found"
    }
    
  5. 最后在 MasterVC 中, viewDidLoad() 添加:

    let initialIndexPath = NSIndexPath(forRow: 0, inSection: 0)
    
    if objects.count != 0 {
        self.tableView.selectRowAtIndexPath(initialIndexPath, animated: true, scrollPosition:UITableViewScrollPosition.None)
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            self.performSegueWithIdentifier("showDetailSegue", sender: initialIndexPath)
        }
    } else {
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            print("No Objects Available")
            self.performSegueWithIdentifier("noObjectSegue", sender: self)
        }
    }
    

不确定这会对任何人有帮助,但我想我会记录下来以防万一。

于 2016-02-20T22:59:55.837 回答