Crashing when passing Var data as delegates - ios

I have been trying to pass data back to my "HostRideViewController" from my "ProfileViewController" but the data I try to pass back creates a fatal error when unwrapping it, but i dont understand why as the console print out before it shows the correct data
import UIKit
import Foundation
import Firebase
protocol RiderSelectionDelegate {
func selectedRideLeader(firstName:String,lastName:String,uid:String)
}
class ProfileViewController: UIViewController {
#IBOutlet weak var ProfilePictureImageView: UIImageView!
#IBOutlet weak var AddasLeaderBtn: UIButton!
#IBOutlet weak var FirstNameFeild: UILabel!
#IBOutlet weak var LastNameFeild: UILabel!
#IBOutlet weak var UserBioFeild: UILabel!
#IBOutlet weak var HiddenEmail: UILabel!
#IBOutlet weak var HiddenUID: UILabel!
var user: MyUser?
var selectedFirstName: String?
var selectedLastName: String?
var selectedUID: String?
var selectedEmail: String?
var selectionDelegate: RiderSelectionDelegate!
override func viewDidLoad() {
super.viewDidLoad()
/// make profile picture circular
ProfilePictureImageView.layer.cornerRadius = ProfilePictureImageView.frame.size.width/2
ProfilePictureImageView.clipsToBounds = true
/// load user data into view
FirstNameFeild?.text = user?.firstName
LastNameFeild?.text = user?.lastName
HiddenUID?.text = user?.uid
HiddenEmail?.text = user?.email
}
#IBAction func SelectedLeaderpressed(_ sender: Any) {
selectedFirstName = FirstNameFeild.text
selectedLastName = LastNameFeild.text
selectedUID = user?.uid
selectedEmail = user?.email
print(selectedUID!,selectedLastName!,selectedFirstName!)
/// where the error is happening
selectionDelegate.selectedRideLeader(firstName:selectedFirstName!, lastName:selectedLastName!, uid:selectedUID!)
dismiss(animated: true, completion: nil)
}
}
My console output and error from passing data
49s64wH2g7hjiMAKp0uvADkzP0k2 bloggs joe
Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: file
My intended use of the delegate is to set a label up with the user's name
extension HostRideViewController:RiderSelectionDelegate{
func selectedRideLeader(firstName: String, lastName: String, uid: String) {
OtherRideLeaderLbl.text = firstName
}
}
where am I going wrong as I'm trying to force unwrap the data which is there and present in the print but crashes when I'm trying to delegate it

It seems selectionDelegate is nil you need to set it when you present/push/segue to the destination vc

Related

