Could not cast value of type 'Work.Login' (0x10c5edd20) to 'Work.DataBaseConn' - swift

I have a problem with my second viewController when trying to send data with segue. The code of Login is:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondView = segue.destination as! DataBaseConn
secondView.user=TextUsername.text!
secondView.pass=Pass.text!
}
override func viewDidLoad() {
super.viewDidLoad()
self.performSegue(withIdentifier: "sendData", sender: self)
}
I get the error at this line:
let secondView = segue.destination as! DataBaseConn
Both views have the custom class set.

Related

Pass data to variable in swift

I want to assign button tag to variable. Then I trying send this data to SecondViewController and assign it to var.
#IBAction func goToProducts(_ sender: UIButton) {
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
categoryNumber = sender.tag }
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = categoryNumber
}
}
At ProductsViewController I create var but there isn't this data. I print this variable and I get this:
categoryNumber: 373
categoryNumb: Optional(24)
Can you tell me haw can I do this?
You need to assign it first before performSegue
categoryNumber = sender.tag
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: self)
OR use sender
performSegue(withIdentifier: TextStrings.ProfileAluVC.goToProducts, sender: sender.tag)
Pass data to ProductsViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToProducts" {
let destinationController = segue.destination as! ProductsViewController
destinationController.categoryNumb = sender as! Int
}
}

Passing variable in swift with segue

I'm using segue to open a new window in a xcode app.
The segue is correct, when I tap on the button the new window is shown.
I want to pass a string variable from FirstViewController to DetailViewController but I can't set the variable in the DetailViewController.
This is the code in FirstViewController:
if control == view.rightCalloutAccessoryView {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! DetailViewController
controller.Name = "test"
}
performSegue(withIdentifier: "showdetail", sender: self)
}
And this the DetailViewController code
class DetailViewController: UIViewController, WKNavigationDelegate {
var Name: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(Name)
}
}
Where am I doing wrong?
The method prepare(for must be on the top level of the class.
And please name variables according to the guidelines with starting lowercase letter
func someMethod()
{
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "showdetail", sender: self)
}
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! DetailViewController
controller.name = "test"
}
}

How to perform prepareForSegueFunction on ViewController load?

I am trying to perform a segue when the ViewController loads so it can trigger the prepareForSegue function.
I have tried using performselector but it does not work. Any suggestions?
func doSegue() {
performSegue(withIdentifier: "doSegue", sender: nil)
}
perform(Selector(("doSegue")), with: nil, afterDelay: 1)
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let guest = segue.destination as! WarningTableViewController
//code
}
It says it cannot find a selector named doSegue.
The code you posted is confusing.
Why there's a perform(Selector... there?
Here's a working example:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
performSegue(withIdentifier: "doSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "doSegue" {
print("It works")
// Or call another function, e.g. "doSomethingElse()"
}
}
}
Also double-check the segue declaration in Interface Builder:

Attempting to pass data from one viewcontroller to another using segues

I have two viewcontrollers setup and set the segue connections as well. I'm attempting to pass data from one of the VC's to the other. Using the below code and using the func override works. What i want to do is only have the override func prepare fire off when a button is pressed and when i paste the override func code into a button action, it doesnt work - the label on the first VC doesnt update - it stays to the default value i set.
First ViewController code:
var testLabel1:String = "default"
#IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
testLabel?.text = testLabel1
// Do any additional setup after loading the view.
}
Second ViewController code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is ViewController
{
let vc = segue.destination as? ViewController
vc?.testLabel1 = "Success"
}
}
Button action that ive tried
#IBAction func actionBtn(_ sender: Any) {
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is ViewController
{
let vc = segue.destination as? ViewController
vc?.testLabel1 = "Success"
}
}
}
You need to trigger it with performSegue
class FirstVc:UIViewController {
#IBOutlet weak var lbl:UILabel!
#IBAction func goToSecond(_ sender: Any) {
self.performSegue(withIdentifier:"YourSegueName",sender:nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueName" {
let vc = segue.destination as! SecondVC
vc.testLabel1 = "Success"
}
}
func setData(_ str:String){
self.lbl.text = str
}
}
class SecondVC:UIViewController {
var testLabel1 = ""
weak var delegate:FirstVc?
func goBack() {
self.delegate?.setData("FromSecond")
self.dismiss(animated: true, completion: nil)
}
}

Pass image from one view controller to another controller gives nil

I am trying to send an image to the preview controller as shown below. My issue is that in the preview controller newImage is nil
Below is the code:
FirstViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("In here")
if segue.identifier == "showImage" {
let secondViewController = self.storyboard!.instantiateViewController(withIdentifier: "showImage") as! showImageController
secondViewController.newImage = UIImage(named: imageArray[0])
}
}
PreviewController
#IBOutlet weak var imageHolder: UIImageView!
var newImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
imageHolder.image = newImage
// Do any additional setup after loading the view.
}
You should use segue.destination not create a new one , like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("In here")
if segue.identifier == "showImage" {
let next = segue.destination as! showImageController
next.newImage = UIImage(named: imageArray[0])
}
}
instantiateViewController calls viewDidLoad() so you will not have setup your image when it gets called. Move the code to viewWillAppear() and it will work