How do I setup a reference variable to another variable/UILabel variable? - objective-c

Let's say I have several lines of code to clarify specific settings on a given UILabel variable:
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].text = #"derp";
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.x = 5;
numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY].center.y = 3;
I'd like to setup a reference variable (&$varname in PHP) for this massive array index-specified variable in Obj-C. What is the best way to do this?

Just use a pointer:
UILabel* label = numberMarkings[selectedBoxX][selectedBoxY][selectedSquareX][selectedSquareY][selectedNoteX][selectedNoteY];
label.text = #"derp";
label.center.x = 5;
label.center.y = 3;
Since you are not writing into the array, you don't need anything fancier than that. If you were to overwrite the value in the array, then you could use a pointer to a pointer:
UILabel** label_in_array = &numberMarkings[selectedBoxX]/* ... */[selectedNoteY];
// Write to the label
UILabel* label = *label_in_array;
label.text = #"derp";
// Write to the array
[label release];
*label_in_array = [[UILabel alloc] init]; // Now numberMarkings[][][...][]
// holds a new uilabel object.

Related

Add label with for loop in different coordinates

I wan't to add labels (strings from an array) onto buttons with a for loop.
I'm new in objective-c and I don't know how I can fit all the changes into the loop on each iteration.
If there is a better way to do this, please show me. Right now I got this, which only prints the second element in the array out at upper right corner.
for (int i=0; i< sizeof(arrayOfLetters); i++ ) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(441,11,30,20)];
label.text = [NSString stringWithFormat:#"%#",[arrayOfLetters objectAtIndex:1]];
[self.view addSubview:label];
}
You are close. You want:
for (int i = 0; i < arrayOfLetters.count; i++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(441, 11 + i * 25, 30, 20)];
label.text = arrayOfLetters[i];
[self.view addSubview:label];
}
You should also give each label a different frame as I did here. Adjust as needed.
Keep in mind that the sizeof function gives you the size of the variable. Since arrayOfLetters is an object pointer the result will probably be 4. You want the actual count of the array. See the docs for NSArray.
Also, do not needlessly use stringWithFormat:. Only use it when you actually have a string that needs formatting.

What is the most efficient way to move NSArray objects to UITextFields?