How to send data from TextField from second view controller to first view controller and add this data to array swift iOS [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 3 years ago.
I want to send data from TextField from second view controller to first view controller and add this data to an array
I have a struct which I will save to array:
struct ContactsModel {
var name : String
var surname : String
var phoneNumber : String
}
first VC:
class FirstViewController: UIViewController {
var contacts : [ContactsModel] = []
}
second VC:
class SecondViewController: UIViewController {
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var surnameTextField: UITextField!
#IBOutlet weak var phoneNumberTextField: UITextField!
#IBAction func saveAndClose(_ sender: UIButton) {
// here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
}
}
You can accomplish this using a delegate:
struct ContactsModel {
var name : String
var surname : String
var phoneNumber : String
}
protocol SecondViewControllerDelegate: class {
func savedAndClosed(with model: ContactsModel)
}
class FirstViewController: UIViewController {
var contacts : [ContactsModel] = []
// Whereever you create and present your instance of SecondViewController make it conform to the delegate
func showSecondVC() {
let secondVC = SecondViewController()
secondVC.delegate = self
present(secondVC, animated: true, completion: nil)
}
}
extension FirstViewController: SecondViewControllerDelegate {
func savedAndClosed(with model: ContactsModel) {
contacts.append(model)
}
}
class SecondViewController: UIViewController {
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var surnameTextField: UITextField!
#IBOutlet weak var phoneNumberTextField: UITextField!
weak var delegate: SecondViewControllerDelegate?
#IBAction func saveAndClose(_ sender: UIButton) {
// here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
guard let name = nameTextField.text, let surname = surnameTextField.text, let phoneNumber = phoneNumberTextField.text else { return }
let new = ContactsModel(name: name, surname: surname, phoneNumber: phoneNumber)
delegate?.savedAndClosed(with: new)
}
}
First be sure to make var contacts in FirstViewController static:
class FirstViewController: UIViewController {
static var contacts : [ContactsModel] = []
}
Then in SecondViewController you can edit variable "contacts" like this:
class SecondViewController: UIViewController {
#IBOutlet weak var nameTextField: UITextField!
#IBOutlet weak var surnameTextField: UITextField!
#IBOutlet weak var phoneNumberTextField: UITextField!
#IBAction func saveAndClose(_ sender: UIButton) {
// here i want to send this objects (nameTextField, surnameTextField, phoneNumberTextField) in array in first VC when i press this button
FirstViewController.contacts.append(ContactsModel(name: nameTextField.text ?? "defaultName", surname: surnameTextField.text ?? "defaultSurname", phoneNumber: phoneNumberTextField.text ?? "defaultPhone"))
}
}
You need to define default values so even if text from field would be nil your app won't crush, in example we set default values here:
name: nameTextField.text ?? "defaultName"

Swift believes non-nil value is actually nil

I have two text boxes, passwordTextField and emailTextField. Values are entered into each. When I hit print, the values print in the console.
However, I am receiving the following error when using the next function to populate a text box on the next view controller.
Note
This was working not a few hours ago and I have changed nothing, so I am unsure how or why this now does not work.
#IBOutlet weak var passwordTextField: UITextField!
#IBOutlet weak var emailTextField: UITextField!
#IBOutlet weak var registerButton: UIButton!
#IBAction func registerButtonPressed(_ sender: Any) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "ABC") as! ABCViewController
self.present(next, animated: true,completion: nil)
let nameText = self.emailTextField.text!
let passwordText = self.passwordTextField.text!
print(nameText) // prints email
print(passwordText) // prints password
next.username.text = nameText // prints error: Unexpectedly found nil while implicitly unwrapping an Optional value
next.password.text = passwordText
}
Unexpectedly found nil while implicitly unwrapping an Optional value.
But non-nil values are displaying when printing out.
The code is unreliable because it's not guaranteed that the view in the second controller is loaded right after the present line.
All outlets are nil until the view is available.
Declare temporary properties in ABCViewController and populate the text fields in viewDidLoad
class ABCViewController : UIViewController {
#IBOutlet weak var username: UITextField!
#IBOutlet weak var password: UITextField!
var tempName = ""
var tempPassword = ""
// .....
func viewDidLoad() {
super.viewDidLoad()
username.text = self.tempName
password.text = self.tempPassword
}
}
In registerButtonPressed assign the values to the temporary properties and present the controller, nothing else.
#IBAction func registerButtonPressed(_ sender: Any) {
let next = self.storyboard?.instantiateViewController(withIdentifier: "ABC") as! ABCViewController
next.tempName = self.emailTextField.text!
next.tempPassword = self.passwordTextField.text!
self.present(next, animated: true,completion: nil)
}

Cloudkit Database New Record does not save

