Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to set random numbers from an array as title of UIButton in iOS?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 338
    Comment on it

     

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

     

     

     

     


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

     

     

     

     

     

     

     

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: