Access UIButton using NSString - nsstring

I have a view with 10 buttons each named Q1, Q2... Q10.
In another method I want to change the background image on the buttons however I am creating the button names from a string as such:
NSString *Q = [NSString stringWithFormat:#"Q%d", i];
i is a integer which changes, is there any way I can access the UIButtons using the string Q so that I can then change the background image.

I managed to find a solution in this question by creating an array of labels, like so:
NSMutableArray *buttons = [NSMutableArray arrayWithObjects: Q1, Q2, Q3, Q4, Q5, Q6, Q7, Q8, Q9, Q9, Q10, Q11, Q12, Q13, Q14, Q15, Q16, Q17, Q18, Q19, Q20, nil];
Then accessing the buttons in the array by their index:
[[buttons objectAtIndex:i] setBackgroundImage:image forState:UIControlStateNormal];

Related

Why does this type conversion of NSString to NSNumber returns invalid value of -1?

This is the line of code in question:
bks.quantity = [NSNumber numberWithInteger: [[arrayOfSplitStrings[i] objectAtIndex:[[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue]] intValue]-1];
sd.dictionaryOfUserIndexes objectForKey: #"29" contains a string (representing a quantity) that has to be converted to NSNumber. When the statement is executed with a valid string quantity (0-10) it always returns -1, when it is supposed to return the NSNumber for the value.
What am I doing wrong?
This is not a "straight forward" answer (since the solution is just a silly one), it's more a suggestion on work methods, that's why I post an answer.
It's not always good to put it various lines in a single line.
Especially when in your case you encounter an issue. It's better to split each command, one by one, and to debug, check the value of each ones.
In your case:
bks.quantity = [NSNumber numberWithInteger: [[arrayOfSplitStrings[i] objectAtIndex:[[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue]] intValue]-1];
==>
NSInteger userOfIndexes = [[sd.dictionaryOfUserIndexes objectForKey: #"29"] intValue];
NSLog(#"userOfIndexes: %d", userOfIndexes);
NSInteger n = [arrayOfSplitStrings[i] objectAtIndex:userOfIndexes] intValue];
NSLog(#"n: %d", n);
bks.quantity = [NSNumberWithInteger:n-1];
I added NSLog(), but the values could be check with breakpoints and debugger. I could have also add a check on arrayOfSplitStrings with
NSArray *splitStrings = arrayOfSplitString[i];
NSLog(#"splitStrings: %#", splitStrings);
and replace n with:
NSInteger n = [splitStrings objectAtIndex:userOfIndexes] intValue];
That way, you would have check that apparently (according to your comment), your issue was were to put the "-1.
NSInteger n = [[arrayOfSplitStrings[i] objectAtIndex: userIndex-1] intValue];

Declaring an NSDictionary with an NSString as its name

Ok, so I would like to create an NSDictionary, but the pointer should be followed by the value of an NSString that has been put in by the user. Is this possible? I imagine it would be something along the lines of this...
someNSString = _someTextField.text;
NSDictionary * {someNSString} = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil]
Thanks in advance, I realize that this is either completely not possible or there is a pretty simple solution. Either way, I'm sorry if I wasted your time.
you could try creating a "dictionary of dictionaries". something to the effect of:
someNSString = _someTextField.text;
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setObject:[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:someUnimportantVariable], #"someUnimportantKey",...nil] forKey:someNSString];
and if you wanted to get back that dictionary
NSDictionary someNSStringDictionary = [myDictionary objectForKey:someNSString];
let me know if it helps in any way.

NSDictionary and plist

I have a plist file with string keys and number values. I think I managed to fill an NSDictionary object with the plist contents just fine with the code:
NSBundle* bun = [NSBundle mainBundle];
NSString* path = [bun pathForResource:#"Tempos" ofType:#"plist"];
tempos = [NSDictionary dictionaryWithContentsOfFile:path];
Now, here's the part I don't understand.
id object = [tempos objectForKey:#"Allegro"];
NSLog(#"bpm: %#", object);
Outputs the desired number, 168.
NSInteger beats = (NSInteger)[tempos objectForKey:#"Allegro"];
NSLog(#"bpm: %ld", beats);
Instead outputs, 43203.
More importantly, when I try
bpm = (NSInteger)[tempos objectForKey:#"Allegro"];
I get 43203 assigned to bpm. How do I get 168 assigned to bpm instead??
I think using this should work:
int beats = [[tempos objectForKey:#"Allegro"] intValue];
NSLog(#"bpm: %i", beats);

Unable to get NSEnumerator to display next item in my array

I am new to Objective-C and I have researched this online for weeks now. Almost every example is the same on every site and does not fully make it clear to me how to integrate it into my code for an Xcode 4 App.
The example seen everywhere is:
NSEnumerator* theEnum = [some_array objectEnumerator];
id obj; or id some_object = NULL;
while(obj = [theEnum nextObject]) {
//do something...
I think if I better understood what id some_object = NULL;/ id obj; represents I could figure it out on my own.
In my code I have three arrays. I want to be able to display one object in each array in a UILabel field every time the user clicks the Next button until all of them have been displayed.
NSArray1 = 1,2,3
NSArray2 = John, Jill, Josh
NSArray3 = boy, girl, boy
When the next button is pushed you would see 1, John and boy. The next time you would see 2, Jill and girl and finally 3, Josh and boy.
Below is basic example, not my actually code.
number = [[NSArray alloc] initWithObjects:#"1",#"2",#"3", nil];
name = [[NSArray alloc] initWithObjects:#"John",#"Jill",#"Josh", nil];
gender = [[NSArray alloc] initWithObjects:#"boy",#"girl",#"boy", nil];
NSEnumerator *enum = [number objectEnumerator];
id obj; (??What is this?? And how to connect to the statement below??)
while ((obj = [enumNumber nextObject])) {
self.numberItem.text = ??
self.nameItem.text = ??
self.genderItem.text = ??
Thanks
id obj is just the variable containing the current variable you're looking at. In this code, since your NSArrays all contain string, you could use NSString *obj instead.
An enumerator can only enumerate one collection at a time. If you want to loop through all three of them together, use a traditional for loop:
for(unsigned int i = 0; i < number.count; i++) {
self.numberItem.text = [number objectAtIndex:i];
self.nameItem.text = [name objectAtIndex:i];
self.genderItem.text = [gender objectAtIndex:i];
}

Trim file extension UITableView

I want to trim the file extension from text I have an NSMutableArray in table cells.
NSMutableArray *theFiles = [NSMutableArray new];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *fileList = [manager directoryContentsAtPath:#"/Test"];
for (NSString *s in fileList){
[theFiles addObject:fileList];
}
cell.textLabel.text = theFiles[indexPath.row];
return cell;
This lists for example "Xylophone.m4r" I want to remove the .m4r.
Try -[NSString stringByDeletingPathExtension] (in NSPathUtilities.h).
Actually, for my use, I was able to create a plist programmatically and just not use an extension and it works great! However, anothe rway to do this is:
[string stringByReplacingOccurrencesOfString:#".fileextension" withString:#""];
Just have to delete path extension component:
cell.textLabel.text = [[theFiles objectAtIndex:indexPath.row] stringByDeletingPathExtension];

Resources