stringRay2014
stringRay2014

Reputation: 646

having 2 or more navigation controllers embedded on a stack

Im trying to get two navigation controller on the same stack, and pass data between them , the best way to explain this is by the image below .

storyboards

first nav controller(the one thats furthest to the left ) is connected to a TabBarController .

When passing data from the TEST tableViewController to the 2nd tableViewController (subDuaList) , i get an error in my code in the prepareForSegue method ...

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

        var subDua:subDuaList = segue.destinationViewController as subDuaList //ERROR OCCURS HERE

        var Index = tableView!.indexPathForSelectedRow()?.row

        var duaIndex = 130 - Index!

        var selectedDua = self.packagedChapter[duaIndex]
        println(selectedDua.title)

        subDua.duaArray = selectedDua.dataArray
        subDua.rowPressed = Index

    }

Reason for embedding the second tableViewController in a Navigation controller is so that I can add a bar button item to the nav bar.

Upvotes: 0

Views: 738

Answers (2)

anneblue
anneblue

Reputation: 706

EDIT

As @Shim commented right, my answer just explains the reason for the error output, but does not tell the things I already said in my comment on your question:

It is not allowed to push a UINavigationController on an existing navigation stack. Therefore you should remove the second navigation controller and just add a UIBarButtonItem to the navigationItem of your subDuaList view controller.


The reason for your error is, that the segue.destinationViewController is the second UINavigationController and not the subDuaList view controller.

If it was allowed, you could have repaired your code as follows by replacing:

var subDua:subDuaList = segue.destinationViewController as subDuaList //ERROR OCCURS HERE 

with

var navCtrl:UINavigationController = segue.destinationViewController as UINavigationController 
var subDua:subDuaList = navCtrl.childViewControllers[0] as subDuaList

Upvotes: 1

shim
shim

Reputation: 10146

You don't need a new navigation controller to add a bar button item. You can add a bar button item in the storyboard by dragging it onto the navigation bar (if the view controller is in a navigation stack and you don't see one, check the simulated metrics in the inspector), or you can add it in code like so:

self.navigationItem.rightBarButtonItem = myBarButtonItem;

Upvotes: 1

Related Questions