Give Values to Buttons - Swift - swift

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)"

Related

Slow performSegue in tableview didSelectRowAtIndexPath - iOS12

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?

How to pass data between two screens while both are elements in NavigationController in Swift?

I have a set of storyboards under the UINavigation Controller. One of these screens contains a table view and I need to send the data of the clicked row to the next screen. I am aware that doing so would require me to use segue. But then how do I get the Navigation Bar and the Navigation Button
For better understanding I have uploaded the screen shot of my first screen. Clicking on the detail button is going to send the challenge object to next screen. However, I am looking forward to having the same navigation bar on the top of the next scree. Any idea how can I accomplish such objective?
Currently if I press on details button, I'll get the details of that challenge in the terminal.
#IBAction func CAStartChallenge(_ sender: UIButton) {
let tag = NSNumber(value: sender.tag) as! Int
print("challenge name to be printed : \(challengeTableConent[tag].display()) ")
}
In your storyboard you want to drag a segue between the view controllers, like so:
Storyboard segue dragging
Use either Show or Push (deprecated) options for the segue. Give this segue an appropriate identifier, such as Details.
Then in your button function you can add these:
#IBAction func CAStartChallenge(_ sender: UIButton) {
let tag = NSNumber(value: sender.tag) as! Int
performSegue(withIdentifier: "Details", sender: challengeTableConent[tag])
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Details",
let destination = segue.destination as? DestinationViewController,
let model = sender as? ChallengeTableContentType {
// pass through model to destination view controller
destination.model = model
}
}
You can then configure the destination view controller using its specific model when it appears.
First of all
Passing data through screens using segues is simple. Just check this answer
Pass data through segue
It will explain exactly how to do it
Second
Check the bug on your button I think you mean "Details"
Third
Check the type of segue, you might not used push or show detail. Also select the details view and make sure that "Simulated Metrics -> Topbar" is set to inferred
Some prints to help
This will ensure that the navigation bar comes from the previous controller
This is how your segue should look, do not forget to give an identifier to your segue

Different definition for sender

So I used sender a lot in swift but it confuses what exactly it does.
#IBAction func btnPressed(_sender: AnyObject){
performSegue(withIdentifier: "newScreen", sender: code)
}
Someone explain the difference between the two senders pls. It kind of confuses me because it has the same name but different functions.
The parameter of an IBAction tells you who caused the action. In your case, when you push the button that triggered this IBAction to be invoked, sender will be set to that button.
The word sender is nothing more than a name. It's a typical convention.
In your case, it's better to use a more strongly typed argument, with a more descriptive name, such as:
#IBAction func btnPressed(_ button: NSButton) { // or UIButton for iOS
performSegue(withIdentifier: "newScreen", sender: code)
}

what replaces indexPathForSelectedRow in Swift 3?

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.

Perform segue with Identifier UIWebView blog, how to link?

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.