I want to add a random number in front of my UI label this is my code which is not working.
#IBOutlet var Label2: UILabel!
#IBOutlet var Label1: UILabel!
Label1.text! = "1"
Label2.text! = "2"
var random = arc4random_uniform(2) + 1
if Label(random).text! == "1" {
print("This is Label 1")
} else {
print ("This is Label 2")
}
If there is any other way to add a random number in front of UI Label i'll welcome the answer.
Use IBOutletCollection
#IBOutlet var label: [UILabel]!
label[0].text! = "1"
label[1].text! = "2"
var random:Int = Int(arc4random_uniform(2))
if label[random].text! == "1" {
print("This is Label 1")
} else {
print ("This is Label 2")
}
UPDATE:
make sure all the labels you want in the array is highlighted
right click and drag to your collection view
set connection to Outlet Collection
You can put your labels into an array and then pick a random one:
#IBOutlet var label1: UILabel!
#IBOutlet var label2: UILabel!
var labels = [UILabel]()
override func viewDidLoad() {
super.viewDidLoad()
labels.append(label1)
labels.append(label2)
var random = Int(arc4random_uniform(2))
if labels[random].text! == "1" {
print("This is Label 1")
} else {
print ("This is Label 2")
}
}
It seems that what you're trying to achieve with Label(random) is to dynamically change the Label variable name. As pointed in other question, this is probably a bad practice. So, I would recommend you read Create a variable in swift with dynamic name, first.
[edit:]
Just to give an example, you can do that using an Array, such as:
// Creating two UILabels:
var label1 = UILabel()
var label2 = UILabel()
label1.text = "0"
label2.text = "1"
// An array of Labels, which starts from 0:
let labels: [UILabel] = [label1, label2]
// Another random function which goes to the size of the array:
var i = random() % labels.count
// Just to check the random number:
print("Your random number: \(i)")
// And then, the test:
if labels[i].text == "0" {
print("This is Label 0: \(labels[i].text)")
} else {
print ("This is Label 1: \(labels[i].text)")
}
Just apply these ideas to your IBOutlet variables.
Hope it helps :D
Related
///swift iOS
/// I have tried with below code
#IBOutlet weak var myLbl: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
func magic(choose: String, row: Int) -> String {
var name = ""
for i in 1...row {
name += (String.init(repeating: " ", count: row-i)+String.init(repeating: choose, count: 2*i-1))
}
return name
}
#IBAction func btnPressed(_ sender: UIButton) {
let result = magic(choose: "0", row: 5)
myLbl.text = result
}
plz check images which type of response I am getting
}[I am getting output at this manner][1]
[1]: https://i.stack.imgur.com/YdV8f.png
we have to show this type of response on my UILabel
Please open up a fresh playground and paste the following code in:
import UIKit
let label = UILabel()
label.text = " 0 \n000" // simple demo text
label.numberOfLines = 0 // unlimited number of lines
label.font = .monospacedSystemFont(ofSize: 14, weight: .regular) // monospaced font so that columns are aligned
label.sizeToFit() // tell the label to evaluate its content and adjust its size to fit
It should display a basic example of what you want and give you all the information you need to update your code accordingly.
I have a difficulty in a simple task. I googled this topic, but other examples are complicated by additional syntax that I don't understand yet. Can you help me to solve it or give link if there is already was similar topic.
I need to move the function responsible for selecting the button to a separate file, because if the number of buttons increases, it will turn into a large sheet. So made a function in separate swiftfile, but naturally the new file does not know about any buttons in viewController and can't find it in scope. Also If i’m not mistaken i need give Bool and return String.
How can I transfer a function with button sender to a separate file so that it returns non-optional text value back in the ViewController?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var Label1: UILabel!
#IBOutlet weak var BTC: UIButton!
#IBOutlet weak var ETC: UIButton!
#IBOutlet weak var LTC: UIButton!
var choice = "Choose coin"
var coinType = getCoinType()
#IBAction func optionSelected(_ sender: UIButton) {
BTC.isSelected = false
ETC.isSelected = false
LTC.isSelected = false
sender.isSelected = true
if BTC.isSelected == true{
ETC.isSelected = false
LTC.isSelected = false
choice = "BTC"
Label1.text = choice
}else if ETC.isSelected == true{
BTC.isSelected = false
LTC.isSelected = false
choice = "ETC"
Label1.text = choice
}else if LTC.isSelected == true{
ETC.isSelected = false
LTC.isSelected = false
choice = "LTC"
Label1.text = choice
}
}
}
new file, i can't understand how to get sender from buttons here
import Foundation
func getCoinType() -> String{
var choice: String
// my if else function
return choice
}
P.S. In general, is it possible to make it easier, without using UIPickerView?
On how to add functions in different swift file you have a few options. This a simple one: Create a new swift file and name it ViewController+Extenstions.swift (you can use any name though). Then add an extension to your ViewController class and add your function like this:
extension ViewController {
func getCoinType() -> String {
var choice: String
// my if else function
return choice
}
}
You can add as many functions as you need and in different files (all extensions to ViewController but of course with different file names).
PS, in your optionSelected function, you are setting:
BTC.isSelected = false
ETC.isSelected = false
LTC.isSelected = false
I don't know what you're trying to achieve, but by doing so, those if-else will never be executed since your are setting the three buttons as not-selected!
UPDATE:
For your button selection problem, you can do this:
1- Add tags to your buttons
You can do it inside viewDidLoad method. This way you can differentiate between different buttons.
override func viewDidLoad() {
super.viewDidLoad()
BTC.tag = 0
ETC.tag = 1
LTC.tag = 2
}
2- Connect the action of buttons
Although your three buttons can have three different handler functions, it's easier to connect all of them to a single handler. However, we can know which button is tapped based on the value of the tag we assign in the previous step. So, connect all buttons to optionSelected(_ sender: UIButton) through Storyboard (because you have used Storyboard.
3- Rewrite getCoinType function
func getCoinType(tag: Int) -> String? {
var choice: String?
switch tag {
case 0:
choice = "BTC"
case 1:
choice = "ETC"
case 2:
choice = "LTC"
default:
choice = nil
}
return choice
}
Buttons' handler
Now when a button is tapped we call getCoinType function with that button's tag as input argument. It will return the string and we assign it to the Label1:
#IBAction func optionSelected(_ sender: UIButton) {
let choice = getCoinType(tag: sender.tag)
Label1.text = choice
}
And you're done!
I have a list of buttons inside an array. I want to replace the buttons position order using that array. I can do this in java, but cant do the same in swift. Here is my code:
#IBOutlet weak var menuView: UIView!
struct ButtonsIndex {
var button: UIButton
var index: Int
}
var footer: [ButtonsIndex] = []
//Set Buttons order etc
...
func setFooter(){
let count = 0
for buttons in menuView.subviews {
if var button = buttons as? UIButton {
button = footer[count].button
count += 1
}
}
}
Is it impossible to change the buttons position without connecting the outlets to my code? Thanks in advance.
I am trying to unhide n number of elements depending on the users input into a text field.
So the user enters a number between 1 - 5 in the text field then clicks submit which calls createSplit. As you can see, it unhides a view and then I want it to loop x (x being the number the user inputs) amount of times to unhide day(i)View textfield
#IBAction func createSplit(_ sender: Any)
{
noOfExerciseView.isHidden = false
let noOfDays: Int = Int(numberOfDays.text!)!
for i in 1...noOfDays
{
day\(i)View.isHidden = false
}
}
I have a working solution but it's not the most efficient so I hope someone can help doing this an efficient way.
#IBAction func createSplit(_ sender: Any)
{
noOfExerciseView.isHidden = false
let noOfDays: Int = Int(numberOfDays.text!)!
for i in 1...noOfDays
{
if (i==1)
{
day1View.isHidden = false
} else if (i==2)
{
day2View.isHidden = false
} else if (i==3)
{
day3View.isHidden = false
} else if (i==4)
{
day4View.isHidden = false
} else if (i==5)
{
day5View.isHidden = false
}
}
}
String interpolation cannot be used to set the name of a variable:
day\(i)View.isHidden // does not work
Your best bet is to use an outlet collection to define all your day views.
Instead of this:
#IBOutlet var day1View: UITextField!
#IBOutlet var day2View: UITextField!
#IBOutlet var day3View: UITextField!
//...
Do this:
#IBOutlet var dayViews: [UITextField]!
Then you can write your loop like this:
for i in 0...noOfDays-1
{
dayViews[i].isHidden = false
}
Note that to do this, you'll need to delete the existing outlets and reconnect them.
If you're using a storyboard, then when you Control-drag from your first text field to your class file, select Outlet Collection for the Connection type and name it dayViews. To add the remaining text fields to the collection, just Control-drag from each one to the dayViews var in your class file.
I created a simple app where the user enters their name into a TextField, clicks a button, and then a label above the textField changes its text to "Hello Name!".
I'm trying to verify that this works correctly with a UI Automation Instrument. I am able to enter text and click a button but when I try to verify the labels new value it doesn't return the correct value. Here is the code,
var input = target.frontMostApp().mainWindow().textFields()["NameInput"];
var button = target.frontMostApp().mainWindow().buttons()["SubmitButton"];
input.setValue("Sonny");
button.tap();
// This is not returning the labels correct value!
var label = target.frontMostApp().mainWindow().elements()["HelloLabel"].value();
if(label != "Hi Sonny!")
{
UIALogger.logFail("The Hello Label did not have the correct value! "+label);
}
else
{
UIALogger.logPass("The Hello Label was correct :D ");
}
I've tried using label() and value() but they both return the "HelloLabel". Has anyone encountered this and found a solution to retrieve the labels updated value?
Here is the xcode code if its needed...
#IBOutlet weak var MyLabel: UILabel!
#IBOutlet weak var MyName: UITextField!
#IBAction func sayHi(sender: AnyObject) {
MyLabel.text = "Hi \(MyName.text)!"
}