2015-04-07 47 views
0

我对Swift还很陌生,并且在做一些个人项目来帮助我理解事情。MasterViewController BarButton在显示详细信息后失踪Segue被触发

现在,我使用Master-Detail应用程序作为模板。在MasterViewController上,它是一个动态的TableViewController。

我想要实现的是,当我点击任何单元格时,MasterViewController将显示另一个导航列表(我已经设法使用Push segue进行设置),并且在DetailViewController上,而不是调用DetailViewController,它目前正在调用ContentsViewController,它是一个带有TabBar的ViewController(我也已经使用Ctrl单击了Cell并使用Accessory Action - > Show)。

源代码片段触发SEGUE

MasterViewController.swift

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if segue.identifier == "showContentDetailSegue" { 
     if let indexPath = self.tableView.indexPathForSelectedRow() { 
      performSegueWithIdentifier ("ContentDetailSegue", sender: self) 
     } 
    } 
} 

眼下,ContentsViewController已经显示没有问题。但是,左上角的BarButton不再具有切换MasterViewController的主BarButton。

我也试过self.presentViewController,但它将取代整个屏幕,这不是我心目中的,因为我想保持分割视图完好无损。

我哪里错了?任何帮助表示赞赏。

+0

通常情况下,相应的快捷文件下列代码,你不应该从prepareForSegue()调用“performSegueWithIdentifier”。后一个函数被调用来设置目标控制器上的一些数据。当它被称为一个segue已经在进行中。因此,它看起来像你同时开始两个赛段,这可能是什么情况下的问题..你可以重写代码,以避免使用prepareForSegue? – Alexey

+0

那我该怎么去做呢? – Gino

+0

如果你与我分享你的整个项目,然后我会看看。 – Alexey

回答

0

好的,发现另一个问题,当试图点击其他选项卡,其中左上角的主将是错误的,直到我点击第一个选项卡。

要解决这个问题,我用的是什么建议在http://nshipster.com/uisplitviewcontroller/

包含在被迷上了故事板xyzViewController

navigationItem.leftBarButtonItem = splitViewController?.displayModeButtonItem() 
navigationItem.leftItemsSupplementBackButton = true 
1

最后,我改变了看你的代码。顺便说一下,今天我不得不升级到Xcode 6.3,所以我也必须将你的代码改为Swift 1.2(没有太大变化)。

所以,你的第一个目标是使DetailsViewController成为一个UITabbar。问题在于该项目的故事板不能反映这种意图。实际上,SplitVC的第二行不会去Tabbar控制器,但它应该。以下是更正的故事板。

storyboard

第二个问题是在prepareForSegue。你正在第一个内部开始第二个过渡,结果是正确的。但是,您还需要处理第二SEGUE,为了增加< Back按钮:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 

    if segue.identifier == "showDetail" { 
     ... 
     // start second segue 
     performSegueWithIdentifier ("ContentDetailSegue", sender: self) 


    } else if segue.identifier == "ContentDetailSegue" { 
     // add back button 
    } 
} 

添加后退按钮是有点棘手。由于目标VC尚未加载,因此您的不能将加回到其导航栏。它根本不存在(它将在该类的viewDidLoad()方法中提供)。

因此,我们将保存的指针回到一个成员变量按钮,像这样:

detailsViewController.leftButton = self.splitViewController?.displayModeButtonItem() 

viewDidLoad安装:

override func viewDidLoad() { 
    super.viewDidLoad() 

    if let button = leftButton { 
     navigationItem.leftBarButtonItem = button 
    } 
} 

有了这些修复一切完美。你可以找到修改后的代码here

+0

Darn,这是我必须消化的一些新概念= D,这对我来说是一个快乐的问题。感谢大家,我会解决它,并打破它,并尝试纳入我最近学会的一些概念,如代表。 +1 – Gino