MooseMan55 MooseMan55 - 1 month ago 6
Swift Question

Segueing to a different tab in a UITabController

I am trying to segue data from my "create a party" form into my second "Find Parties" tab.

Here is a screen shot of my main story board:

main story board

my code for the create a party controller:

import UIKit

struct party {
var name: String
var location: String
var description: String
}

class FirstViewController: UIViewController {

@IBOutlet weak var nameField: UITextField!
@IBOutlet weak var locationField: UITextField!
@IBOutlet weak var descriptionField: UITextView!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

var newParty = party(name: "", location: "", description: "")

@IBAction func create(_ sender: AnyObject) {
newParty = party(name: nameField.text!, location: locationField.text!, description: descriptionField.text!)
nameField.text = ""
locationField.text = ""
descriptionField.text = ""
performSegue(withIdentifier: "toPartyList", sender: newParty)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationViewController : SecondViewController = segue.destination as! SecondViewController
destinationViewController.parties.append(newParty)
}

}


Code for the find parties tab:

import UIKit

class SecondViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var partyTable: UITableView!

override func viewDidLoad() {
super.viewDidLoad()

partyTable.delegate = self
partyTable.dataSource = self

// updates the table of users everytime submit is clicked
DispatchQueue.main.async{
self.partyTable.reloadData()
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

var parties = [party]()

// creates the number of cells in the table
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parties.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// Make table cells the show the user name
let cell = UITableViewCell()
cell.textLabel?.text = parties[indexPath.row].name
return cell
}

// Allows the user to swipe and delete people from the table and also the users array
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {

if editingStyle == .delete {

// delete the person in users array
parties.remove(at: indexPath.row)

// delete the cell from the table
partyTable.deleteRows(at: [indexPath], with: .fade)
}
}


}


currently my segue just takes me directly to the "Find Parties" view controller with no way back, and not to the tab. what do I need to change in my segue to do this?

Answer

Rather than use a segue in this case, just switch tabs programatically:

@IBAction func create(_ sender: AnyObject) {
    newParty = party(name: nameField.text!, location: locationField.text!, description: descriptionField.text!)
    nameField.text = ""
    locationField.text = ""
    descriptionField.text = ""
    tabBarController?.selectedIndex = 1 // 2nd tab
}

To pass information along, you can do something like this:

let navVC = tabBarController.viewControllers[1] as! UINavigationController
let rootVC = navVC.viewControllers[0] as! SomeViewController
rootVC.someData = myData