views:

109

answers:

2

I'm new to Obj-C and iPhone SDK. The test application I'm stock with is a color switcher containing two buttons ("Back", "Forward") and one text label. The idea is to switch between rainbow colors (background) and setting an appropriate text label in a cyclic manner.

I declared NSArray (which is to contain colors names) in RainbowViewController.h, synthesized it in RainbowViewController.h and I can't add any string into that array.

This is "h" file:

#import <UIKit/UIKit.h>

@interface RainbowViewController : UIViewController {
    IBOutlet UILabel *currentColorTextLabel;
    NSArray *colorsArray;
    NSString *msg;
}

@property (nonatomic, retain) IBOutlet UILabel *currentColorTextLabel;
@property (nonatomic, retain) NSArray *colorsArray;
@property (nonatomic, retain) NSString *msg; 

- (IBAction) pressForwardButton;
- (IBAction) pressBackButton;

@end

This is "m" file:

#import "RainbowViewController.h"
#import <Foundation/Foundation.h>

@implementation RainbowViewController

@synthesize currentColorTextLabel;
@synthesize colorsArray;
@synthesize msg;

int currentArrayIndex = 0;

colorsArray = [[NSArray alloc] init]; //here i get "Initializer element is not constant" error message
[coloursArray addObject:@"Red"]; //here I get "Expected identifier or '(' before '[' token"
[coloursArray addObject:@"Orange"];
//etc

- (IBAction) pressForwardButton {
    //here I'm going to increment currentArrayIndex, set an appropriate color, and update a currentColorTextLabel based on currentArrayIndex.
}

- (IBAction) pressBackButton {

}

//auto-genereted code here
@end
+1  A: 

You have the code that should go in your init method just sitting out in the middle of the file. You can't set instance variables like that.

Chuck
+1  A: 

I'm new to obj-c as well, but I think you need to initialize the array with objects, or use an NSMutableArray if you want to add objects after it is created.

jasongetsdown