Swift / tableView pass data with instantiateViewControllerWithIdentifier - ios

I have an array of BackendlessUsers filled within a tableView. How can I pass the user within the didSelectRowAtIndexPath function?
var deejays: [BackendlessUser] = []
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("You selected cell #\(indexPath.row)!")
self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
**let user = deejays[indexPath.row]**
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("DJProfileAsUserVC") as! DJProfileAsUserVC
dispatch_async(dispatch_get_main_queue()) {
self.presentViewController(vc, animated: true, completion: nil)
}
}
Help is very appreciated.

Create a var user: DJProfileAsUserVC! in DJProfileAsUserVC and after that to this:
let user = deejays[indexPath.row]
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("DJProfileAsUserVC") as! DJProfileAsUserVC
vc.user = user

Related

Need to double tap on cell to present modally VC

I am generating table cell.
cell2 = settingsTableView.dequeueReusableCell(withIdentifier: "ModuleCell", for: indexPath) as! ModuleCell
It looks fine, the cell has tag = 2000.
In override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath), I am checking the tag end if tag == 2000 I want to present modal view. I am doing that in this way
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let modalView = storyboard.instantiateViewController(withIdentifier: "ModalView")
present(modalView, animated: true, completion: nil)
Then in modalView I have a button which should dismiss modalView, what's happening as expected.
#IBAction func saveAndClosePopup(_ sender: UIButton) {
UserDefaults.standard.removeObject(forKey: "ActiveCantachoOptions")
UserDefaults.standard.set(Modules.activeCantachoOptions, forKey: "ActiveCantachoOptions")
self.dismiss(animated: true, completion: nil)
}
However, when I want to immediately present modalView again, sometimes it's okay, but sometimes I need to hit two times on the cell, which should show modalView. After the first hit, there is no difference if I hit the cell again in 1 second or in 30 seconds. The modalView will appear after the second one. What is the cause?
Try this
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)
{
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let modalView = storyboard.instantiateViewController(withIdentifier: "ModalView")
self.present(modalView, animated: true) {
tableView.deselectRow(at: indexPath, animated: false)
}
}

storyboard?.instantiateViewController(withIdentifier: " ") as! viewcontroller

I have as a Menu tableview in my custom navigation bar. If i click on one cell , it should take me to another view controller. But my app crashes when i click on it.
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return menu_items.count //It has 5 values
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.value1, reuseIdentifier: "cell")
cell.textLabel?.text = menu_items[indexPath.row]
cell.textLabel?.textColor = .white
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = menu_items[indexPath.row]
if (item == "Patienten") {
tableView.removeFromSuperview()
let vc = self.storyboard?.instantiateViewController(withIdentifier: "patientView") as! patientViewVC //App crashes here
self.present(vc, animated: false, completion: nil)
} else if (item == "Mitarbeiter") {
} else if (item == "Kalender") {
} else if (item == "Tourenplanung") {
tableView.removeFromSuperview()
let vc = storyboard?.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
self.present(vc, animated: false, completion: nil)
}else if (item == "Abmelden") {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here
self.present(vc, animated: false, completion: nil)
}
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = colorLightGreen
}
}
I have many times verified all Identifier names and view controller names are correct.
Make sure you load a correct storyboard before instantiating the viewController.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let item = menu_items[indexPath.row]
if (item == "Patienten") {
tableView.removeFromSuperview()
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "patientView") as! patientViewVC
self.present(vc, animated: false, completion: nil)
} else if (item == "Mitarbeiter") {
} else if (item == "Kalender") {
} else if (item == "Tourenplanung") {
tableView.removeFromSuperview()
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "tour") as! tourVC //App crashes here
self.present(vc, animated: false, completion: nil)
}else if (item == "Abmelden") {
let storyBoard = UIStoryboard(name: "StoryboardName", bundle: nil)
let vc = storyBoard.instantiateViewController(withIdentifier: "loginvc") as! LoginVC //App crashes here
self.present(vc, animated: false, completion: nil)
}
let selectedCell:UITableViewCell = tableView.cellForRow(at: indexPath)!
selectedCell.contentView.backgroundColor = colorLightGreen
}
StoryboardName above is the name of the storyboard that contains the patientViewVC, tourVC, LoginVC
A view controller in storyboard needs to have a custom class set in Identity inspector:
Then you can cast it to desired type. If all the view controllers are in the same storyboard, you can instantiate them with storyboard property of self. If they are in different storyboard, you have to instantiate UIStoryboard instance with corresponding name and use it.
Btw, why are you calling tableView.removeFromSuperview() after cell selection?

