我正在尝试在
Swift中创建一个MasterDetail应用程序,它在iOS8模拟器上运行良好.但是,当我在iOS 7.1 iPad上试用它时出现了这个错误:
**Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '- [UISplitViewController displayModeButtonItem]: unrecognized selector sent to instance**
这是在我的AppDelegate.swift文件中(由Xcode生成,我没有添加任何内容):
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
}
问题来自创建左按钮的这一行:
navigationController.topViewController.navigationItem.leftBarButtonItem = splitViewController.displayModeButtonItem()
当我删除此行时,它在iOS 7上运行但仅显示DetailView.当我从左边缘滑动时,MasterView没有出现(在iOS 8模拟器上它确实如此)并且基本上没有导航到MasterView
有没有人有同样的问题?
谢谢!
最佳答案 您仍然可以在UISplitViewControllerDelegate中使用不推荐的回调函数来添加和删除UIBarButtonItem到IOS7平台的详细视图.在UISplitViewControllerDelegate中实现如下
func splitViewController(svc: UISplitViewController, willHideViewController aViewController: UIViewController, withBarButtonItem barButtonItem: UIBarButtonItem, forPopoverController pc: UIPopoverController) {
if !self.respondsToSelector(Selector("displayModeButtonItem")) {
let navigationController = self.viewControllers.last as! UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
barButtonItem.image = UIImage(named: "IC_BackArrow")
detailViewController?.navigationItem.leftBarButtonItem = barButtonItem
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}
func splitViewController(svc: UISplitViewController, willShowViewController aViewController: UIViewController, invalidatingBarButtonItem barButtonItem: UIBarButtonItem) {
if !self.respondsToSelector(Selector("displayModeButtonItem")) {
let navigationController = self.viewControllers.last as! UINavigationController
let detailViewController: UIViewController? = navigationController.viewControllers?.last as? UIViewController
detailViewController?.navigationItem.leftBarButtonItem = nil
} else {
// This callback function is depreciated in IOS8. We use displayModeButtonItem.
}
}