掌握细节应用,传递对象

时间:2015-06-24 02:16:47

标签: ios swift xcode7 swift2

我确信我忽视了一些基本的东西,但我对此感到沮丧。我无法访问行中所选对象的属性并传递给详细视图。这是相关的代码:

的AppDelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    // Override point for customization after application launch.
    let splitViewController = self.window!.rootViewController as! UISplitViewController
    let navigationController = splitViewController.viewControllers[splitViewController.viewControllers.count-1] as! UINavigationController
    navigationController.topViewController!.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
    splitViewController.delegate = self

    let masterNavigationController = splitViewController.viewControllers[0] as! UINavigationController
    let controller = masterNavigationController.topViewController as! MasterViewController
    controller.managedObjectContext = self.managedObjectContext
    return true
}

MasterViewController:

override func viewDidLoad() {
    super.viewDidLoad()
    if let split = self.splitViewController {
        let controllers = split.viewControllers
        self.detailViewController = (controllers[controllers.count-1] as! UINavigationController).topViewController as? DetailViewController
    }
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showDetail" {
        if let indexPath = self.tableView.indexPathForSelectedRow {
            let object = matchups[indexPath.row]
            let controller = (segue.destinationViewController as! UINavigationController).topViewController as! DetailViewController
            controller.detailItem = object
            controller.navigationItem.leftBarButtonItem = self.splitViewController?.displayModeButtonItem()
            controller.navigationItem.leftItemsSupplementBackButton = true
            controller.detailItem = matchups[indexPath.row]
        }
    }
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if (detailViewController != nil) {
        self.detailViewController!.detailItem = matchups[indexPath.row]
    }
}

DetailViewController:

var detailItem: AnyObject? {
    didSet {
        self.configureView()
    }
}


func configureView() {
    // Update the user interface for the detail item.
    if let detail = self.detailItem {
        if let label = self.detailDescriptionLabel {
            label.text =  detail //** This is where the problem lies **//
        }
    }
}

这是Apple模板代码。我也尝试过使用替代方法的网络教程,但我仍遇到问题。我错过了什么?提前致谢。

1 个答案:

答案 0 :(得分:1)

Test是String,DetailItem是AnyObject?。在分配之前,您需要将detailItem强制转换为所需类型。

假设detailItem类型是什么?它只是一个字符串?在这种情况下你可以写为?在你的if定义中,这就足够了。