I have some code where there may or may not be objects in the Array... this is the code I am dealing with:
oServices1.text = CustomServicesArray[0];
oServices2.text = CustomServicesArray[1];
oServices3.text = CustomServicesArray[2];
oServices4.text = CustomServicesArray[3];
oServices5.text = CustomServicesArray[4];
oServices6.text = CustomServicesArray[5];
oServices7.text = CustomServicesArray[6];
oServices8.text = CustomServicesArray[7];
oServices9.text = CustomServicesArray[8];
oServices10.text = CustomServicesArray[9];
oServices11.text = CustomServicesArray[10];
oServices12.text = CustomServicesArray[11];
oServices13.text = CustomServicesArray[12];
oServices14.text = CustomServicesArray[13];
oServices15.text = CustomServicesArray[14];
oServices16.text = CustomServicesArray[15];
oServices17.text = CustomServicesArray[16];
oServices18.text = CustomServicesArray[17];
oServices19.text = CustomServicesArray[18];
oServices20.text = CustomServicesArray[19];
oServices21.text = CustomServicesArray[20];
oServices22.text = CustomServicesArray[21];
oServices23.text = CustomServicesArray[22];
Rather than check each and every array object for nil, is there a way I can take the oServices*xx*.text UIFields and put them into some kind of array so I can just use a loop?
Are you aware of reflexivity? With KVC you could save up much code and time:
for(int i=1; i<=23; i++) {
NSString* key= [NSString stringWithFormat: #"oServices%d"i];
// Remember that variables should start with a lowercase letter
[[self valueForKey: key] setText: customServicesArray[i-1] ];
}
But if you don't want to bind all these variables in your storyboard/xib file (even this may be too much), just set the tag of each text field in the order that you want (from 1), so that you can get them back using viewWithTag:
// From the UIViewController
for(int i=1; i<=23; i++) { // Consider defining a constant instead of 23
[[self.view viewWithTag: i] setText: customServicesArray[i-1] ];
}
I consider this last solution better because you avoid binding so many variables.
You can use an OutletCollection to hold oServices and loop on that. Note however that outlet collections are not sorted so you would need to sort them beforehand (on the tag criteria, or location for example).
For ordering see this question.
Set the tag property of the UITextFields to their corresponding ordinal in the array. The default value of tag is 0, so you may need to set the tag property to ordinal + 1 if there are other views in the parent view of your UITextFields. On the parent view of your text fields, you can use the viewWithTag: method to retrieve the appropriate UITextField.

Programmatically change the state of a UIButton

I've got a pop-up view that loads when a user clicks on a TableView with Core Data elements. On the pop-up view I have a label that represents an int value.
The pop-up view has two butons, one for decreasing the value of the label by 1 and one for increasing it by one. So + and -
What I want to do is to disable the minus button if the label's value is 0. What I've tried is:
-(void)viewDidLayoutSubviews{
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
minus.enabled = NO;
else
minus.enabled = YES
}
The problem with my code is that the button stays disabled after I increase the label's value, and it's no longer equal to 0.
Any suggestions?
You should keep a reference to minus button e.g.
#property (strong, nonatomic) IBOutlet UIButton *minusButton;
Set it with a value of your minus button, or connect outlet in Interface Builder
in your action handler for plusButton, do something like that
-(IBAction)plusAction:(id)sender {
//Do your business logic
...
self.minusButton.enabled = YES;
}
//In your minusButton action handler
-(IBAction)minusAction:(id)sender {
//Do your business logic
...
NSString *daString = currentVal.text;
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:daString];
int number = [myNumber intValue];
if (number==0)
self.minusButton.enabled = NO;
else
self.minusButton.enabled = YES
}
It seems like you have things the other way around. I would take a totally different approach:
Keep an instance variable (which we'll call 'count') in this viewController which holds the number. it can be an NSInteger. now add a target (self) to both buttons with a #selector(buttonPressed:). now this is how this selector should look like:
- (void)buttonPressed:(id)sender{
if (sender==plusButton)
self.count++;
if (sender==minusButton)
self.count--;
label.text = [NSString stringWithFormat:#"%d",self.count];
minusButton.enabled = (self.count>0);
}
I would just do this with a UIStepper, instead of the 2 buttons. You can set properties right in your storyboard/IB file that specify the max and min, increments, and a bunch of other useful things too. There are a couple video tutorials posted on YouTube that probably cover everything you'll need to know to use it.
Also, I have noticed one thing that...
If the button in disabled state and you are trying to change the title of normal state, it wont work.
I had to change the state to enabled and then I could manipulate title and set back to disabled.

Objective C variable counting [duplicate]

This question already has answers here:
Create multiple numbered variables based on a int
(2 answers)
Closed 10 years ago.
I have some variables like vh1 vh2 vh3 etc.
Is it possible in a for-loop to count with the i variable?
I mean something like for(int i = 1; blablabla) { [[vh + i] setBackGroundColor blablabla];}
Regards
Edit: vh1 etc. are UILabels!!!
While this is possible through introspection, if you have such variables you better put them in an NSArray, and access them with an index.
As other answerers have noted, with the new array syntax you can quite easily construct an array with all your objects in it, but it will keep the old values even if you subsequently change the values of the original ivars. That may or may not be what you are after.
If you are hell-bent on keeping your variables as single objects (as opposed to arrays,) then you can use key-value coding to access them programmatically. Key-value coding is also known as KVC.
The method that does it is valueForKey: and can be used both on self and other objects.
MyClass *obj = ... // A reference to the object whose variables you want to access
for (int i = 1; i <= 3; i++) {
NSString *varName = [NSString stringWithFormat: #"var%d", i];
// Instead of id, use the real type of your variables
id value = [obj valueForKey: varName];
// Do what you need with your value
}
There is more about KVC in the docs.
In the interest of completeness, the reason this direct access works, is because a standard KVC compliant object inherits a class method called accessInstanceVariablesDirectly. If you don't want to support this direct access, then you should override accessInstanceVariablesDirectly so it returns NO.
If you are loading the UILabels from XIB, you can use IBOutletCollection.
Declare property:
#property (nonatomic, strong) IBOutletCollection(UILabel) NSArray *labels;
Now you can link multiple labels in XIB to this property. Then in -viewDidLoad (after loading the XIB), your array is populated and you just use simple for-in:
for (UILabel *label in self.labels) {
label.backgroundColor = ...
}
You can access each values the following code.
UILabel *label1;
UILabel *label2;
UILabel *label3;
NSArray *array = #[label1, label2, label3];
for (int i = 0; i<3; i++) {
[array objectAtIndex:i];
}
Adding values to NSArray is available for initializing it.
If you want to add values later, you can use NSMutableArray.
I modified my code.
UILabel *label1 = [[UILabel alloc] init];
UILabel *label2 = [[UILabel alloc] init];
UILabel *label3 = [[UILabel alloc] init];
NSArray *array = #[label1, label2, label3];
for (int i = 0; i<3; i++) {
UILabel *label = [array objectAtIndex:i];
label.frame = CGRectMake(0, i*100, 150, 80);
label.text = [NSString stringWithFormat:#"label%d", i];
[self.view addSubview:label];
}

Using a String representing the name of a variable to set the variable

This is a basic example that I know can be simplified, but for testing sake, I would like to do this in such a way. I want to set a variable based on an appending string (the variables "cam0" and "pos1" are already declared in the class). The appending string would essentially be an index, and i would iterate through a loop to assign cameras (cam0, cam1..) to different positions (pos0, pos1..).
cam0 is defined as an UIImageView
pos1 is defined as a CGRect
This works for a NSString Variable named coverIndex:
NSString* text = [[NSString alloc] initWithString:#""];
NSLog(#"%#",(NSString *)[self performSelector:NSSelectorFromString([text stringByAppendingString:#"coverIndex"])]);
The correct string that I set for coverIndex was logged to the Console.
Now back to my UIImageView and CGRect. I want this to work.
NSString* camText = [[NSString alloc] initWithString:#"cam"];
NSString* posText = [[NSString alloc] initWithString:#"pos"];
[(UIImageView *)[self performSelector:NSSelectorFromString([camText stringByAppendingString:#"0"])] setFrame:(CGRect)[self performSelector:NSSelectorFromString([posText stringByAppendingString:#"1"])]];
My error is "Conversion to non-scalar type requested"
This is the only way I found to do this sort of thing (and get the NSLog to work), but I still believe there is an easier way.
Thank you so much for any help :)
Use KVC, it's an amazing piece of technology that will do just what you want:
for (int index = 0; index < LIMIT; index++) {
NSString *posName = [NSString stringWithFormat:#"pos%d", index];
CGRect pos = [[self valueForKey:posName] CGRectValue];
NSString *camName = [NSString stringWithFormat:#"cam%d", index];
UIImageView *cam = [self valueForKey:camName];
cam.frame = pos;
}
One way you can do this would be to create your cameras in a dictionary and use those special NSStrings to key in to it. Like,
NSMutableDictionary *myCams;
myCams = [[myCams alloc] init];
[myCams addObject:YOUR_CAM0_OBJECT_HERE forKey:#"cam[0]"];
[myCams addObject:YOUR_CAM1_OBJECT_HERE forKey:#"cam[1]"];
NSString camString = #"cam[0]"; // you'd build your string here like you do now
id theCamYouWant = [myCams objectForKey:camString];