passing CoreData from UITableViewCell to viewcontroller - swift

I'm trying to create the feature where when a user clicks on a specific cell in my UITableView, the Project will segue to a new ViewController and show all the information that has been saved in the CoreData. The Problem is that when I touch on a cell I get an error of
Unexpected nil while unwrapping optional value
Here is my code as it is right now within the ViewController that has the TableView
class ContactViewController: UIViewController, UITableViewDataSource, NSFetchedResultsControllerDelegate, UITableViewDelegate {
var selectName:String?
var selectPhone:String?
var selectComment:String?
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
selectName = contact[indexPath.row].name
selectPhone = contact[indexPath.row].phone
selectComment = contact[indexPath.row].comments
performSegue(withIdentifier: "MySegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegue"{
if let destVC = segue.destination as? IndiViewController {
destVC.NameLabel = selectName
destVC.PhoneLabel = selectPhone
destVC.CommentLabel = selectComment
}
}
}
This is my code in IndiViewController (the VC in which I want the user to view the contact)
class IndiViewController: UIViewController {
#IBOutlet var NameLabel: UILabel!
#IBOutlet var PhoneLabel: UILabel!
#IBOutlet var CommentsLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
I've tried a few other methods but all still delivered the same error
I troubleshooted a bit to see which variable truly was causing the nil by doing this:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegue"{
if let destVC = segue.destination as? IndiViewController {
destVC.NameLabel = "selectName" //I thought if perhaps the nil was the var selectName this would at least let me know
}
}
However even through this, the app crashes and gives the same error. So I think the issue is with the Labels in the IndiViewController.
So I tried creating an empty String and assigning it to NameLabel like this:
class IndiViewController: UIViewController {
#IBOutlet var NameLabel: UILabel!
var name = ""
override func viewDidLoad() {
super.viewDidLoad()
NameLabel.text = name
}
}
but still no luck.
What am I doing wrong?

Yet it can be an IBOutlet error, even for typo.
delete #IBOutlet var NameLabel: UILabel!
remove the link in the StoryBoard "referencing outlets" tab
click-drag a new outlet for the UILabel and do not name it with a capital letter
// #IBOutlet var NameLabel: UILabel!
#IBOutlet var nameLabel: UILabel!
Edit
Also try to pass your informations in two times :
create a new var in your destinationViewController like :
class IndiViewController: UIViewController {
#IBOutlet var nameLabel: UILabel!
var myText: String = ""
override func viewDidLoad() {
super.viewDidLoad()
nameLabel.text = myText // assign the var to the labels text
}
}
In your prepareForSegue method assign myText and not the nameLabel.text
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "MySegue"{
if let destVC = segue.destination as? IndiViewController {
destVC.myText = "selectName"
}
}
}
my guess is that you cant acces IBOutlets in prepare(for segue:) method. The destination ViewController is not built yet

Related

#IBOutlet property cannot have non-object type

I am trying to pass a struct between view controllers, but I get the compiler error "#IBOutlet property cannot have non-object type". I have tried to add #objc but still get the error. How can I pass this data between view controllers? Why do I get this error and how do I correct it? Thanks.
import UIKit
struct DocObject: Codable {
let filename: String
let doclink: Int
}
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
// var nameText = ""
var obj = DocObject(filename: "filename", doclink: 123)
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func done(_ sender: Any) {
print ("In VC1 nameText ", obj)
performSegue(withIdentifier: "name", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let vc = segue.destination as! VCTwo2
vc.finalObj = obj
}
}
import UIKit
class VCTwo2: UIViewController {
#IBOutlet var finalObj: DocObject!
// var finalName = 0
override func viewDidLoad() {
super.viewDidLoad()
print ("In VC2 ", finalObj!)
}
}
You only define a property with #IBoutlet if the property is loaded from a xib or storyboard. Since you are passing the data structure from another view controller just define finalObj as a regular var.
class VCTwo2: UIViewController {
var finalObj: DocObject!
override func viewDidLoad() {
super.viewDidLoad()
}
}

How to pass data between view controllers - not working

