Preselected options in UIPickerView when app starts SWIFT - 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>]

Related

How do i randomize between labels when button is pressed?

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!

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?

Display route.steps (swift )

I am having some problem when it comes to display the steps from MKDirections route into a UILabel. I tried get each index from the steps array using the for in loop, it only show the Arrive at destination which means that all the instructions within the loop went by so fast, therefore i am seeing the last index in the array . Can someone help me please ...
it shows "Arrive at destination" because you are cycling the whole array and the label gets only the object at the end, you are basically overwriting it over and over until the for cycle arrives to its final element
You are seeing only the last value from the array in the UILabel, though it is actually displaying all the values very, very quickly. If you just want to see each of the values in the UILabel, you could either use UITimeInterval to change the value every few seconds, or add a UIButton that would handle updating of the text on the UILabel.

Xcode: he first view doesn't see changes made by the second one

I'm a very newbie in the obj-c programming and have some troubles trying to change values between two views. I'm using Xcode 4.5 and storyboards and I've some problems with passing a changed value from the second view to the first one.
Here's my 2 very simple views (posting the link as I'm a new user and can't post images):
https://www.dropbox.com/s/q4o2bblu1p57zod/img.png
These views are assigned to the same class (ViewController) and the code I'm using to change the 2 labels is:
-(IBAction)setLabel:(id)sender
{
if (myTextField.text.length != 0) {
myLabel1.text = myTextField.text;
myLabel2.text = myTextField.text;
}
}
The problem is that Label1 changes correctly its text, but there's nothing to do with Label2! It doesn't want to change...
I think I'm trying to do something that can be made in others ways...Can you please tell me if it's correct?
You need to use Protocol-Delegate approach to update content in First view.
I suggest you to visit this sample link.
Your two view controllers may be of the same class but they are going to be different objects at runtime. You have a segue between them and when that executes a new instance will be created. Since the 'label2' of the second instance is not displayed on its screen, your assignment doesn't produce a visible change.

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});