Add multiple NSSlider to view programmatically - objective-c

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.

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 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.

Objective-C/Cocos2D - Display different Sprites with Animation on the same positon on the Screen

Hello i have a big question and i don't find a way to solve my problem till now.
I work on simple game with objective-c and cocos2D.
I have 3 different objects (sprites with animation) and four fixed position on the screen.
Alternately with a interval i want to display the different objects on the positions.
I wanted to do it with a double for() to position the objects. And in the for for i want to create a multidimensional array with all the objects.
And then i want to create a Method where i have access to the time-interval and the frequency of the different objects.
Do you think i can solve my problem with this solution or do you know a better way…??
It would be great if anybody could help me.
Thanky you
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"myPlist.plist"];
myArray = [[CCArray alloc] init];
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 2; j++) {
Figure *figure = [Figure spriteWithSpriteFrameName:#"a0001.png"];
figure.position = ccp(j * figure.contentSize.width + 50, i * figure.contentSize.height + 50);
[myArray addObject: figure];
[self addChild:figure z:1];
}
}
I do not know how you structured your plist, but I imagine that you have renamed the images in this way:
a0001.png
a0002.png
a0003.png
why you don't try to make a more simple for loop instead of a "for inside a for" for place only 3 images at your view and manage your file like this:
for (int i = 1; i < 3; i++) {
Figure *figure = [Figure spriteWithSpriteFrameName:[NSString stringWithFormat:#"a000%i.png", i]]
figure.position = //add something that fit with your layout
[myArray addObject: figure];
[self addChild:figure z:1];
}

UICollectionView Very Slow Custom Layout

I am using a UICollectionView with a custom layout that lays out cells in a grid format. There can be well over 50 rows and 50 columns. Scrolling occurs both vertically and horizontally. Currently, I am doing all of the layout setup in prepareLayout and storing it in arrays:
- (void)prepareLayout {
NSMutableArray *newLayoutInfo = [[NSMutableArray alloc] init];
NSMutableArray *newLinearLayoutInfor = [[NSMutableArray alloc] init];
NSInteger sectionCount = [self.collectionView numberOfSections];
NSIndexPath *indexPath = [NSIndexPath indexPathForItem:0 inSection:0];
self.heightForRows = [delegate collectionViewHeightForAllRows];
self.totalWidthsForRows = [[NSMutableArray alloc] init];
for (int i = 0; i < sectionCount; i++) {
[self.totalWidthsForRows addObject:[NSNumber numberWithInt:0]];
}
for (NSInteger section = 0; section < sectionCount; section++) {
NSMutableArray *cellLayoutInfo = [[NSMutableArray alloc] init];
NSInteger itemCount = [self.collectionView numberOfItemsInSection:section];
for (NSInteger item = 0; item < itemCount; item++) {
indexPath = [NSIndexPath indexPathForItem:item inSection:section];
UICollectionViewLayoutAttributes *itemAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
itemAttributes.frame = [self frameForCellAtIndexPath:indexPath];
[cellLayoutInfo addObject:itemAttributes];
[newLinearLayoutInfor addObject:itemAttributes];
}
[newLayoutInfo addObject:cellLayoutInfo];
}
self.layoutInfo = newLayoutInfo;
self.linearLayoutInfo = newLinearLayoutInfor;
}
Then in layoutAttributesForElementsInRect I have:
- (NSArray*)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray *rows = [self.linearLayoutInfo filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(UICollectionViewLayoutAttributes *evaluatedObject, NSDictionary *bindings) {
return CGRectIntersectsRect(rect, [evaluatedObject frame]);
}]];
This works okay, but it is laggy and jumpy when I have over 50 columns and 50 rows. The problem I now have is that I must set
-(BOOL)shouldInvalidateLayoutForBoundsChange {
return YES;
}
This makes it prepare the entire layout every time the bounds change, which, needless to say, has a huge impact on performance and you can barely scroll. The cells consist of just text with an opaque background, so there is no issue there.
I am sure I am not doing this right and that there must be a better way. Thanks for the help in advance.
In custom flow layout I do this and it seems to help:
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return !(CGSizeEqualToSize(newBounds.size, self.collectionView.frame.size));
}
-(BOOL)shouldInvalidateLayoutForBoundsChange {
return YES;
}
Causes the layout to do prepareLayout() every time it scrolls, which means anything of heavy computing in prepare will lead to a laggy practice, so one possible direction to solve this is to check what's really taking much time. One possibility is what's inside
for (NSInteger section = 0; section < sectionCount; section++)
{
// generate attributes ...
}
in order to generate attributes for the layout. Every time it scrolls, every time this generalization reruns, so that it impacts on the scroll appear to be jumpy and clumsy. So in order to solve this issue, or at least sort out that this is not the really trouble, I suggest setting a flag in this layout algorithm, say, isScrolling, standing for the situation where the layout needs to prepare. Every time in prepareLayout() check the flag, if it is YES, then we'll know there's no need to do for loop to regenerate all the attributes, which alreay exsit ever since the first time the layout is initialised.
ok--I understand now. Here's what I recommend: create 3 collection views... one for the column headers (where each cell is column header), one for the row leaders (each cell = 1 row leader) and one collection view for your cells. Then when the scroll position of any collection view is changed by the user, update the scroll positions for the other 2 collection views as appropriate.

Populate NSMutableArray with CCSprites via for loop

Hey guys, using obj-c and the cocos2d framework;
How would I populate an NSMutableArray with CCSprite's with a for-loop to reduce code.
I have +100 sprites/images that need to go into this array, so a for-loop is necessary.
This is my current code:
_backgrounds = [[NSMutableArray alloc]initWithCapacity:31];
for (int i = 31; i > 1; i--){
[_backgrounds addObject:
[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:
[NSString stringWithFormat:#"background_%d.png", i]]];
}
Im looking for something along the lines of this: (Although this code doesnt work),
for (int i = 31; i > 1; i--){
[backgrounds addObject:[CCSprite spriteWithFile:#"background%d.png", i]];
}
Thanks in advanced,
Oliver.
Chances are your shared CCSpriteFrameCache does not contain any values. You must populate it prior to accessing anything from it.