In projects prior to Swift 3 - If I was looking to segue an indexPath from a tableController, I'd have a line of code like
let indexPath = self.tableView.indexPathForSelectedRow!
However, in Swift 3, I am unable to write this. I can't seem to find any documentation on Apple's site or other SO questions to find a workable piece of code.
In response to Frankie's comment, I went back to my code (here is all of the current segue Swift 3 code) and have tried to copy and paste above code snippet... but to no avail. Xcode does not auto complete if I enter "self.tableView". It does not recognize indexPathForSelectedRow! either. I'm missing something.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "dineSegue"
{
if let destinationViewController = segue.destination as? RestaurantController
{
let indexPath = DineController.indexPathForSelectedRow!
destinationViewController.restaurantIndex = index
}
}
}
I googled 'indexPathForSelectedRow Apple documentation` and was able to find the property reference for you as it was conveniently the first result.
https://developer.apple.com/reference/uikit/uitableview/1615000-indexpathforselectedrow
That being said, it's exactly what you posted in Swift 3.
self.tableView.indexPathForSelectedRow
Are you sure the "DineController" reference is to the actual UITableView
When I had this issue, my initial reference was to the generic class, not the actual table I had coded.
Related
Has anyone experienced this? I'm, not 100% certain that this is iOS12-related but calling performSegue inside didSelectRowAtIndexPath has a delay of like 1-2 secs.
I already tried different things that I found elsewhere like bringing it to the main thread but nothing works. Not sure if this is a bug or not but I haven't seen anyone talking about it online.
Try your code inside main thread:
DispatchQueue.main.async{
self.performSegue(withIdentifier: "YourSegueName", sender: self)
}
This worked for me, As sometimes we can not get e main thread which is important if you are working with some UI stuff.
Are you using the prepare Method? if so, what are you doing before the Segue?
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ExampleSegue" {
let ChangeVC = segue.destination as! ExampleViewController
...
}
}
Have you tried to hand over the index path of your selected Row to a different ViewController? And decide there what to do?
I have a view controller with a name input text box in it. I want to be able to pull the name.text value from my main view controller, but I'm struggling with the code.
Sorry, I know there are similar questions, I am struggling to understand where I am going wrong though, here is my code:
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "statsegue" {
let statVC = segue.destination as! SecondViewController
NameLabelMain.text = statVC.NameDisplayStat.text
//or try that if above doesn't work: detailsVC.passedString = recipes[indexPath?row]
}
}
This is on my main page, trying to pull the data from my second view.
Any thoughts or suggestions would be awesome.
Thanks
This is a common timing problem.
In prepareForSegue the view of the destination view controller is not loaded yet so all IBOutlets are not connected yet (aka not available).
You can only access data which is available right after initialization.
However generally you are passing data from the source controller to the destination controller rather than the other way round.
I was wondering if someone can help me with this. Right now when I click on the button 0 it will automatically take me to the 2nd screen for testing purposes. Now I want to give value to the numbers accordingly and when the number clicked is equals to the number on my label than it will show the 2nd screen. If not, the Question mark label will blink or change color. Thanks
Place this variable above viewDidLoad():
var chosenNumber: Int?
Attach your buttons to an IBAction like this one and in the storyboard make each of their tags equal to their face value.
#IBAction func aNumberButtonWasPressed(_ sender: UIButton) {
chosenNumber = sender.tag
performSegue(withIdentifier: "theSegueIdentifier", sender: nil)
}
Pass the number to the following view controller like so and then use that number to populate the label accordingly.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
super.prepare(for: segue, sender: sender)
if let vc = segue.destination as? TheViewControllerYouAreGoingTo {
vc.theVariableYouWantToSaveYourNumberTo
}
}
If you want to know how to do something differently, just ask.
If you want to really learn swift well, I highly recommend taking this Paul Hegarty Course for FREE. Paul Hegarty Course
The first two lessons specifically will teach you everything you could want to know about proper swift syntax and building a calculator early on. It might seem slightly advanced, but that is why I have watched the course 3 times now. Every 3-6 months since I started learning Swift this has helped refine my skills.
EDIT
let thisString = label.text
let thisInt = Int(thisString)
thisInt will be an optional, so at some point you will need to unwrap it. You can use it however you want to and set it to another label like so:
anotherLabel.text = "\(thisInt)"
I am swift beginner and I am struggling with my first application. Sorry if my question is too basic, but I get an error message in my code here:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
let secVC: NavigationViewController = segue.destinationViewController as! NavigationViewController // here is the error
secVC.receiveImeNaSladkarnica = Label1.text!
secVC.KordaA = Shirina.text!
secVC.KordaB = Duljina.text!
secVC.PodImeNaObekt = Label2.text!
I have created 2 different buttons: Navi Me and More Info buttons.
The first one (Navi Me) is connected with another view controller which is called NavigationViewContorller, there I send all of the data I need by using prepare for segue. Everything was working perfect, but now I have created another button which is called (MoreInfo).It is connected with 3rd viewcontroller called MoreInfoViewController. I think I have connected everything fine, but still when I click on the MoreInfo button the app stops working and I get the following error: Thread 1: signal SIGBART in the marked line. I cannot understand why is it breaking when this segue is for another view controller, nt for the MoreInfoViewController. Would you please advice me what to do or provide me with an example how to fix it
I have tried that:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
switch segue.identifier {
//error here-> case "VC1" :
let secVC: NavigationViewController = segue.destinationViewController as! NavigationViewController
secVC.receiveImeNaSladkarnica = Label1.text!
secVC.KordaA = Shirina.text!
secVC.KordaB = Duljina.text!
secVC.PodImeNaObekt = Label2.text!
//error here -> case "VC2" :
let secVC2: MoreInfoViewController = segue.destinationViewController as! MoreInfoViewController
secVC2.receiveImeNaSladkarnica = Label1.text!
secVC2.KordaA = Shirina.text!
secVC2.KordaB = Duljina.text!
secVC2.PodImeNaObekt = Label2.text!
default:
break
It still doesn't work
Each segue is supposed to have an unique string identifier.
For multiple view controllers use a switch statement
switch segue.identifier {
case "vc1" :
// prepare to go to vc1
case "vc2" :
// prepare to go to vc2
default : break
}
Once again my yesterday comment:
Since you are a beginner in Swift please learn first to consider the naming convention that variable names start with a lowercase letter.
Big problem here guys, I'm on the verge of finishing my blog project until I came across a segue issue, I'm trying to display the contents of a blogs web view on a uiwebview in another controller. This is my first time working with MediaRSS and AFNetworking. I have been able to parse the contents before, but am having no luck this time around. All help is greatly appreciated. I keep getting NSIndexPath is not a member of PostLink
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "viewpost" {
let indexPath = tableView.indexPathForSelectedRow()
let item = items[indexPath!.row]
let detailViewController = segue.destinationViewController as PostViewController
detailViewController.postLink = indexPath.postLink
}
}
}
UPDATE 9:17PM
Looks like im performing a segue properly, because now my page will go to a blank. Can someone please confirm the parameters I am passing in, I just need someone to look and tell me please
NSIndexPath does not have any member named postLink. I think you mean to write item.postLink.
detailViewController.postLink = item.postLink
I hope item is of some type that has 'postLink' member and it is populated with appropriate value.