Exc_Bad_Instruction When Passing Uicollectionview Cell Data to Different Viewcontroller

EXC_BAD_INSTRUCTION When Passing UICollectionView Cell Data to Different ViewController

The crash occurs because the outlet passLabel is not connected yet when prepare(for segue is called.

You have to declare a (temporary) variable in PollController and set the label in viewDidLoad

class PollController: UIViewController {

@IBOutlet weak var passLabel: UILabel!

var pass = ""

override func viewDidLoad() {
super.viewDidLoad()
passLabel.text = pass
}

...

In prepare(for segue set the variable rather than the text property of the label:

let pollViewController = segue.destination as! PollController
if let cell = sender as? PollCell {
pollViewController.pass = cell.pollQuestion.text
}
}

Note: It's not recommended to gather information from the view (cell). Get the index path and read the information from the model (data source array).

Passing Data from one view controller to another (Image)

This might work:

override func viewDidLoad() {
super.viewDidLoad()

imageView.image = getimage // change this in view did load method
}

How do I change the label in a view controller I want to load a tableviewcell is selected?

You are trying to reference a view (profileName) that has not already been instantiated.

You should declare a global variable in ProfileViewController like this

var expectedString = ""

and edit your prepare(for segue: ...) method like this

    guard let pvc = segue.destination as? ProfileViewController else { return }
let indexpath = tableView.indexPathForSelectedRow

if let unwrappedPath = indexpath {
tableView.deselectRow(at: unwrappedPath, animated: true)
pvc.expectedString = userList[unwrappedPath.row]
}

Then you can set your label text in the viewWillAppear method of the ProfileViewController:

override func viewWillAppear(_ animated: Bool)
{
profileName.text = expectedString
}

Unexpected EXC_BAD_INSTRUCTION

Notice what you did:

In didSelectRowAtIndexPath you got the selected row and deselected the row.

You then called prepareForSegue and asked the tableView for the selected row, but now, there isn't one. The function returns a nil

If you remove the 'deselectRowAtIndexPath' it should work.

More to the point, you should read the section on Optionals in the swift tour. You are appending an exclamation mark to self.tableView.indexPathForSelectedRow. That means it is potentially a nil value and you are absolutely certain it is not.

To quote Apple:

"Once you’re sure that the optional does contain a value, you can access its underlying value by adding an exclamation mark (!) to the end of the optional’s name. The exclamation mark effectively says, “I know that this optional definitely has a value; please use it.” This is known as forced unwrapping of the optional’s value"

Passing data back to previous view controller after selecting table view cell

Your delegate needs to be set. In your case you have to set it inside prepareForSegue method like

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "friendList"{
let friendViewController = (segue.destinationViewController as! friendListController)
friendViewController.mDelegate = self //Include this line
//rest of the code
}
}

How to pass data from one view controller to the other SWIFT

For instance, in your BoothsTableViewController create variable holding the currently selected Booth, which you assign in didSelectRowAtIndexPath. You can then pass this variable to the detail view controller in your prepareForSegue method.

Error while passing data between two ViewControllers

You should not have both triggered and manual segues happening at the same time.

Try this:

Delete your didSelectRowAt function (or comment it out), and replace prepare() with:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "showDetails"){
let secondViewController = segue.destination as? EventViewController
if let idx = tableView.indexPathForSelectedRow {
secondViewController?.data = posts[idx.row]
}
}
}


Related Topics



Leave a reply



Submit