Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:40:09+00:00 2026-06-13T19:40:09+00:00

In my view, i have 12 buttons,and a array contains 6 names , i

  • 0

In my view, i have 12 buttons,and a array contains 6 names , i want to print the array names in UIButton title. Here’s my code:

texts = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",nil];
UIButton *button;
NSString *name;
NSUInteger count = [texts count];
int i=0;

for(UIView *view in self.view.subviews)
{
    if([view isKindOfClass:[UIButton class]])
    {
        button= (UIButton *)view;
        if(button.tag >= 1||button.tag <= 20)
        {
            int value = rand() % ([texts count] -1) ;
            int myTag= i+1;
            button = [self.view viewWithTag:myTag];
            name=[NSString stringWithFormat:@"%@",[texts objectAtIndex:value]];
            [button setTitle:name forState:UIControlStateNormal];
            NSLog(@"current  name :%@",name);
        }
        i++;
    }

}
[super viewDidLoad];

The problems I am facing are:

  1. While shuffling the values are repeating,i tried with What's the Best Way to Shuffle an NSMutableArray?, its not working.

  2. I want 6 titles in 12 button that means each title will be in 2 buttons. Please help me solve this issue. What changes should I make?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:40:10+00:00Added an answer on June 13, 2026 at 7:40 pm

    Basically you need to separate the shuffling logic from adding name to button feature. So first shuffle the array and then set the name of buttons.

    [super viewDidLoad]; //Always super call should be the first call in viewDidLoad
    texts = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6", @"1",@"2",@"3",@"4",@"5",@"6", nil];
    //adding all 12 button titles to array, use your own logic to create this array with 12 elements if you have only 6 elements
    
    NSUInteger count = [texts count];
    
    for (NSUInteger i = 0; i < count; ++i) {
            // Select a random element between i and end of array to swap with.
            NSInteger nElements = count - i;
            NSInteger n = (arc4random() % nElements) + i;
            [texts exchangeObjectAtIndex:i withObjectAtIndex:n];
    }
    
    UIButton *button = nil;
    NSString *name = nil;
    int i = 0;
    
    for(UIView *view in self.view.subviews)
    {
        if([view isKindOfClass:[UIButton class]])
        {
            button= (UIButton *)view;
            if(button.tag >= 1 && button.tag <= 20)
            {
                name = [NSString stringWithFormat:@"%@",[texts objectAtIndex:i]];
                //assuming that the above texts array count and number of buttons are the same, or else this could crash
                [button setTitle:name forState:UIControlStateNormal];
                NSLog(@"current  name :%@",name);
                i++;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an alert view with two buttons using the following code: UIAlertView
I have a FrameLayout view which contains one (MapView-like) control and some additional buttons
I have a document-based application with a view controller that contains a table, array
I have an array of buttons. Now, when each button is clicked, I want
I want to have user select between two different buttons on a view, If
On my view, I used to have few buttons and each button had an
I am creating a split view ipad app. I have four buttons in the
I have an view in my App which has a number of buttons based
I have a list view in jQuery Mobile with delete buttons in them. <ul
I have a simple Backbone view which uses jQuery UI's buttons. For example I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.