IOS Array from text field - ios

I'm building an IOS app. I want the user to input two options from a text field. One of the options will later be randomly displayed in a label. I'm thinking the text field should log the information in an NSMutableArray that I can later call on. The information only needs to be temporarily stored because it will go away after it is randomly displayed in the label.
I'm not sure how to do create this array from a text field. Any help is appreciated.

Is this what your looking for?
int choice = rand() % 2;
NSString* result;
if (choice == 0)
result = textField1.text;
else
result = textField2.text;
....
label.text = result;

Related

(Adobe Animate ActionScript) how can iRemove specific Symballl's from the stage using name, arry, and lib()?

I'm super frustrated with this.
first for you to understand my code - My goal here is for the user to get randomly selected word appear to them in a way that every letter sits inside of a box.
Then if the user clicks on a button called "Pick a word", another word will be selected and the correct number of boxes will appear.
I have an array of words like this:
var word_group_1 = ["abolsh", "absorbent", "betrayal", "frutish", "commensurate", "eonfident", "zite"]
I'm using this function to select a random word from that array then splice it.. works perfectly:
function random_word_genereator() {
random = randomNumber(0, word_group_1.length);
//putting the chosen word from array in the chosen word variable
chosen_word = word_group_1[random]
//after we used the chosen word were removing it from the away
word_group_1.splice(random, 1)
//splitting the chosen word into an array
chosen_word_letters_arry = chosen_word.split("")
}
in a button click of "pick a word"- I'm creating 5 instances of a Movieclip I have in my library (just a blue box to put text in it) with text in at like this:
function create_boxes(e)
{
//to know which word has been displayed to the user//
old_word=chosen_word
random_word_genereator()
for (i=0;i<chosen_word.length;i++){
cell_boxes = new lib.cell_box();
stage.addChild(cell_boxes)
cell_boxes.name="cell_box"+i;
cell_boxes.x=(xlocation * i) + 50
cell_boxes.y = 80;
output = new createjs.Text();
cell_boxes.addChild(output)
output.text=chosen_word_letters_arry[i]
}
everything works fine on the first click As You Can View Here.
The word being selected and displayed on the stage
my problem is when I'm clicking Again on the button "pick a word"
its not deleting the correct number of boxes.
I'm putting visible false to the boxes which holds the "Old word" (the one I need to delete)
but As you can se here After I click again its getting messed up.
sometimes its's working, switches from 12 letter word, to a 4 one.
but it should be luck. I'm dying to get this to WORK! its for my school project.
Please help me!
Easy answer that will plug and play into your code:
js
...
//to know wichh word has been displayed to the user//
old_word=chosen_word
random_word_genereator()
for (i = 0; i < stage.numChildren; i++) // Loop through all children of the stage
if (stage.getChildAt(i) is lib.cell_box) // Checks if the child is a lib.cell_box
stage.removeChildAt(i--); // Removes child from stage and decrements i
for (i=0;i<chosen_word.length;i++){
...
Original answer (cleaner code, some restructuring):
It's best to break this kind of logic down into steps.
var boxes:MovieClip = new MovieClip();
boxes.y = 80;
addChild(boxes);
...
function createBoxes(word:String):void {
// Remove boxes first
while (boxes.numChildren > 0)
boxes.removeChildAt(0);
// Add boxes
for each(var c:String in word.split("")) {
var box:Box = new Box(c);
box.x = boxes.width + 50;
boxes.addChild(box);
}
}
Then set the text inside a Box class.

MIT App Inventor & Arduino & ESP8266 : Store Values in Google Firebase

I'm trying to built an Android application in MIT App Inventor 2.
This is my design
This is my code blocks
My purpose is; when I click somewhere on the color wheel; getting the coordinates of the place that I clicked (black ball) and get its RGB values.
It works perfectly on phone screen, it shows the values. But the problem is; when I try to import the rgb values to Firebase, the values are like in this format in this picture
As you see, the text formats in their boxes are like: "\"101\""
But I want: 101 only. Because I will get the values to my NodeMCU ESP8266 for blink a RGB LED. I will insert these values to analogWrite(pin,value) function.
Where is my fault in MIT App Inventor Block screen? Is there any solution in there? Or can you give me suggestion about it for ESP8266 code part (like split the text or something) ?
You can add this line
String b_fir = Firebase.getString("B");
String str_b_fir = getStringPartByNr(b_fir, '"', 1);
int int_b_fir = str_b_fir.toInt();
You can add this function under the loop
String getStringPartByNr(String data, char separator, int index)
{
// spliting a string and return the part nr index
// split by separator
int stringData = 0; //variable to count data part nr
String dataPart = ""; //variable to hole the return text
for(int i = 0; i<data.length()-1; i++) { //Walk through the text one letter at a time
if(data[i]==separator) {
//Count the number of times separator character appears in the text
stringData++;
}else if(stringData==index) {
//get the text when separator is the rignt one
dataPart.concat(data[i]);
}else if(stringData>index) {
//return text and stop if the next separator appears - to save CPU-time
return dataPart;
break;
}
}
//return text if this is the last part
return dataPart;
}

limit UIlabel text field in iOS to two characters, reset when 3 or more are entered

I am a beginner learning iOS development.
I am trying to limit the contents of a label marked "Factor" to only allow 2 whole integers between 0 and 99. I also want to force the user to enter at least two digits for every Factor attempt (i.e. "7" would be "07").
The label is not directly edited by the user. There is a number pad that the user can use to enter the Factor directly (just as you would on a calculator or remote control). There is also an Increment and Decrement button that modifies the label text.
I have limited the label so that it will not go above 99 or below 0 when the Increment or decrement buttons are pushed. But I am stuck on how to limit the user from pressing 3 or more numbers and that getting populated in the label text.
Current behavior:
Currently, if I press '9','0','1', that will result in '901'.
My goal would be to press '9','0','1', and see '1'.
In other words, more than 2 characters would never be displayed and once a third number is pushed, it will overwrite the existing two.
Here is what I am doing for my number buttons:
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if (self.numberCheck)
{ self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];
}
else
{
self.fDisp.text = number;
self.numberCheck = YES;
}
}
I think is is easy to accomplish that. See my solution below, I think it should work in your case.
- (IBAction)numPush:(UIButton *)sender {
NSString *number = sender.currentTitle;
if([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text stringByAppendingString:number];
}
You could simply check the length of the label string.
if ([self.fDisp.text length] == 2)
self.fDisp.text = sender.currentTitle;
else
self.fDisp.text = [self.fDisp.text
stringByAppendingString:number];

Average of textfields depending on how many textfield are "used"

Little explication: I have an app with 5 textfields, i already got the code to make the avaerage of them. Using floates and giving a variable to each textfield.so, the adding of the value of all the textfield /5 (divided per 5)
My problem is that when a textfield is leaved emptty, its not goona be /5, if user fill in only 4 cells it will be/4 and so on.
Question: how to divide the adding of cells depending in how many cells have content?
I was tryng with textfield.text.length > 0 in an if. But i dont get it
Thanks, hope i was clear.
You could check each textfield and see if the text is NULL or blank (#"")
Use an int to keep track of the number of textfields that have a value.
For example:
int counter = 0;
float total = 0.0;
if([textfield1.text length] > 0)
{
counter++;
float thisValue = [textfield1.text floatValue];
total = total + thisValue;
}
repeat this for all 5 textfields and then divide your total by the counter variable.
You could include the if statement in a loop if you are going to vary the number of text fields or to reduce the amount of coding you do.
EDIT I have changed my answer based on the good suggestions of other to use text.length
try to count the number of used textfields by increasing a number every time you read out a textfield that has valid text in it and divide the result by that number. (logically the "number" should be initialized as 0)

trying to add number to number allready saved on label

So I have a TextField integer that needs to be added to a label.
For example when I type 20, it appears in the label and when I save it and come back it's still there, so far so good right, i got this part down.
Now lets say I come back and want to add another amount to whats already there, to the 20. how exactly would I go about doing it?
I used the NSUserDefaults to load/save the data but when I go to update the label I get stuck when trying to use my variables, say: currentValue, valueToAdd and valueTotalAfterAddition.
You have to get the intValue of the text, and then add whatever number. Example
int currentValue = textField.text.intValue;
int valueToAdd = 10; //change this to whatever you need
int valueTotalAfterAddition = currentValue + valueToAdd;
label.text = [NSString stringWithFormat:#"%d", valueTotalAfterAddition];

Resources