主从申请,传递对象
Master detail application, passing object
我确信我忽略了一些基本的东西,但我对此感到很沮丧。我无法访问在行中选择并传递给详细视图的对象的属性。这是相关代码:
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
}
主视图控制器:
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 **//
}
}
}
这是苹果模板代码。我也尝试过遵循使用替代方法的网络教程,但我仍然 运行 遇到问题。我错过了什么?提前致谢。
Test 是 String,DetailItem 是 AnyObject?。在分配之前,您需要将 detailItem 转换为所需的类型。
detailItem 类型应该是什么?它只是一个字符串吗?在这种情况下你可以写成?在您的 if let 定义中,这就足够了。
我确信我忽略了一些基本的东西,但我对此感到很沮丧。我无法访问在行中选择并传递给详细视图的对象的属性。这是相关代码:
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
}
主视图控制器:
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 **//
}
}
}
这是苹果模板代码。我也尝试过遵循使用替代方法的网络教程,但我仍然 运行 遇到问题。我错过了什么?提前致谢。
Test 是 String,DetailItem 是 AnyObject?。在分配之前,您需要将 detailItem 转换为所需的类型。
detailItem 类型应该是什么?它只是一个字符串吗?在这种情况下你可以写成?在您的 if let 定义中,这就足够了。