App fails to replace label text with UUID - ios

I am trying to work on a program that will change a label to a user's UUID but for some reason it does not work and I can't seem to figure out since I'm still learning the language. Below is my attempt. This is the ViewController.swift file:
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var text: UILabel!
#IBAction func getuuid(sender: AnyObject) {
text.text = UIDevice.currentDevice().identifierForVendor!.UUIDString
}
}

Place a breakpoint on the line where you set the text.text value to the UUID. Odds are, you are missing the connection from your storyboard to the function call. To test this, click on the button once the breakpoint is in place, and if you see it freeze the code at that spot, then your connection is at least working. If it does not freeze there, then you need to connect the getuuid function to your button.

Related

Where should I drag and drop "label"?

Now I'm learning swift using Xcode, but I don't know where to drag and drop and why drag and drop "label" to somewhere
should I drop no.1? or no.2? or no.3? and why I should drop there?
If you drop it to 1, Xcode automatically generate an #IBOutlet for you. If you drop it to 2 or 3, then #IBAction. There's no other reason for that except that Xcode tries to be smart and help you organize code more nicely: properties with properties in the top of the class, methods – in the methods area. And you can also move declarations to another place later: except for code style matters, it doesn't matter, where exactly within your class you put declarations.
as you are trying to take an IBOutlet for a label , its nice to keep it on top of the class (where you mention 1).
normally Xcode gives suggestion you , if you drug on top side it will be #IBOutlet
or you on bottom like 2 or 3 it will be #IBAction like you already took a button action in your code .
Your Solution On Code:
import UIKit
class CodePresentViewController: UIViewController {
#IBOutlet weak var YourLabel: UILabel! // insert #IBOutlet type property here
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func tapBackButton(_ sender: UIButton) {
//Yout Button COde
}
}
If you want to own the label in your code, and then make some configuration to the label by code, use "1". It will give you a #IBOutlet label object in your code.
If you want to set the label's action, use "2" or "3".

uncaught exception 'NSUnknownKeyException' - this class is not key value coding-compliant for the key buttonPressed

I am creting my first IOS app. and am stuck with this error. I knew it is asked many times but i don't find my answer or may be not understand those answer as i am complete beginner in ios.
Here is my viewController file code.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var showLabelText: UILabel!
#IBAction func buttonPressed(_ sender: UIButton) {
let title = sender.title(for: .normal)!
showLabelText.text = "I Am \(title)"
}
}
I tried by "resetting content and settings" but that doesn't helped.
Please help me to solve this problem. It will encourage me to learn IOS. Thanks in advance.
In your storyboard, make sure that your IBAction is properly connected with the button that triggers it. It's possible that you modified the IBAction (changed the name, maybe?) and your storyboard no longer knows what you're talking about.

Connected UILabel error EXC_BAD_EXCEPTION when trying to change text value

