Container views with segmented control in swift

793 views Asked by At

I have a segmented control with three segments. "Cattle", "Sheep" and "Goats". In Sheep and Goats there is another segmented control "RFID" and "Mobs"

I have used three container views on a parent viewController, a cattleView, sheepGoatMob view and a sheepGoatRFID view which have UITableViewControllers CattleTableViewController, SheepGoatMobTableViewController and SheepGoatRfidTableViewController. The parent view contains the if statement to hide/show each view, which works okay..

The problem that I am having is that each child view needs to be able to send the info on their pages to a soap web service from a UIBarButtonItem on the parent view. My first thought was to have a "send" button in each child view but because all three views are loaded into memory when the app starts, the button doesn't know which view function to call.

EDIT : How can I accomplish setting a button in each of the three views for the UIBarButtonItem of the parent viewController and allowing the correct function from the childViewControllers to be called?

Option 2: How could I access the three childviewcontroller's function through a UIBarButtonItem on the parent viewcontroller?

1

There are 1 answers

0
Tyron On BEST ANSWER

I found a solution to have all three views loaded into memory and be able to change which function I am calling using @Julian's response: Objective-c: How to invoke method in container view controller

//Determine which view is showing and point to the correct child class
func sendTransactionButton(sender: UIBarButtonItem) {
   log.debug("send transaction button called p2p")

    for childViewController in self.childViewControllers {

        if childViewController.isKindOfClass(CattleTableViewController) && containerTableViewCattle.hidden == false {

            var cattleVC = childViewController as! CattleTableViewController
            cattleVC.transactions()

        } else if childViewController.isKindOfClass(SheepGoatRfidTableViewController) && containerSheepGoatsRfid.hidden == false {

            var sheepGoatRFIDVC = childViewController as! SheepGoatRfidTableViewController
            sheepGoatRFIDVC.transactions()

        } else if childViewController.isKindOfClass(SheepGoatTableViewController) && containerTableViewSheepGoats.hidden == false {

            var sheepGoatsMob = childViewController as! SheepGoatTableViewController
            sheepGoatsMob.transactions()
        }
    }
}

You need to ensure that your other views are hidden or you will get a warning from xcode.