Put Background Data in UITableViewController - swift

I have a UITableView with 3 dynamic Cells.
Cell A
Cell B
Cell C
While clicking a Cell, a new View will be opened with a Segue. I commit the Text "Cell A" (...) to the other View. But I need other Data to proceed (for example an unique ID). How can I transfer this without the user can see?

You can do something like this:
1) Create a variable yourUniqueId in your destination view controller.
2) Add following method in your root view controller.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "YourSegue"
{
let tempController = segue.destinationViewController as! DestinationViewController
tempController.yourUniqueId = "UNIQUEID"
}
}

Related

connect segue to Navigation control

Before I used a Segue to transfer data between to view controllers.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Edit List" {
if let currentTodo = todoReadyForEditing, let editControler = segue.destination as? ListPageController {
editList.prepare(for: currentList)
}
Now the plan is changed I should transfer the data from the first view controllers to a tableViewController. Since I want to have a navigation bar to the tableViewController page, I added a navigationBarController before it.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Edit List" {
if let currentTodo = todoReadyForEditing, let editControler = segue.destination as? ListTableViewController {
editList.prepare(for: currentList)
}
The problem is when I connected segue directly to UITableViewController, everything is work, same as before that I have two view controllers, but the navigation bar is hidden, which I don't want. As I read, apparently i have to connect the segue first to the navigationViewController, but when I did not, the data won't transfer to the table view controller.
Is there any suggestion? Thanks for your help in advance
Destination of segue is UINavigationController, but you can work with its topViewController property which is your controller
guard let navCon = segue.destination as? UINavigationController,
let controller = navCon.topViewController as? ListTableViewController else { fatalError("Segue wasn’t set right") }
Anyway, I would suggest you don’t use segues and create your UINavigationController with embedded controller programmatically

Difference Between 2 Ways of Declaring a ViewController Object

So I experimented with these 2 different ways of declaring a ViewController variable and it seemed to offer me the same results. However, I do feel there must be a difference between setting the destinationVC variable because if not, won't people use the more straightforward way of just declaring a new object?
[using segue.destination as! ViewControllerName]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "changeCityName" {
let destinationVC = segue.destination as! ChangeCityViewController
destinationVC.delegate = self
}
}
[using ViewControllerName()]
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "changeCityName" {
let destinationVC = ChangeCityViewController()
destinationVC.delegate = self
}
}
In the prepareForSegue method, these two methods of creating a new VC differs greatly.
If you use segue.destination, you refer to the specific VC that the segue is going to, i.e. the one in your storyboard that the segue is connected to. If you create a new VC, then the VC you created won't be the same one as the segue is going to. i.e. you are dealing with a separate VC. Setting the delegate of the newly created VC won't do anything to the VC that is actually being presented.
If you are talking about the difference between using a segue to present a VC and this:
let vc = SomeViewController()
self.present(vc, animated: true)
Then the difference is less. If you use segues, then the views in the view controller will be read from the storyboard (NIB) file. If you create the VC by calling the initializer, you will have to handle adding the views in your view controller class.
Result may be visually same but its not true.
If you don't put any code inside prepare(for segue) still you will get same result(visually)
prepare(for segue) is called when UIViewControllers are connected through storyboard.
Since UIViewControllers are already connected in storyboard, so the destination UIViewController is called on your desired event.
In your first case using (segue.destination as! ViewControllerName) which is correct way of using segue.
Before going further one more thing is to be discussed about and that is
Why we are required to write code inside prepare(for segue) if its already connect through storyboard
1.From one button action you can connect several segues depending on your requirements, but each time button is pressed same prepare(for segue) method will be called, so to differentiate which UIViewController is to be called we do something like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "FirstViewControllerIdentifier")
{
}else if(segue.identifier == "SecondViewControllerIdentifier"){
}else if(segue.identifier == "ThirdViewControllerIdentifier"){
}else{
// and so no
}
}
Now here we get object of destination controller(UIViewController) already being prepared.So we are not required to make a new object of destination controller
2.We can pass data to destination controller and also we can set delegate
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "FirstViewControllerIdentifier")
{
// here we get object of first view controller and set delegate
let firstVC = segue.destination as! FirstViewController
firstVC.delegate = self
}else if(segue.identifier == "SecondViewControllerIdentifier"){
// here we get object of second view controller and pass some data to it
let secondVC = segue.destination as! SecondViewController
secondVC.someData = someData
}else if(segue.identifier == "ThirdViewControllerIdentifier"){
}else{
// and so no
}
}
Now in your second case using ViewControllerName() (the wrong code)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "FirstViewControllerIdentifier")
{
// here we create object of first view controller.This object is different from the destination view controller
//you create an object and set a delegate but after that you are not using that object and that object is totallu useless.
let firstVC = FirstViewController()
firstVC.delegate = self
// above code does not affect any thing but the contoller which is to be presented is destination view controller which is connected through storyboard
}
}
Hope you understand how to use segue and let me know if there is any problem