Im trying to pass an player score from VC1 to a view that displays the current scores of all players(4). the display view is on a separate view controller than where the player(s) score is defined and stored.
I have done the prepare(segue) and im able to pass other variables to the display VC(ThirdViewController). but when I try to assign the player score to the uilabel.text It tells me that I have unwrapped a nil value
I have even tried to just set the label text to a static string and still get the nil error.
class ViewController: UIViewController {
var name = String()
var player1Score = 1
var player2Score = 2
var player3Score = 3
var player4Score = 4
//MARK: ********* IBOutlets **********
#IBOutlet weak var playerSegmentOutlet: UISegmentedControl!
#IBOutlet weak var diceSegmentOutlet: UISegmentedControl!
#IBOutlet weak var targetScoreSliderOutlet: UISlider!
#IBOutlet weak var matchTargetSwitchOutlet: UISwitch!
#IBOutlet weak var targetScoreLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let VC = segue.destination as? SecondViewController {
VC.player1CurrentScore = player1Score
VC.player2CurrentScore = player2Score
VC.player3CurrentScore = player3Score
VC.player4CurrentScore = player4Score
}
}
Second view Controller
class SecondViewController: UIViewController {
#IBOutlet weak var CurrentRoundScoreLabel: UILabel!
#IBOutlet weak var CurrentPlayerScoreLabel: UILabel!
var player1CurrentScore = 1
var player2CurrentScore = 1
var player3CurrentScore = 1
var player4CurrentScore = 1
override func viewDidLoad() {
super.viewDidLoad()
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let VC = segue.destination as? ThirdViewController {
VC.player1ScoreLabel.text = String(player1CurrentScore)
VC.player2ScoreLabel.text = String(player2CurrentScore)
VC.player3ScoreLabel.text = String(player3CurrentScore)
VC.player4ScoreLabel.text = String(player4CurrentScore)
}
}
Third View Controller
class ThirdViewController: UIViewController {
#IBOutlet var player1ScoreLabel: UILabel!
#IBOutlet var player2ScoreLabel: UILabel!
#IBOutlet var player3ScoreLabel: UILabel!
#IBOutlet var player4ScoreLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
no matter what I try to do with with UILabel.text it shows up as nil
I'm totally frustrated and I'm sure I am missing something simple because of my frustration, please someone help me.
This is an inefficient way to do this as you are passing data through 3 different objects. However, going on with this methodology, the problem is the label's are not created yet inside
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let VC = segue.destination as? ThirdViewController {
VC.player1ScoreLabel.text = String(player1CurrentScore)
VC.player2ScoreLabel.text = String(player2CurrentScore)
VC.player3ScoreLabel.text = String(player3CurrentScore)
VC.player4ScoreLabel.text = String(player4CurrentScore)
}
}
See, the label isn't created yet. So, you are setting text on aUILabel that isn't initialized. Therefore, you need to create variables for the labels inside ThirdViewController.
Third View Controller
class ThirdViewController: UIViewController {
#IBOutlet var player1ScoreLabel: UILabel!
#IBOutlet var player2ScoreLabel: UILabel!
#IBOutlet var player3ScoreLabel: UILabel!
#IBOutlet var player4ScoreLabel: UILabel!
var score0:Int!
var score1:Int!
var score2:Int!
var score3:Int!
override func viewDidLoad() {
super.viewDidLoad()
self.player1ScoreLabel.text = String(score0)
self.player2ScoreLabel.text = String(score1)
self.player3ScoreLabel.text = String(score2)
self.player4ScoreLabel.text = String(score3)
}
}
and change the segue in SecondViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let VC = segue.destination as? ThirdViewController {
VC.score0 = player1CurrentScore
VC.score1 = player2CurrentScore
VC.score2 = player3CurrentScore
VC.score3 = player4CurrentScore
}
}
Another Way:
Let's create a singleton called Game. Within this (assuming you only have 4 players) we can create 4 players that will never change. This allows us to create instances of players in one location and call upon them as necessary.
NOTE: A singleton can be misused EASILY.
https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift
https://cocoacasts.com/are-singletons-bad/
class Game {
static var score0:Int = 0
static var score1:Int = 0
static var score2:Int = 0
static var score2:Int = 0
}
Then, anywhere in your code you can access Game.score0, Game.score1.
CAUTION:
I would caution you to very carefully use singletons. You don't want everything with public access. You need to determine if this is good for you or not. Cheers.

Text field is permanently in the second view controller upon segue