Data not appearing in view controller

I have been trying everything I could but nothing seems to work. I am trying to pass firebase user information from one view controller to another, but it never appears.
It starts from a table view
The table view is populated with users that are being pulled from my firebase database. When the user selects a cell the following code gets executed:
I reference function "showProfileControllerForUser" as a way to transport the data of the cell that was selected
var mainProfile: userProfileNavigator?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user1 = self.users[indexPath.row]
self.mainProfile?.showProfileControllerForUser(user3: user1)
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "mainNavProfile") as! UINavigationController
self.present(vc, animated: true, completion: nil)
}
In "userProfileNavigator" is where the function that was used before is. The function passes the information that was collected and passes that to the final destination.
func showProfileControllerForUser(user3: User){
mainProfile?.user2 = user3
}
Finally, "mainProfile" is where I want the data to be passed to.
var user2: User?{
didSet{
navigationItem.title = user2?.name
}
I could imagine this being very confusing, I can provide anymore information or clarity if needed
attempted to setup table as such:
var mainProfile: mainProfile?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user1 = self.users[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "mainProfile")
self.mainProfile?.user2 = user1
self.present(vc, animated: true, completion: nil)
}
And the ViewController in which the information is being passed as such:
import UIKit
import Firebase
class mainProfile: UIViewController{
#IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
print("lets see if this is the new user", user2.name as Any)
}
var user2 = User()
}
but it still comes up as "nil"
If I understood your question right, then you have two ViewControllers. One which contain UITableView, and another is ShowProfileControllerForUser.
In ViewController which contain UITableView, code this:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user1 = self.users[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "mainNavProfile") as! UINavigationController
vc.user3 = user1
self.present(vc, animated: true, completion: nil)
}
Also, I am not sure if you're presenting the right ViewController.
In your ShowProfileControllerForUser.swift,
var user3 = User // declare a variable with proper type.
Now in your viewDidLoad():
override func viewDidLoad() {
super.viewDidLoad()
showProfileControllerForUser(user3)
}
Edit:
var mainProfile: mainProfile?
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let user1 = self.users[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let vc: UIViewController = storyboard.instantiateViewController(withIdentifier: "mainProfile")
//self.mainProfile?.user2 = user1 // Instead of this line
vc.user2 = user1 // use this line
self.present(vc, animated: true, completion: nil)
}
If you want to open controller with that passed value, you can set that value after instantiating it
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "mainNavProfile") as! UINavigationController
// this is what your showProfileControllerForUser doing
vc.user2 = user1
self.present(vc, animated: true, completion: nil)

Push different UIView for each row of UITableView

I'm trying to push a new view when I select a row.
let identifiers:[String] = ["vc1", "vc2", "vc3", "vc4", "vc5"]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let identifier = identifiers[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("\(identifier)") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
All the different views called with this tableView have their own class and Storyboard ID.
At the moment I get the error:
Could not cast value of type 'testApp.CalculatorViewController' to 'UINavigationController'
I think you are trying to cast UIViewController to UINavigationController.
Please try this. This may help you.
let identifiers:[String] = ["vc1", "vc2", "vc3", "vc4", "vc5"]
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let identifier = identifiers[indexPath.row]
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UIViewController = storyboard.instantiateViewControllerWithIdentifier("\(identifier)") as! UIViewController
self.navigationController?.pushViewController(vc, animated: true, completion: nil)
}

In Swift, how to grab object id from UITableView and delete object based on object id?

I don't know how to grab object id from UITableView after user select from it and in another view controller, I want to delete the object based on object id that I already fetch from previous one.
UITableView
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
/* grab object id here and pass to NSUserDefault*/
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
self.presentViewController(deleteViewController, animated:true, completion:nil)
}
DeleteViewController
/* Delete object happen here*/
You can set a property in DeleteViewController and pass the value to it when initializing the view controller.
class DeleteViewController {
var objectId: Int?
}
// Previous view controller
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Grab your object from the data source
let object = dataSource[indexPath.row]
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
// Pass the Id
deleteViewController.objectId = object.objectId
self.presentViewController(deleteViewController, animated:true, completion:nil)
}
I already found the answer.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let data: NSManagedObject = myList[indexPath.row] as NSManagedObject
let objectId = data.valueForKeyPath("objectId") as? String
NSUserDefaults.standardUserDefaults().setObject(objectId, forKey: "objectId")
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let deleteViewController = storyBoard.instantiateViewControllerWithIdentifier("accessView") as DeleteViewController
self.presentViewController(deleteViewController, animated:true, completion:nil)
}

Resources