Segue is being executed twice

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.selectedClubState = stateNamesForDisplay[indexPath.row]
self.performSegueWithIdentifier ("Cities", sender: self)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var clubsToPassToCitiesViewController = [clubObject]()
if segue.identifier == "Cities" {
for club in clubsForTable{
if club.clubState == self.selectedClubState{
clubsToPassToCitiesViewController.append(club)
}
}
let citiesView = segue.destinationViewController as? citiesViewController
citiesView?.clubsForChosenCity = clubsToPassToCitiesViewController
}
}
Segue is being executed twice leading to the next VC. How can I prevent this from happening?
Delete the current segue in storyboard. Then CTRL-drag from the viewController (not the cell) to the next view controller and name it "Cities". Now, when you select a cell, the didSelectRowAtIndexPath() will fire first and will call performSegueWithIdentifier()
However, if all you're looking to do in the didSelectRowAtIndexPath() is get the row that performed the segue, you can maintain your original setup of having the cell segue from the storyboard, remove didSelectRowAtIndexPath() and in prepareForSegue() do:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let indexPath = self.tableView.indexPathForSelectedRow {
self.selectedClubState = stateNamesForDisplay[indexPath.row]
}
var clubsToPassToCitiesViewController = [clubObject]()
if segue.identifier == "Cities" {
for club in clubsForTable{
if club.clubState == self.selectedClubState{
clubsToPassToCitiesViewController.append(club)
}
}
let citiesView = segue.destinationViewController as? citiesViewController
citiesView?.clubsForChosenCity = clubsToPassToCitiesViewController
}
}
You are the one executing the segue twice — once automatically in the storyboard (because your segue emanates as an Action Segue from the cell prototype), and once in code when you say self.performSegueWithIdentifier. If you don't want the segue executed twice, remove one of those.
Personally, my recommendation is that you delete didSelectRow entirely and move your self.selectedClubState assignment into prepareForSegue.

Sending data from static cells in tableview in container view to view controller in Swift

swift noob here...
I have a view controller with a container view to a tableview controller because I want to use static cells. I have a label in the view controller that I want to be updated by a textfield in the static cell of the container tableview. Here's what I got, but the label isn't updating.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "tvEmbed") {
let newViewController = segue.destinationViewController as! ViewController
self.textField.text = newViewController.label.text
}
I know I don't have to use static cells for this purpose but this is just for me to get an idea of how it will work. Thanks.
You're setting the textField's text to the value from newViewController, not the other way around. Change your assignment to:
newViewController.label.text = self.textField.text

Using 3D Touch with storyboard's peek and pop

I found very easy way to use 3D Touch — check "Peek & Pop" in storyboard. But I'm struggling with one problem.
I have UITableView, when user touches cell all is working ok with my code:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
print(tableView.indexPathForSelectedRow)
}
}
So I'm filling data in my Detailed controller based on selected row. But when I'm pressing with Peek or Pop method tableView.indexPathForSelectedRow always returning nil (hm.. I haven't selected row, I'm just previewing so indexPath is nil I guess). How can I get that "peeked" cell indexPath to pass it to segue?
Or storyboard's Peek & Pop not working in this simple way and I need to fully implement peek & pop in my code?
It is possible to fully implement it using the storyboard and prepareForSegue. You can do this by making use of the sender object.
By assuming that you have created your segue directly from the table cell in the storyboard to the next view controller, then the sender object will be of the type UITableViewCell. If you trigger the segue programmatically, then just remember to set the sender object in the method call.
You can use this cell to get a hold of the NSIndexPath from the tableView, something similar to the following (ignore all the force unwrapping, this is just for demonstration purposes):
override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
let cell = sender as! UITableViewCell
let indexPath = tableView.indexPath(for: cell)!
if segue.identifier == "mySegue" {
let destination = segue.destination as! DetailsViewController
destination.model = self.model[indexPath.row]
}
}