Something very strange has been happening when I change my UILabel's text in my view controller's viewDidLoad method. Although I am 100% certain that the label is connected (I have reconnected it multiple times as well as changed the name), it still gives me an EXC_BAD_EXCEPTION error when trying to change it. My code is below.
**NOTE: I should also mention that this error does not occur when the VC first initializes, but when I press a button that segues to another VC.
class BroadwayOrderReview: UIViewController, UITableViewDelegate, UITableViewDataSource, ClassNameDelegate {
#IBOutlet weak var BroadwayOrderReviewTableView: UITableView!
#IBOutlet weak var finalOrderPriceTotalLbl: UILabel!
var OrderDictionary: [String:String] = [:]
func addButtonAction(addedList:[String:Float],numOrders:[String:Int]) {
print(addedList)
print(numOrders)
}
override func awakeFromNib() {
}
override func viewDidLoad() {
super.viewDidLoad()
print("NUM ORDERS \(numOrders)")
self.finalOrderPriceTotalLbl.text = "0.00"
let totalPriceArray = Array(numOrders.keys).sort(<)
for (key) in totalPriceArray {
print("TOTAL PRICE ARRAY \(totalPriceArray)")
self.finalOrderPriceTotalLbl.text = String(Float(self.finalOrderPriceTotalLbl.text!)! + (Float(numOrders[key]!) * addedList[key]!))
print("TOTAL ORDER LBL \(finalOrderPriceTotalLbl.text)")
}
BroadwayOrderReviewTableView.delegate = self
BroadwayOrderReviewTableView.dataSource = self
for (name,orders) in numOrders {
print(name)
OrderDictionary["\(numOrders[name]!) \(name)"] = String(addedList[name]! * Float(numOrders[name]!))
}
print(OrderDictionary)
}
Thank you for any and all help, I really appreciate it.
You should not use a capital for naming your tableView.
BroadwayOrderReviewTableView should be broadwayOrderReviewTableView. You only want to use class, struct with first letter caps.
Did you try to delete your label from your code and storyboard?
It looks like the connection isn't there.
With what you shared it seems it can't access a value because it hasn't been initialized.
Something i didn't know when I started learning dev. you can actually type in the console log.
type po finalOrderPriceTotalLbl (for print out finalOrderPriceTotalLbl)
You can only use that when your app is on standby with breakpoint, and maybe crashes (I forgot for one sec) to setup a breakpoint just click on the number's line. to remove just drag it out.
If the print give you nil. you know there is a issue with your label connection for sure.

Renaming an outlet in Swift

I accidentally misspelled one of the outlet's in my view controller and ran into a few issues. When I manually try to correct the typo I get stopped at runtime within AppDelegate I'm shown the message,
Thread 1: signal SIGABRT
which highlights the beginning of the code block:
class AppDelegate: UIResponder, UIApplicationDelegate {
...
}
I've found that to fix this issue in Objective-C you would right-click the outlet's original name and "Refactor => Rename" but unfortunately I get the message:
Xcode can only refactor C and Objective-C code
View Controller
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var billTextField: UITextField!
#IBOutlet weak var tipRateSegmentedControl: UISegmentedControl!
#IBOutlet weak var tiLabel: UILabel! // Variable name should be "tipLabel"
#IBAction func calculateTapped(sender: AnyObject) {
var userInput = billTextField.text as NSString
var totalBill: Float = userInput.floatValue
var index: Int = tipRateSegmentedControl.selectedSegmentIndex
var tipRate: Float = 0.15
if index == 0 {
tipRate == 0.15
} else if index == 1 {
tipRate = 0.20
} else {
tipRate = 0.25
}
var tip: Float = totalBill * tipRate
tipLabel.text = "$\(tip)"
}
}
EDIT: Since Xcode9, there's a refactor function; the new good method is the one in this answer
Since Xcode 6, you can look for your outlet names directly in the Find navigator (Cmd+3), it shows occurrences in your code and in your xibs and storyboards. It also works with actions.
Just search the name in the Find navigator.
You can see that the second result is a reference in a storyboard. You can replace one by one by clicking on each result and press Replace, or you can directly Replace All if you are sure you don't break anything.
Refactor > Rename...
Xcode 9 had many editor improvements including a feature that changes an outlet/action name in the code and storyboard from one place.
In the example Swift code below, right click on btnRequestCode, from the popup menu select "Refactor > Rename...", change the outlet/action name and Xcode changes it in the Storyboard also.
#IBOutlet weak var btnRequestCode: UIButton!
With swift this kind of refactor doesn't work yet. You have to change the name of the outlet, and then delete and reset the connection in the interface builder.
Updated:
Right click the IBOutlet that you want to rename, delete reference outlet, and then reassign it, but you don't have to do anything with your code.
I like to fix situations like this by just opening the Storyboard as source code and editing the XML-like tags.
Right click the .storyboard file and choose "Open As". Search for the old outlet (or action/method) name that is still set in the storyboard but which no longer matches the renamed method/outlet object in the View Controller. Edit the Xml tag in the storyboard to give it the new name of your outlet or action in the view controller.

Changing text of Swift UILabel

I am attempting to learn Apple's Swift. I was recently trying to build a GUI app, but I have a question:
How do I interact with GUI elements of my app? For instance, I used interface builder to make a UILabel, and I connected it to my viewcontroller by control-clicking, so that I get the #IBOUTLET thing. Now, how do I, while in my view controller, edit the text of this UILabel? To state it another way, what code can I use to programatically control the text of something on my storyboard? All methods I have found online only appear to work with a button generated in code, not a button generated on a storyboard.
I've seen code like
self.simpleLabel.text = "message"
If this is right, how do I link it with the label in question? In other words, how do I adapt this code to be connected with the IBOutlet (If that's what I do)
If you've successfully linked the control with an IBOutlet on your UIViewController class, then the property is added to it and you would simply replace simpleLabel with whatever you named your IBOutlet connection to be like so:
class MyViewController: UIViewController {
#IBOutlet weak var myLabel: UILabel!
func someFunction() {
self.myLabel.text = "text"
}
}
The outlet you created must've been named by you. The outlet belongs to your view controller. self.simpleLabel means 'fetch the value of my property named 'simpleLabel'.
Since different outlets have different names, using self.simpleLabel here won't work until your outlet is named 'simpleLabel'. Try replacing 'simpleLabel' with the name you gave to the outlet when you created it.
The correct way now would be:
self.yourLabelName.text = "message"
If you have something like this for an IBOutlet:
#IBOutlet var someLabel: UILabel!
then you could set the text property just like in your example:
someLabel.text = "Whatever text"
If you're having problems with this, perhaps you're not assigning the text property in the right place. If it's in a function that doesn't get called, that line won't execute, and the text property won't change. Try overriding the viewDidLoad function, and put the line in there, like this:
override func viewDidLoad() {
super.viewDidLoad()
someLabel.text = "Whatever text"
}
Then, as soon as the view loads, you'll set the text property. If you're not sure if a line of code is executing or not, you can always put a breakpoint there, or add some output. Something like
println("Checkpoint")
inside a block of code you're unsure about could really help you see when and if it runs.
Hope this helps.
You may trying to change a UI component not in the main thread, in that case, do this:
DispatchQueue.main.async {
someLabel.text = "Whatever text"
}

Resources