How do i randomize between labels when button is pressed? - swift

I'm pretty new to swift. The purpose of my app is to show different text on my labels when a button is pressed. I will have hundreds of labels so should i use some kind of a database. If so how can I randomize between these labels. Would be great if any of you could write that piece of code. I also need my app to remember the previous label, so that a user can go back when another button is pressed.

Here is how to generate a random number in Swift:
let rand = Int(arc4random_uniform(x))
This generates a random number between 0 and x-1.
Unfortunately, without a more detailed description and without your code, I can't answer anything more than that.

Create an array of all the possible labels, i.e.
let labels = ["Label1", "Label2", "Label3", "Label4"]
//add data to the labels
Use randomElement() on the labels array to get a randomLabel, i.e.
let randomLabel = labels.randomElement()

You can do create an Array with labels. For example:
let labels = ["Some Text","Some Text","Some Text"]
Then use randomElement() from labels array:
randomTextLabel.text = labels.randomElement()
You can also use json to store data. I dont know how, but you can always google!

Related

Preselected options in UIPickerView when app starts SWIFT

I am new at Swift and I need help.
I have 4 UIPickerViews that have some data. Result ( that are combination of 4 ) is shown in Label. And everything is working smoothly, but what I want is, when the app starts there is some preselected values in those pickerviews that are not showing results before I manually change those values. When I start picking values I get results in Label. My question is how to show those results when the app starts with those preselected values?
This should probably do the trick:
yourPicker.selectRow(<index of item you want to select>, inComponent: 0, animated: true)
Also if the labels are not showing the correct text, then you can also do:
myLabel.text = yourPickerData[<index of item you want to select>]

How to identify programmatically generate TextViews in swift?

I need a way to identify programmatically generated text views. The problem is that I am creating a list of textviews, and I want to control them with code. How can I set and ID or something to know which one is which?
use tag
let textview = UITextView()
textview.tag = your value in int

Remove value in circle Swiftcharts

At the momtent I try to find out how I can make the table that I would need for my app. I would like to remove the value in the circles.
But now I came across the possibility during my search that it is possible to hide the value in the circle.
This should be possible with LineChartData.drawValuesEnabled = false. However, I can not find the same in the Swiftcharts code, so I wonder if this option has been removed or is it called / works differently?
Values should be removed from DataSet, not from LineChartData.
This code might help you.
let set1 = LineChartDataSet(entries: values)
set1.drawValuesEnabled = false

UIPickerView and UI Unit Test: How to get values of UIPickerWheel?

Recently I had a bad headache (and I'm still struggling) to find out how to retrieve all values inside an UIPickerWheel. For me should be enough to move at particular row of the wheel, but I can't! So frustrating! I tried to scroll row by row to retrieve all values (https://stackoverflow.com/a/39300344/821407) but it's so slow! Any clue?
NB: I can't use adjustToPickerWheelValue because my root problem is that I don't know the value since they are dynamic and I would like to avoid launchArguments/launchEnvironment.
This is probably not the answer you were hoping for but it is not possible to get the title of all rows in a UIPickerView in a UITest.
As you know when running a UITest you can only access your app's UI elements via the XCUIElement class. That class has a value property that gives you some information about the UI element you access. When accessing a UIPickerView the value gives you the title of the currently selected row. But only of the selected row. You can access the picker's row elements, but unfortunately the value property for the row elements is always empty. So, no luck here. All you the info you can get is the number of rows of your picker.
This is not really surprising though. Even if you had access to the UIPickerView, you could not access the titles of all rows directly. UIPickerView does not know about the titles that it displays. It is the job of the UIPickerViewDataSource to provide the titles.
So, unfortunately, if you need to know all the row titles of your UIPickerView in a UITest, you really have to select each value one by one via your app's user interface.
But it does not have to be as complicated as in the answer you linked. Instead of simulating a scroll you can simply tap on the next row to select it (should be slightly faster):
let pickerView = app.pickerWheels.element
let numRows = pickerView.children(matching: .any).count
var values: [String] = [pickerView.value as! String]
for _ in 0..<numRows {
pickerView.coordinate(withNormalizedOffset: CGVector(dx: 0.5, dy: 0.55)).tap()
values.append(pickerView.value as! String)
}
In your question you do not really describe what you are trying to test. But if you want to test if the picker has the correct row titles maybe a UnitTest would be a more practical approach?

Get the values in picker from server

I want to get some values to the picker dynamically. Statically it is performing well. But when I want to add picker row dynamically from calling web service, there is an occurrence of NSRangeException. Whether the array having data and I am able to alert that data. I am using Titanium SDK for this iphone application.
if(gameTypeName.length>0){
alert(gameType.length);
picker.add(gameTypeName);
}
The array is creating as:
var typeName = college[j].GameTypeName;
gameTypeName.push(Titanium.UI.createPickerRow({title:typeName}));
Whether it is working fine with static data as:
var picker_data = [
Titanium.UI.createPickerRow({title:'Title 1',value:'1'}),
Titanium.UI.createPickerRow({title:'Title 2',value:'2'})
];
picker.add(picker_data);
Believe it or not, the Picker doesn't have a "Value" property.
You can use pickerView. But, easy to use Table View. you set array data in you TableView (tableView.data =;).
and for get value
tableView.addEventListener('click',function(e){
// own your requirement
Ti.API.log(e);
});
I think this is easy to use and good looking.
If, you want to show and hide. then you can use animation or window.modal property.
var win = Ti.UI.createWindow({});
win.add(tableview);
win.open({modal:true});