主明细应用,传递对象
Posted
技术标签:
【中文标题】主明细应用,传递对象【英文标题】:Master detail application, passing object 【发布时间】:2015-06-24 02:16:47 【问题描述】:我确定我忽略了一些基本的东西,但我对此感到沮丧。我无法访问在行中选择并传递给详细视图的对象的属性。以下是相关代码:
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 **//
这是 Apple 模板代码。我也尝试过使用替代方法的网络教程,但我仍然遇到问题。我错过了什么?提前致谢。
【问题讨论】:
【参考方案1】:Test 是 String,DetailItem 是 AnyObject?。在分配之前,您需要将 detailItem 转换为所需的类型。
什么是 detailItem 类型?它只是一个字符串吗?在这种情况下,你可以写成?在你的 if let 定义中,这就足够了。
【讨论】:
我知道我遗漏了一些基本的东西。非常感谢。以上是关于主明细应用,传递对象的主要内容,如果未能解决你的问题,请参考以下文章