5 Replies - 3523 Views - Last Post: 26 December 2011 - 06:55 AM

#1 ColdAsIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 11

How to change titles of many buttons in ObjectiveC with a loop?

Posted 25 December 2011 - 06:27 PM

Hi!

My Problem: I have 50 Buttons and I want to change all the titles of them in a loop. I named them like this: button1, button2, button3...

I wrote this code (i made it simpler here so that you can see my problem easier):

  int i = 0;
  while (i<=50) {
      button[i].title = @"hello";
      i++;
  }




I don't know how to do this right. Its obvious that this cannot compile cause I have a string in front of .title and there has to be a NSButton type but I don't know how to do this right.
I would be really thankful for some help and I also would be thankful if someone can give me a tip where to find answers to ObjectiveC questions. I searched for 2 days to solve this problem but I didn't find anything about it on the web (maybe cause I didn't really know what I am searching for...).

Is This A Good Question/Topic? 0
  • +

Replies To: How to change titles of many buttons in ObjectiveC with a loop?

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: How to change titles of many buttons in ObjectiveC with a loop?

Posted 25 December 2011 - 07:59 PM

I'm not sure about this but I believe you want something like:
[[button[i]] setTitle:@"hello"];

Was This Post Helpful? 0
  • +
  • -

#3 ColdAsIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 11

Re: How to change titles of many buttons in ObjectiveC with a loop?

Posted 25 December 2011 - 08:35 PM

View PostCTphpnwb, on 25 December 2011 - 07:59 PM, said:

I'm not sure about this but I believe you want something like:
[[button[i]] setTitle:@"hello"];


Ok maybe I have to be more specific:
I have about 500 Buttons and I want to set the title of button1 to 1, the title of button2 to 2 etc.

And I want to do this with a loop like this:
    int i = 1;
    while (i<=500) {
        button[i].title = [NSString stringWithFormat: @"%d", i];             //here the compiler says: Use of undeclared identifier "button"
        i++;
    }



When I try it like this the compiler says: Use of undeclared identifier "button".

The code you wrote makes no sense to me and it doesn't work, too.
But nevertheless I am thankful that you try to help me.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2911
  • View blog
  • Posts: 10,083
  • Joined: 08-August 08

Re: How to change titles of many buttons in ObjectiveC with a loop?

Posted 25 December 2011 - 09:48 PM

This compiles for me:
    NSButton *button[10];
    for (int i = 0; i < 10; i++) {
        [button[i] setTitle:[NSString stringWithFormat: @"Title: %d", i]];
    }


Was This Post Helpful? 0
  • +
  • -

#5 ColdAsIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 11

Re: How to change titles of many buttons in ObjectiveC with a loop?

Posted 26 December 2011 - 06:19 AM

Ok I tested it but your code doesn't work for me (it compiles but when I press the button with your code in it, the program crashes.

I tested it after that without a loop but nevertheless the program crashed::
 int i = 10;
 NSButton *button[12];
 [button[i] setTitle:[NSString stringWithFormat: @"%d", i]];          //in this line it says "Thread 1: Program received signal: EXC_BAD_ACCESS"



After that I tested it like this::
int i = 10;
 [button10 setTitle:[NSString stringWithFormat: @"%d", i]]; 



This works fine, so the problem is "button[i]". I don't know why this doesn't work. I hope someone of you knows it and can tell me how to do it right.
Was This Post Helpful? 0
  • +
  • -

#6 ColdAsIce  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-December 11

Re: How to change titles of many buttons in ObjectiveC with a loop?

Posted 26 December 2011 - 06:55 AM

I finally got what I wanted. I used an array to do it:

    NSArray *buttons = [NSArray arrayWithObjects:button1,button2,button3, nil];
    NSButton *currentButton = [buttons objectAtIndex: 1];
 
    for (int i = 0; i < 4; i++) {
        currentButton = [buttons objectAtIndex:i];
        currentButton.title = [NSString stringWithFormat: @"%d", i];
    }



Thanks a lot for the help CTphpnwb!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1