Reputation: 41
Hello all! I am very new to xcode and objective-c but I have been programming in C# for a long time. I am just starting out with Xcode and I have been tasked with making an app and I am lost!
I have 5 UILabels and an array with 5 values, I would like the UILabels to randomly load a value from the array until all values from the array are used.
I can make Dynamic Labels but its a pain to get them lined up correctly which I will do if needed, but I am not sure how to do that with objective-c(In C# its easy).
Thanks!
Upvotes: 0
Views: 5144
Reputation: 166
From what I can understand, you have an array and want with 5 strings and want to randomly display then in a label? I think you could do like this:
-(void)fillLabel {
int random = arc4random() % 5; //random value from 0-4
yourLabel.text = [NSString stringWithFormat:@"%@",[yourArray objectAtIndex:random]];
}
you can call that with
[self fillLabel];
if you are in the same class.
Upvotes: 4
Reputation: 3258
Hey there and welcome to SO, this is a great place to help you learn XCode and Objective-C.
There is a couple ways that you could do this, so I'll just give you one example solution.
You can actually create your labels in your NIB(.xib) file, and then link them to variables in your code, that way you can easily access them without having to create them programatically. The way that you would do this is as follows:
ViewController.h
@interface ViewController : UIViewController
{
//Some Variables
}
@property (nonatomic, strong) NSMutableArray *stringValues;
@property (nonatomic, strong) IBOutlet UILabel *randLabel1;
@property (nonatomic, strong) IBOutlet UILabel *randLabel2;
@property (nonatomic, strong) IBOutlet UILabel *randLabel3;
@property (nonatomic, strong) IBOutlet UILabel *randLabel4;
@property (nonatomic, strong) IBOutlet UILabel *randLabel5;
The key here is that the labels are declared is IBOutlets in your .h file. This allows you to then go into your .xib file and link the labels you've positioned to the variables in the code. To link a label, press and hold your control key, and then drag it to your viewcontroller and release, you should see a menu that lists your 5 labels and then you can select the appropriate one.
As for then selecting a random string to load into the label, you could that in the following way.
ViewController.m
-(void)selectLabelValue:
{
for(int i = 5; i > 0; i--)
{
randIdx = arc4random() % i;
switch (i):
case 1:
Label1.text = [stringValue objectAtIndex:randIdx];
[stringValue removeObjectAtIndex:randIdx];
case 2:
Label2.text = [stringValue objectAtIndex:randIdx];
[stringValue removeObjectAtIndex:randIdx];
case 3:
Label3.text = [stringValue objectAtIndex:randIdx];
[stringValue removeObjectAtIndex:randIdx];
case 4:
Label4.text = [stringValue objectAtIndex:randIdx];
[stringValue removeObjectAtIndex:randIdx];
case 5:
Label5.text = [stringValue objectAtIndex:randIdx];
[stringValue removeObjectAtIndex:randIdx];
default:
NSLog(@"Error, index out of bounds, there is no label for this value!";
}
The idea with the above is that you iterate through your 5 labels, and as you do you randomly generate an index to select your string to load. After you load the string you remove it from the possible strings so that you dont get duplicates. I would double check the function names for the array manipulation because I did those off of the top of my head, so I'm not 100% sure they are correct, but something that does what they should do does exist.
Anyways, I hope that helps, if you have questions leave a comment and I'll do my best to answer it.
EDIT: Just in case someone is skimming and doesn't read the comment by Richard. He brings up a valid point that using a switch statement in the above was is bad design, and should be handled using an array, while programatically creating the labels. This way you can easily just select the label you want via the array index. It is a significantly cleaner solution, and preferable to the above. The only reason it was done with switches is that this way you don't have to create the labels through code which was the preference of user1221399, so keep this in mind.
Upvotes: 1