开发者

How to assign existing buttons in an Nview to an Array

开发者 https://www.devze.com 2023-01-31 10:59 出处:网络
I need help to make an Array, in xcode, to collect 25 existing buttons in a Window(NView) for, for example, change background colors or titles of others buttons when one button is pressed.

I need help to make an Array, in xcode, to collect 25 existing buttons in a Window(NView) for, for example, change background colors or titles of others buttons when one button is pressed.

Thanks.

This is the Code:

Botones.h

#import <Cocoa/Cocoa.h>

extern const NSString* Nivel[];
extern const NSString* Ayuda[];
extern const NSString* Boleo[];
BOOL Estado[5][5];
int Columna;
int Fila;
//NSColor* Rojo = (NSColor *)redColor;

@interface Botones : NSObject {
    IBOutlet id Intents;
    IBOutlet id Nivel;
    IBOutlet id BoxBot;
    NSArray *Arbot;
}

- (IBAction)OrBoton:(id)sender;

Botones.m

#import "Botones.h"

const NSString *Nivel[] = {@"101214",@"0002040507091517192202224",@"01030506080910111314151618192123"};
BOOL Estado[5][5] = {FALSE};
int Columna = 0;
int Fila = 0;


@implementation Botones
- (void)awakeFromNib {
Arbot = [[开发者_Python百科BoxBot subviews] copy];
}

- (IBAction)OrBoton:(id)sender {
    [Intents setIntValue:[sender tag]]; 
    Columna =[sender tag] % 5;
    Fila = [sender tag] / 5;
    if (Estado[Columna][Fila] == FALSE) {
        Estado[Columna][Fila] = TRUE;
        [sender setTitle:@"OK"];
        //[sender setBackgroundColor:[NSColor redColor]];
         }
    else {
        Estado[Columna][Fila] = FALSE;
        [sender setTitle:@""];
        //[sender setBackgroundColor:[NSColor greenColor]];

    }
    if (Fila > 0) {   
        if (Estado[Columna][Fila - 1] == FALSE) {
            Estado[Columna][Fila - 1] = TRUE;
            NSLog(@"%@",[BoxBot subviews]);
            [[Arbot objectAtIndex:5] setTitle:@"OK"];// <-- Don't changes button's title                
    }
}
}   


If the buttons are the only subviews in the view, you can use this:

//view is a variable pointing to the view containing the buttons
NSArray *buttons = [[view subviews] copy];

If there are other views in the view, you will have to filter them out:

//view is a variable pointing to the view containing the buttons
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:25];
for(NSView *v in [view subviews]) {
    if([v isKindOfClass:[NSButton class]]) [buttons addObject:v];
}

Edit

...
@interface Botones : NSObject {
    IBOutlet id Intents;
    IBOutlet id Nivel;
    IBOutlet id BoxBot;
    NSArray *ArBot;
}
...

Botones.m

...
@implementation Botones
- (void)awakeFromNib {
    ArBot = [[BoxBot subviews] copy];
    /*or
    NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:25];
    for(NSView *v in [BoxBot subviews]) {
        if([v isKindOfClass:[NSButton class]]) [array addObject:v];
    }
    ArBot = [array copy];
    [array release];*/
}
...
0

精彩评论

暂无评论...
验证码 换一张
取 消