Add label with for loop in different coordinates - objective-c

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.

Related

Changing UIImageView picture which name stored in a string

Hi there i have image views in my application to change pictures in them. but their name like imageview1, imageview2, and so.. i create a string which like:
`("imageview%i", number)`
so my string is imageview1 for example. and i need to change
self."**mystring**".image = [uiimage ...]
I looked key-value coding but i couldn't get it exactly. i searched the forum and i can't get anything either. what could i do to resolve this. i think i must do an array with my uiimageviews inside of it. than compare their name with my string (i didn't know how can i get property names as nsstring). then return that image view. Please help.
If you want to do that in runtime, either hack your way using selectors or use tags on your UIImageViews. They are not really good programming concepts, but they work. Examples:
-(void)usingSelectors{
for(int i=0; i < 4; i++){
NSString* propName = [NSString stringWithFormat:#"imageview%d", i];
UIImageView* imageView = [self performSelector:NSSelectorFromString(propName)];
[imageView setImage:/*your image*/];
}
}
-(void)usingTags{
for(int i=0; i < 4; i++){
UIImageView* imageView = [self.view viewWithTag:i];
[imageView setImage:/*your image*/];
}
}
If you want to do that in compile time you'll have to use preprocessor macros, but I don't think that's your case.

Add multiple NSSlider to view programmatically

I'm new to objective C and Cocoa, but I have managed to add a slider into some views etc. I got that.
I want to add multiple sliders with code, and I tried it with a for loop and a array. It didnt work, but here is the Code I tried:
NSMutableArray* myarray;
for (int i = 0; i<5; i = i+30) {
[myarray addObject:[[NSSlider alloc] initWithFrame:NSMakeRect(15+i, 15, 30, 200)]];
}
for (int i=0; i<5; i++) {
[firstView addSubview:[myarray objectAtIndex:i]];
NSLog(#"%d",i);
}
(wanted to show them 30 apart, but doesnt matter it didnt work. Then I tried this if its even working "through" an array
[myarray addObject:[[NSSlider alloc] initWithFrame:NSMakeRect(15, 15, 30, 200)]];
[firstView addSubview:[myarray objectAtIndex:0]];
I tried to show only one slider. Doesnt work either.
But this works:
[firstView addSubview:[[NSSlider alloc] initWithFrame:NSMakeRect(45, 15, 30, 200)]];
Why? And what is the right way to add multiple sliders/buttons/whatever doesnt really matter to add to a view dynamically? The real Idea is to get Data from CoreData and for each entry in CoreData it should Display a slider. (But thats far away)
I need some tips and tricks! Thanks in advance guys!
BTW when I try to init the array this way:
NSMutableArray* myarray = [[NSMutableArray alloc] init];
the views dont load (firstView especially because this view is black)
Your first for statement is totally wrong:
for (int i = 0; i<5; i = i+30)
It will enter the loop only once.
That's probably why you're not getting all the sliders you want.

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

How to change the font size in my textview

I am using a textview in which I have passed an array of objects. In that array I have taken a list of messages that would be passed to the textview, but I have changed the size of textview font so. give any suggestion and source code which is apply in my application.
Below my code in that I have apply the UIFont but that is not use in font.
msgtxtView.text=selectedmsg;
[msgtxtView setFont:[UIFont fontWithName:#"Arial" size:10]]
selectedmsg is array of object in that message list that would be pass to textview's object but output font size and arialfont not work give any.
Use this :
msgtxtView.text=selectedmsg;
[msgtxtView setFont:[UIFont fontWithName:#"ArialMT" size:10]]
Look at his example how to add text from array and change size (You changing size correctly):
NSMutableArray *array = [NSMutableArray arrayWithObjects:#"message1", #"message2", #"message3", #"message4", #"message5", nil];
for (int i=0; i<[array count]; i++) {
msgtxtView.text = [NSString stringWithFormat:#"%#%#\n",msgtxtView.text,[array objectAtIndex:i]];
}
[msgtxtView setFont:[UIFont fontWithName:#"Arial" size:10]];
Don't forget to IBOutlet msgtxtView.
Result:
And for example using this: [UIFont fontWithName:#"Chalkduster" size:25]:

Using a variable inside a variable objective-C

EDIT: {
No longer need help!
I used tags and a bunch of loops to reference them at anytime.
I never knew you could store so many images in one UIImageView!
}
I have an application that deals with a a lot of images and what i want to do is use an integer inside the variable name so i don't have to write code for each image. Ex:
- (void)addIconClicked {
if (icons < 28) {
icons += 1;
}
if (icons == 2) {
UIImageView * iconImage2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon1.png"]];
//modify image view like setting the frame, setting the title, etc... (not important)
}
else if (icons == 3) {
// iconImage 3 set up
}
this continues all the way up to iconImage26!
so i was wondering if i could use the integer "icons" as part of the variable name so i don't have to run the code 26 different times!!
Ex:
- (void)addIconClicked {
if (icons < 28) {
icons += 1;
}
/*some how insert the "icons" int where (icons) is. Like NSString uses
stringWithFormat ([NSString stringWithFormat:#"%i", icons])*/
UIImageView * iconImage(icons) = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:#"icon%i.png", icons]]];
}
-----edit----- (to clarify what i want)
my problem is that i want to make an unlimited amount of image views but if i use the same variable more than once, it would show up on the view fine, but it would be released and i can't edit it anymore.
Ex:
- (void)addIconClicked {
if (icons < 28) {
icons += 1;
}
NSString * iconName = [NSString stringWithFormat:#"icon%i.png", icons];
//if i ran this 100 times, 100 images would show up, but i can no longer edit any of them except the newest.
iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
}
so i need to know how to make an unlimited amount of images that i can still edit individually.
i thought i might be able to use the integer's value inside a variable name when i create it, but i guess i can't:(
so if anyone knows, please explain!!
so to sum it up...
my exact problem is that i want to create an unlimited number of UIImageView's using a different image each time.
You think i could just use 1 variable for all of the images (which would show up) but then i can't edit them at all because they are released.
I just need a way to create an unlimited amount of Global UIImageViews that i can edit and access at any time.
The best way to do this is with an array.
NSMutableArray* allIcons = [NSMutableArray array]; //First, you'll need this empty array.
//Then, when you create your icon...
NSString* iconName = [NSString stringWithFormat:#"icon%lx.png",icons];
UIImageView * newIcon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:iconName]];
[allIcons addObject:newIcon]; //This adds the icon at the next index, starting at 0.
Then, when you use your icons, instead of icon1, icon2, and so on, use [allIcons objectAtIndex:0], [allIcons objectAtIndex:1], and so on.
This is a pretty common concept--you should consider checking out a few beginning Cocoa tutorials; you might find it in use.
You can use an array for storing UIImageView references. Or you can set their tags and find them later using viewWithTag.
EDIT: Ok I am home now and I can explain:
Use tagging:
UIImageView * anImageView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat#"icon%i.png",icons]]];
anImageView.tag=icons;
[parentView addSubview: anImageView];
UIImageView *theOneYouWant = (UIImageView)[parentView viewWithTag:someTag];
or use a c-array of UIImageView pointers to store references if you don't mind scope:
UIImageView * imageViews[28]; // put this into class interface
imageViews[icons]= [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat#"icon%i.png",icons]]];