apple swift slideoutMenu indexPath - ios

I am creating a slide menu for ios but I'm having problems
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let DestVC = segue.destinationViewController as ViewController
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
DestVC.varView = indexPath.row
}
why doesn't this code work?

You need to make optional cast cause segue.destinationViewController is UIViewController that can't be always casted to its subclass
Try this
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let DestVC = segue.destinationViewController as? ViewController else { return }
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow()!
DestVC.varView = indexPath.row
}

Related

Pass a variable between controllers - Swift4

I'm trying to pass 1 variable to another view controller and then another view controller.
var selectedPlace = Place {
name = Daniel Webster Highway;
country = United States;
lat = 42.72073329999999;
lon = -71.44301460000001;
}
When I select on a cell, I have access to a variable selectedPlace
I want to pass it on to my PlaceDetailVC and also MapVC.
For some reasons, I can't make that happen.
PlacesVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! PlaceDetailVC
if let indexPath = tableView.indexPathForSelectedRow {
destinationVC.selectedPlace = (places?[indexPath.row])!
destinationVC.selectedTrip = selectedTrip!
}
}
PlaceDetailVC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMap" {
let destinationVC = segue.destination as! MapVC
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}
}
}
I also try
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! MapVC
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}
}
MapVC
print(selectedPlace,"<<<<<<")
Result
I kept getting
Any hints on what I did wrong ?
No need to check indexPath if the segue "toMap" is called by the nav button
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toMap" {
let destinationVC = segue.destination as! MapVC
destinationVC.selectedPlace = selectedPlace
/*
if let indexPath = placesTable.indexPathForSelectedRow {
destinationVC.selectedPlace = selectedPlace
}*/
}
}

UITableView Not Using Cell as Sender

I'm trying to segue and pass data (from UITableView to a UIViewController). My code is
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let cell = sender as! UITableViewCell
let selectedRow = AchievementsTable.indexPathForCell(cell)!.row
destination!.viaSegue = achievements[selectedRow]
print(selectedRow)
}
}
but the line let cell = sender as! UITableViewCell is throwing
Could not cast value of type 'ProgramName.AchievementsViewController' to 'UITableViewCell'
I have also tried
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let selectedRow = AchievementsTable.indexPathForSelectedRow!.row
destination!.viaSegue = achievements[selectedRow]
print(selectedRow)
}
}
That fails because it finds nil unwrapping the optional during achievements[selectedRow] since that apparently gives me a nil from let selectedRow = AchievementsTable.indexPathForSelectedRow!.row
I think the problem is that your segue is connected with your AchievementsViewController to IndividualAchievementsViewController instead of UITableViewCell to IndividualAchievementsViewController. Check in the storyboard for that and correct the segue with TableCell to IndividualAchievementsViewController.
So the solution was to go into DidSelectrowAtIndexPath and remove DeselectRowAtIndexPath, which was in there twice by accident. Then use the following
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "IndividualAchievementsSegue" {
let destination = segue.destinationViewController as? IndividualAchievementsViewController
let selectedRow = AchievementsTable.indexPathForSelectedRow?.row
destination!.viaSegue = achievements[selectedRow!]
print(selectedRow)
}
}

Value of type 'UIViewController' has no member 'varView'

Please help fast
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var DestVC = segue.destinationViewController as UIViewController
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
DestVC.varView = indexPath.row // This is the error line
}
The error is
Value of type 'UIViewController' has no member 'varView'
change UIViewController with your destination view controller.
for e.g your destinationviewcontroller name is YourViewController
and also use if let for safely unwrap
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let DestVC = segue.destinationViewController as? YouViewController {
var indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
DestVC.varView = indexPath.row
}
replace
var DestVC = segue.destinationViewController as UIViewController
with
var DestVC = segue.destinationViewController as YourDestinationClassName
and make sure that there is a varView member available in that class
Hope this will help :)

prepareForSegue UICollectionView not working (swift)

I’ve tried to push segue by using UICollectionView (show image form MasterView to DeatilView) but is not working. Xcode also doesn’t show any error message. What’s wrong with my code. I need some advice.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
collection is empty array get data form json
[title = self.nameFood.title], it is working in DetailMenuViewController.swift but not image on code above.
You need to add the UICollecitonViewDelegate
func collectionView(collection: UICollectionView, selectedItemIndex: NSIndexPath)
{
self.performSegueWithIdentifier("showDetail", sender: self)
}
After that add
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showDetail" {
if let indexPath = self.collectionView?.indexPathForCell(sender as! UICollectionViewCell) {
let detailVC = segue.destinationViewController as! DetailMenuViewController
detailVC.picFood = self.collection[indexPath.row]
}
}
}

Swift pass data between VCs

i'm new to Swift and i would like to know why this piece of code i translated from Obj-C doesn't work here.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let itemVC: ScoreVC = segue.destinationViewController as ScoreVC
itemVC.detailItem = object
}
}
Here is the storyboard. I have a Show Replace segue from the cell to the UINavigatorController
I get this error as soon as i click on a UITableViewCell
The problem has nothing to do with Swift. The destinationViewController is the navigation controller, not your ScoreVC, so the code you translated that from shouldn't have worked either (if it had the same setup). It should look like this,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "scoreDetail" {
let indexPath = self.tableView.indexPathForSelectedRow()
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
let nav : UINavigationController = segue.destinationViewController
let itemVC: ScoreVC = nav.topViewController as ScoreVC
itemVC.detailItem = object
}
}

Resources