I have been working on an app that allows multiple text fields from the first view controller pass over to the second view controller upon pressing a button. However, the text fields are permanently in the second view controller when I only want them to be if the button is pressed. Here is the code for the first view controller! Any help is greatly appreciated.
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textField1: UITextField!
#IBAction func buttonTwo(_ sender: Any) {
if textField1.text != "" {
performSegue(withIdentifier: "segue", sender: self)
}
}
#IBAction func buttonOne(_ sender: Any) {
if textField.text != "" {
performSegue(withIdentifier: "segue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var secondController = segue.destination as! SecondViewController
secondController.myString1 = textField1.text!
secondController.myString = textField.text!
}
}
Here is the code in the second view controller:
class SecondViewController: UIViewController {
#IBOutlet weak var label: UILabel!
#IBOutlet weak var label1: UILabel!
var myString = String()
var myString1 = String()
override func viewDidLoad() {
super.viewDidLoad()
label.text = myString
label1.text = myString1
// Do any additional setup after loading the view.
}
}
Image of storyboard
This happens because, prepare for segue will be called every time you perform some segue action.
You should manage to have a bool variable that helps you track, if any button is clicked or not, if the segue is performed from the click of the button, then only you will have to set the text while preparing for segue.
here is your updated viewController
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
#IBOutlet weak var textField1: UITextField!
var isButtonClicked: Bool = false
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
/*reset isButtonClicked to false, when you back from second viewController */
isButtonClicked = false
}
#IBAction func buttonTwo(_ sender: Any) {
if textField1.text != "" {
isButtonClicked = true
performSegue(withIdentifier: "segue", sender: self)
}
}
#IBAction func buttonOne(_ sender: Any) {
if textField.text != "" {
isButtonClicked = true
performSegue(withIdentifier: "segue", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if isButtonClicked {
var secondController = segue.destination as! SecondViewController
secondController.myString1 = textField1.text!
secondController.myString = textField.text!
}
}
}
Try and share your results.

Passing a variable from a customCell.xib's button to another UIViewController

My custom cell has a button that when clicked, the user can be taken to another ViewControler. That button has a titleLabel with the String of a user id. What I want to do is take the user to a new UIViewController, passing that clicked buttons's titleLabel (user id) to a variable on the new View Controller. That way, I can use that variable (user id) get further information from firebase and display it on the UI View controller.
on the .xib of the custom cell, I print the UID to make sure each button prints with the correspondent ID, which it does. I can't figure out a way to pass that ID to a new ViewController.
I tried researching online and I found out you can't do prepare(for segue) or performsegue(WithIdentifier) on a customCell.xib.
I tried doing delegates and then protocols but still couldn't get it to work. I am new with Swift. Any help would be great, thank you!
This is the customCell.Xib's Swift file:
class CustomCellTableViewCell: UITableViewCell {
#IBOutlet weak var postImage: UIImageView!
#IBOutlet weak var nameLbl: UILabel!
#IBOutlet weak var descriptionLbl: UITextView!
#IBOutlet weak var profileImageBtn: UIButton!
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var view: UIView!
var btnSelected : Bool = false
var vcInstance: ProfilesTableVC?
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
view.clipsToBounds = true
view.layer.cornerRadius = view.frame.size.width / 2
profileImage.layer.cornerRadius = profileImage.frame.size.width / 2
descriptionLbl.alpha = 0.7
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
#IBAction func profileBtnPressed(_ sender: Any) {
let passValue = UserProfileVC()
let profileTvC = ProfilesTableVC()
print (profileImageBtn.titleLabel!.text!)
var id = (profileImageBtn.titleLabel!.text!)
profileTvC.didSelectProfileBtn(senderID: id)
}
This is the tableViewController, where I everything gets loaded (not where I want to pass the value). I tried passing the value here and then do a prepareForSegue to pass the value to the new UIViewController but the value becomes nil after the segue happens. I am just including code where the .xib call the function from the table view.
class ProfilesTableVC: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate{
let cell = CustomCellTableViewCell()
func didSelectProfileBtn(senderID: String) {
senderIDArray.append(senderID)
var index = senderIDArray.count - 1
selectedCellUserID = senderIDArray[index]
performSegue(withIdentifier: "showSendersProfile", sender: Any?.self)
}
This is the UIViewController where I want to pass the variable and display further information from Firebase using that ID
import UIKit
import Firebase
class UserProfileVC: UIViewController {
let customCell = CustomCellTableViewCell()
let profileTvC = ProfilesTableVC()
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var nameLbl: UILabel!
var delegate : Any?
var getName = String()
var getsenderID = String()
let userDataRef = Database.database().reference().child("Users")
override func viewDidLoad() {
super.viewDidLoad()
print("ID is: \(profileTvC.selectedCellUserID)")
let sender = getsenderID
print(sender)
}
}
If you don't want to use protocols, you can addTarget to the button in the tableView - cellForRowAt method, also in that method you set the tag to the row index value.
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let profileCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! MoviewReviewCell
reviewCell.profileImageBtn.tag = indexPath.row
reviewCell.profileImageBtn.addTarget(self, action: #selector(tappedOnXibCellButton), for: .touchUpInside)
return reviewCell
}
#objc func tappedOnXibCellButton(sender: UIButton) {
print(sender.tag)
performSegue(withIdentifier: reviewListToDetails, sender: sender.tag)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let identifier = segue.identifier
if identifier == "segueName" {
let destViewController = segue.destination as! DestinationClassViewController
let selectedIndex = sender as! Int
let profileId = profilesList[selectedIndex].id
destViewController.profileId = profileId
}
}
You seem to create a NEW ProfilesTableVC, and try to perform the segue on. The newly created one is not on your view stack, and not from your storyboard.
You could add this while returned cellForRowAt (at the ProfilesTableVC of course):
cell.vcInstance = self
Then in the button click
#IBAction func profileBtnPressed(_ sender: Any) {
print (profileImageBtn.titleLabel!.text!)
var id = (profileImageBtn.titleLabel!.text!)
vcInstance?.didSelectProfileBtn(senderID: id)
}
You can do it with protocols/delegates. I think you tried but there is something wrong with your trial.
Define a callback delegate:
protocol CustomCellDelegate: AnyObject {
func customCell(didSelectButton name: String)
}
You will notice extending AnyObject. This is to allow weak references, try to read about Swift ARC
Then, in your cell:
weak var delegate: CustomCellDelegate?
Then in the profileBtnPressed(_ sender: Any)
#IBAction func profileBtnPressed(_ sender: Any) {
var id = (profileImageBtn.titleLabel!.text!)
delegate?.customCell(didSelectButton: id)
}
When dequeing the cell:
(cell as? CustomCellTableViewCell)?.delegate = self