Please keep in mind that this is the first time I have used this website.
I am a complete beginner in coding who, somehow, ended up responsible for creating an app for a high-school level competition. My knowledge is extremely limited and I have found it difficult to find clear tutorials on how to use a CloudKit database.
My task for this specific portion of my application was to make a "Create a Profile" page, where users can input information that will then be stored and displayed in other ViewControllers of the application.
Most of the code related to the database was given by a teacher (taken out from the project of another group), yet its use was not explained. So, I simply tried to copy it and adapt it to the best of my abilities.
However, when running the simulator and creating a test profile, nothing happens once the save button is clicked.
My current code is as follows:
import UIKit
import CloudKit
class ProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
let database = CKContainer.defaultContainer().publicCloudDatabase
#IBOutlet weak var profileType: UISegmentedControl!
#IBOutlet weak var descriptionTextfield: UITextField!
#IBOutlet weak var nameTextfield: UITextField!
#IBOutlet weak var addressTextfield: UITextField!
#IBOutlet weak var emailTextfield: UITextField!
#IBOutlet weak var passwordTextfield: UITextField!
#IBOutlet weak var phoneTextfield: UITextField!
#IBOutlet weak var saveButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated
}
#IBAction func saveProfile(sender: AnyObject) {
let newrecord = CKRecord(recordType: "PotLuck")
newrecord.setObject(nameTextfield.text as CKRecordValue?, forKey: "CompanyName")
newrecord.setObject(addressTextfield.text as CKRecordValue?, forKey: "CompanyLocation")
newrecord.setObject(emailTextfield.text as CKRecordValue?, forKey: "CompanyEmail")
newrecord.setObject(passwordTextfield.text as CKRecordValue?, forKey: "CompanyPassword")
newrecord.setObject(phoneTextfield.text as CKRecordValue?, forKey: "CompanyPhone")
switch profileType.selectedSegmentIndex
{
case 0:
newrecord.setObject("Business" as CKRecordValue?, forKey: "CompanyType")
case 1:
newrecord.setObject("Charity" as CKRecordValue?, forKey: "CompanyType")
default:
break;
}
database.saveRecord(newrecord, completionHandler: {newrecord, error in
if error != nil {
print("An error occured")
} else {
let newview = URLViewController(nibName: "URLViewControler", bundle: nil)
self.presentViewController(newview, animated: true, completion: nil)
}
}
)
}
Would anyone be able to point out whatever is missing or incorrect?

Unresolved identifier using segue when passing data

