当对象数组为空时显示 UISplitview 辅助视图的回退屏幕

Showing fall back screen for UISplitview Secondary view when Object array is empty

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

所以我的问题是,当没有对象时,如何设置一个单独的 "no objects founds" 风格的屏幕来回退?

谢谢,这是我的 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.
    }

好的,明白了,这就是我需要做的。

  1. 创建一个新的UIViewController,我只是调用它'NoObjectVC'并连接起来。
  2. 在情节提要中将其连接到 SplitView 控制器作为关系 segue 详细视图控制器,嵌入导航控制器。
  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)
        }
    }
    

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