How can I pass data from a parent view controller to an embedded view controller in Swift?

I have a view controller embedded in another VC.
I would like to get the value of a variable from the main VC inside the embedded one. Specifically, I would like to change the text of label2 based on the value of label1.
I tried with "prepareForSegue", but it seems it's not triggered for embedded view controllers. I tried to isolate the problem in a test project:
Code for main VC:
class MyViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "Hello"
}
}
Code for embedded VC:
class EmbeddedVC: UIViewController {
#IBOutlet weak var label2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
Thanks for your help :)
A way to achiŠµve this is to get the child view controller instance in the parent's viewDidLoad. It appears that the parent's viewDidLoad: gets called after the child's viewDidLoad:, which means the label is already created in the child's view.
override func viewDidLoad() {
super.viewDidLoad()
if let childVC = self.childViewControllers.first as? ChildVC {
childVC.someLabel.text = "I'm here. Aye-aye."
}
}
First of all you can't set directly EmbeddedVC's lable2.text In prepareForSegue
because call sequence following below
MainVC's prepareForSeque this time EmbeddedVC's label2 is nil
EmbeddedVC's viewDidLoad called then label2 loaded
MainVC's viewDidLoad called then label1 loaded
so if you assign MainVC's label1.text to EmbeddedVC's label2.text in prepareForSeque
both label1 and label2 are nil so did not work
There are two way to solve this question
First Solution
MainViewController has EmbeddedVC and when MainVC's viewDidLoad called, assign label1.text to embeddedVC.label2.text
class MyViewController: UIViewController {
#IBOutlet weak var label1: UILabel!
var embeddedVC: EmbeddedViewController? = nil
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "Hello"
embeddedVC?.label2.text = label1.text
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let embeddedVC = segue.destination as? EmbeddedViewController {
self.embeddedVC = embeddedVC
}
}
}
class EmbeddedViewController: UIViewController {
#IBOutlet weak var label2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
Second Solution, use protocol and get MainVC's label text when viewWillAppear or viewDidAppear (later viewDidLoad called)
protocol EmbeddedVCDelegate: class {
func labelText() -> String?
}
class MyViewController: UIViewController, EmbeddedVCDelegate {
#IBOutlet weak var label1: UILabel!
// MARK: EmbeddedVCDelegate
func labelText() -> String? {
return label1.text
}
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "Hello"
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let embeddedVC = segue.destination as? EmbeddedViewController {
embeddedVC.delegate = self
}
}
}
class EmbeddedViewController: UIViewController {
#IBOutlet weak var label2: UILabel!
weak var delegate: EmbeddedVCDelegate? = nil
override func viewDidLoad() {
super.viewDidLoad()
}
override viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
label2.text = delegate?.labelText()
}
}
You should try to use prepareForSegue like this:
if segue.identifier == "identifier" {
guard let destinationViewController = segue.destination as? VC2 else { return }
destinationViewController.label2.text = mytext
}
Where the segue identifier you assign in storyboard