In my app I am using segue to pass data between two viewcontrollers and that should be easy enough, but for som reason I can`t see there I keep getting "Unresolved Identifier"
Her are some of the code that has to do with that function.
from ViewController 1
import UIKit
import CoreData
class ViewController: UIViewController, UITextFieldDelegate
{
#IBOutlet var panelWidthTextField: UITextField!
#IBOutlet var panelHightTextField: UITextField!
#IBOutlet var panelsWideTextField: UITextField!
#IBOutlet var panelsHightTextField: UITextField!
#IBOutlet var panelPitchTextField: UITextField!
#IBOutlet var calculateButton: UIButton!
#IBOutlet var resultWithLabel: UILabel!
#IBOutlet var resultHightLabel: UILabel!
#IBOutlet var fillAllFieldsLabel: UILabel!
var pawidth:String!
var pahight:String!
var papitch:String!
override func viewDidLoad()
{
super.viewDidLoad()
panelWidthTextField.text = pawidth
panelHightTextField.text = pahight
panelPitchTextField.text = pap itch
From Second ViewController
import UIKit
import CoreData
class DataBase: UIViewController, UITextFieldDelegate
{
#IBOutlet var makerTextField: UITextField!
#IBOutlet var modelTextField: UITextField!
#IBOutlet var stPanelWidthTextField: UITextField!
#IBOutlet var stPanelHightTextField: UITextField!
#IBOutlet var stPitchTextField: UITextField!
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
// Removes keyboard when touch outside edit field.
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
view.endEditing(true)
super.touchesBegan(touches, withEvent: event)
}
#IBAction func saveButton(sender: UIButton)
{
let ed = NSEntityDescription.entityForName("Ledinfo", inManagedObjectContext: moc)
let model = Ledinfo(entity:ed!, insertIntoManagedObjectContext:moc)
model.manufactor = makerTextField.text
model.model = modelTextField.text
model.panelwidth = stPanelWidthTextField.text
model.panelhight = stPanelHightTextField.text
model.pitch = stPitchTextField.text
do {
try moc.save()
makerTextField.text = ""
modelTextField.text = ""
stPanelWidthTextField.text = ""
stPanelHightTextField.text = ""
stPitchTextField.text = ""
Alert.show("Succsess", message: "Your Record Is Saved", vc: self)
}
catch _ as NSError
{
Alert.show("Failed", message: "Something Went Wrong", vc: self)
}
}
#IBAction func searchButton(sender: UIButton)
{
let ed = NSEntityDescription.entityForName("Ledinfo", inManagedObjectContext: moc)
let req = NSFetchRequest()
req.entity = ed
let cond = NSPredicate(format: "manufactor = %#", makerTextField.text!)
req.predicate = cond
do {
let result = try moc.executeFetchRequest(req)
if result.count > 0
{
let model = result[0] as! Ledinfo
makerTextField.text = model.manufactor
modelTextField.text = model.model
stPanelWidthTextField.text = model.panelwidth
stPanelHightTextField.text = model.panelhight
stPitchTextField.text = model.pitch
} else
{
Alert.show("Failed", message: "No Record Is Found", vc: self)
}
} catch _ as NSError!
{
Alert.show("Failed", message: "No Record Is Found" , vc: self)
}
}
#IBAction func transfereButton(sender: UIButton) {
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "transfereButton") {
let svc = segue.destinationViewController as! ViewController
svc.pawidth = stPanelWidthTextField.text
svc.pahight = stPanelHightTextField.text
svc.papitch = stPitchTextField.text
}
}
}
It can not find panelWidthTextField.text, panelHightTextField.text and panelPitchTextField.text as identifier.
I have check spelling and just can`t seem to be able to find what is missing.
Any help is appreciated
"Segue" means, that in "prepareForSegue" method you set the property of ViewController to some data in your DataBase controller. In your example, this can be done like this:
svc.pawidth = someDataFromDataBaseWhichYouWantToPassToSecondVC
svc.pahight = someDataFromDataBaseWhichYouWantToPassToSecondVC
svc.papitch = someDataFromDataBaseWhichYouWantToPassToSecondVC
And then, you can manipulate this data from your ViewController class.
You mistake that you are not passing the data from one VC to another, instead of that you are trying to set the property of 1stVC to another property of 1stVC, and there is no segue needed.
This has nothing to do with segues. do you have 3 text fields in your DataBase class with names panelWidthTextField, panelHightTextField and panelPithcTextField? It's complaining about not being able to find those variables.
You should call the performSegueWithIdentifier("transfereButton", sender: nil) inside your transfereButton IBOutlet action to actually make the prepareForSegue to run.

Uncaught Exception Saving String NSUserDefaults [Swift]

I am trying to save an inputted string from a text field and load that saved value using NSUserDefaults. When I run my code, the app crashes when I hit the "saveScore" button. My error message is "terminating with uncaught exception," but I can't find what's actually wrong.
class ScoreViewController: UIViewController {
#IBOutlet weak var loadDataButton: UIButton!
#IBOutlet weak var mathScore: UITextField!
#IBOutlet weak var testMath: UILabel!
#IBOutlet weak var saveScore: UIButton!
#IBOutlet weak var displayDataLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func saveDataClicked(sender: AnyObject) {
saveData()
}
func saveData() {
let defaults = NSUserDefaults.standardUserDefaults()
let latestScore = mathScore.text
defaults.setObject(latestScore, forKey: "12/1")
defaults.synchronize()
}
#IBAction func loadDataClicked(sender: AnyObject) {
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults()
if let firstNameIsNotNill = defaults.objectForKey("12/1") as? String {
self.displayDataLabel.text = defaults.objectForKey("12/1") as String
}
}
Restarting XCode fixed the problem!

Resources