Hi Reader’s,
This blog includes the concept of how to set random numbers from an array as title of different UIButton taken in story board. Random numbers are much needed if you are making a number based gaming application where in every new game you need random numbers so that user will think more to won the game. So here you will get to know about fetching random numbers from an array. Screenshot is given below of the storyboard:-
![](https://lh4.googleusercontent.com/-kjCgqMGBweI/V6GDoYegBZI/AAAAAAAAGBk/-GcRqhKrbL8UdIadJufqOIppemhFES5VgCL0B/w598-h624-no/Screen%2BShot%2B2016-08-03%2Bat%2B10.57.03%2BAM.png)
Now coding for the same is given below:-
ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UIButton *btn1;
@property (weak, nonatomic) IBOutlet UIButton *btn2;
@property (weak, nonatomic) IBOutlet UIButton *btn3;
@property (weak, nonatomic) IBOutlet UIButton *btn4;
@property (weak, nonatomic) IBOutlet UIButton *btn5;
@end
ViewController.m
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *displayNumber=[[NSMutableArray alloc]init];
NSMutableArray *numberArray=[[NSMutableArray alloc]init];
for (NSInteger i = 1; i < 6; i++)
{
[displayNumber addObject:[NSNumber numberWithInteger:i]];
}
NSInteger j=displayNumber.count;
for (int i=0; i<j; i++) {
NSInteger index = arc4random() % (NSUInteger)(displayNumber.count); //pick random number index from displayNumber array
[numberArray addObject:[displayNumber objectAtIndex:index]];
[displayNumber removeObjectAtIndex:index];
}
// Set title of button as random number from numberArray(array of random elements)
[_btn1 setTitle:[[numberArray objectAtIndex:0] stringValue] forState:UIControlStateNormal];
[_btn2 setTitle:[[numberArray objectAtIndex:1] stringValue] forState:UIControlStateNormal];
[_btn3 setTitle:[[numberArray objectAtIndex:2] stringValue] forState:UIControlStateNormal];
[_btn4 setTitle:[[numberArray objectAtIndex:3] stringValue] forState:UIControlStateNormal];
[_btn5 setTitle:[[numberArray objectAtIndex:4] stringValue] forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Output:-
Random numbers from array will be display as title screenshot is given below:-
![](https://lh3.googleusercontent.com/-wg-HwLNSaZE/V6GDoVgf0kI/AAAAAAAAGBk/I3kVgXkJmYESL1ETDdz2c0-wA0EgiaVCwCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BAug%2B3%252C%2B2016%252C%2B11.05.03%2BAM.png)
![](https://lh3.googleusercontent.com/-VyMSwaOIIko/V6GDo47x2uI/AAAAAAAAGBk/wuiOZluvTUAzxr2APl67Tu3Pv0GzfvCxACL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BAug%2B3%252C%2B2016%252C%2B11.05.19%2BAM.png)
I hope this blog will help you to understand the concept of random numbers as title of UIButton. You can also download the sample project from the link given below:-
0 Comment(s)