How to delete a character from a UI Label in Swift? - swift

I am new to Swift and I am having issues with deleting a character from a UI Label that I have created. I am trying to make a simple phone dailer app, and I am trying ti implement a backspace button. My UI Label is called DailerLabel, and I know I'm supposed to use the dropLast() function but I keep running into issues about mismatching types or unwrappers. I am not really sure what I am supposed to do here. I tried the thing in the commented code which didn't work, and then I tried what I listed below which doesn't either. Could anyone help me?
#IBAction func backspaceButtonPressed(_ sender: UIButton) {
if (!((DailerLabel.text?.isEmpty)!)) {
// DailerLabel.text?.substring(to: (DailerLabel.text?.index(before: (DailerLabel.text?.endIndex)!))!)
let temp = DailerLabel.text
temp?.dropLast()
DailerLabel.text = temp
}

You can try this one, replace label with your own UILabel
var name: String = label.text! //shauket , for example
name.remove(at: name.index(before: name.endIndex))
print(name) //shauke
label.text = name
print(label.text!)

You are very close. dropLast actually returns the string without the last character and you haven't stored that to anything so there is no change. You also have to conver back to String from Substring.
#IBAction func backspaceButtonPressed(_ sender: UIButton) {
if (!((DailerLabel.text?.isEmpty)!)) {
let temp = DailerLabel.text ?? ""
DailerLabel.text = String(temp.dropLast())
}
}
Here's a better version
#IBAction func backspaceButtonPressed(_ sender: UIButton) {
guard
let text = dialerLabel.text,
!text.isEmpty
else {
return
}
dialerLabel.text = String(text.dropLast())
}

Related

How to Pass the data given in UITextField to another variable on another class

So i have a UITextField named apiTextField and a function of the saveButton :
func saveButton(_ sender: Any) {
}
I want when the user writes to the UITextField and press the saveButton that text the user wrote be passed to a variable which is called var baseURL:String? in another class.
I didn't find anything related to UITextField so i decided to make this question, another similar one is 10 years old!.
var anotherClass = AnotherClass()
func saveButton(_ sender: Any) {
guard let text = apiTextField.text, !text.isEmpty else { return }
anotherClass.baseURL = text
}
Is that what you are looking for?

Missing Identifier for Radio Button when trying to identify the Sender

I'm trying to identify the clicked radio button using the Interface builder identifier.
#IBAction func text_radio_changed(_ sender: Any) {
let button:NSButton = sender as! NSButton
let id:String = button.accessibilityIdentifier()
print("===========>"+id)
}
But the console keeps printing this
===========>
There is no identifier
button.accessibilityIdentifier is the Accessibility Identity Identifier. The Identity Identifier is button.identifier.
#IBAction func text_radio_changed(_ sender: Any) {
let button:NSButton = sender as! NSButton
let id:String = button.identifier!.rawValue
print("===========>"+id)
}
On your code you are actually trying to access the accessibility identifier which is a different thing. To identify the radio button you should use tags. Set a tag for the button and then read it like this.
#IBAction func text_radio_changed(_ sender: Any) {
let button:NSButton = sender as! NSButton
let id:Int = button.tag
print("===========>"+id)
}
Note: You can actually do use the accessibility id to do the same thing. Check this other similar post.
Update: The answer by "Willeke" seems better (if it works), I am used to developing for iOS and I wasn't aware there is an identifier property for NSButtons.

Swift can't get text of label even though the same expression

im programming a calculator app as an exercise project.
I have already used the text of the label before via
calculatorLabel.text
Eventhough I use exactly the same expression in another function, I always get 0 instead of the actual text inside it. I seriously do not understand why that happens. I have already tried using debug mode and stuff but it doesn't work.
here ar the two functions (shortened):
#IBAction func operationButtonTapped(_ sender: UIButton) {
let operation = sender.currentTitle!
lastSender.backgroundColor = UIColor.systemBlue
lastSender = sender
sender.backgroundColor = UIColor.orange
//a switch would be here(deleted)
isTyping = false
let currentNumber = Double(calculatorLabel.text!)!
a = currentNumber
}
#IBAction func changeStateButtonTapped(_ sender: UIButton) {
let operation = sender.currentTitle!
let displayedNumber = Double(calculatorLabel.text!)!
var displayedText = self.calculatorLabel.text!
if operation == "+/-"{
if operation.prefix(1) == "-" {
displayedText.remove(at: displayedText.startIndex)
}
else{
displayedText = "-" + String(displayedNumber)
}
}
else if operation == "%"{
displayedText = String(displayedNumber/100)
}
calculatorLabel.text = displayedText
}
the top one gets the text while the bottom one doesn't...
thank you very much in advance for trying to help me!
I found the answer...
It seems the connection between it and the code was somehow messed up.
To all who have the same problem: right click the component in the storyboard and check the connections!

On the value of the structure of the issue

When using input structures to create multiple save arrays, do not display the array in a custom cell.
The following is the initialization settings
struct LearnList {
var Title:String
var Des:String
init (Title:String,Des:String){
self.Title = Title
self.Des = Des
}
}
The following is the method code after clicking Complete
#IBAction func Complete(_ sender: Any) {
if (InputTitleTextField.text != nil),
(InputDesTextField.text != nil)
{
let LearnString = LearnList(Title: InputTitleTextField.text!, Des: InputDesTextField.text!)
LearnArray.append(InfoString)
self.InputTitleTextField.text = ""
self.InputDesTextField.text = ""
}
}
Where should I insert the following code in the main ViewController?
(override) func viewDidAppear(_ animated: Bool) {
LearnTableView.reloadData()
}
What should be read in this area?
My learning reference link:
How To Create A To Do List App In Xcode 8 (Swift 3.0)
My question on programming in China,(there is still no answer):
UITextField:用户输入append方法错误以及回答里的新问题无法传值的问题
The first question in detail the process of inquiry, later found their own programs to show the fixed characters, but the code was not saved, try to modify their own user input failed.
Add the reloadData() to the end of your Complete() not in the viewDidAppear()

deleteCharactersInRange:NSMakeRange equivalent in swift 3 to delete characters one by one from last index of string

I have user input string which contains characters. I want to delete those characters one by one from its last index when I tap button until the string is empty
Here is my string variable
#IBAction func btnBackSpaceAction(_ sender: AnyObject) {
if self.userInput.characters.count >= 1{
let string = String(self.userInput.characters.dropLast())
print(string)
}
}
How to delete characters from last?
You can try something like this
var string = "Good Morning" // Your string
#IBAction func bnuttonAction(_ sender: UIButton) { //Your button action
string = String(string.characters.dropLast())
if !string.isEmpty {
print(string)
}
else {
print("String is Empty")
}
}
Output:
Good Mornin
Good Morni
Good Morn
Good Mor
Good Mo
Good M
...