10-04-2011, 08:34 PM
#1 (permalink )
Registered Member
Join Date: Aug 2011
Posts: 12
NSArray Troubles
Hi everyone,
I'm having trouble initializing an array, populating a UITextView with an object from the array (all of the objects are NSStrings) and then being able to switch which object is displayed with a next and a previous button. Here is my code:
The header file:
Code:
#import <UIKit/UIKit.h>
@interface WordsPage : UIViewController {
IBOutlet UITextView *wordsText;
IBOutlet UIButton *prevButton;
IBOutlet UIButton *nextButton;
NSArray *WordsArray;
}
@property (retain, nonatomic) UITextView *wordsText;
@property (retain, nonatomic) UIButton *prevButton;
@property (retain, nonatomic) UIButton *nextButton;
@property (retain, nonatomic) NSArray *WordsArray;
-(IBAction)nextButton:(id)sender;
-(IBAction)prevButton:(id)sender;
-(void)updateText;
@end
And my implemenation file:
Code:
#import "WordsPage.h"
@implementation WordsPage
@synthesize wordsText;
@synthesize prevButton;
@synthesize nextButton;
@synthesize WordsArray;
int num;
NSString *words1 = @"Words string 1";
NSString *words2 = @"Words string 2";
NSString *words3 = @"Words string 3";
NSString *words4 = @"Words string 4";
NSString *words5 = @"Words string 5";
NSString *words6 = @"Words string 6";
//ERROR Initializer element is not a compile-time constant
static NSArray *WordsArray = [[NSArray alloc] initWithObjects:
words1,
words2,
words3,
words4,
words5,
words6, nil]; //ERROR Initializer element is not a constant
-(IBAction)prevButton:(id)sender {
num--;
if (num<0) num=5;
[self updateText];
}
-(IBAction)nextButton:(id)sender {
num++;
if (num>5) num=0;
[self updateText];
}
-(void)updateText {
wordsText.text = [WordsArray objectAtIndex:num];
}
- (void)viewDidLoad {
int num = 0;
wordsText.text = [WordsArray objectAtIndex:num];
self.navigationItem.title=@"Words Page ";
[super viewDidLoad];
}
-(void)dealloc {
[wordsText release];
[favButton release];
[nonfavButton release];
[WordsArray release];
[super dealloc];
}
@end
Any ideas of what I am doing wrong?
Thanks!
-Andrew
10-04-2011, 08:51 PM
#2 (permalink )
Cocoa Junkie
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Quote:
Originally Posted by
Peak Mobile
Hi everyone,
I'm having trouble initializing an array, populating a UITextView with an object from the array (all of the objects are NSStrings) and then being able to switch which object is displayed with a next and a previous button. Here is my code:
The header file:
Code:
#import <UIKit/UIKit.h>
@interface WordsPage : UIViewController {
IBOutlet UITextView *wordsText;
IBOutlet UIButton *prevButton;
IBOutlet UIButton *nextButton;
NSArray *WordsArray;
}
@property (retain, nonatomic) UITextView *wordsText;
@property (retain, nonatomic) UIButton *prevButton;
@property (retain, nonatomic) UIButton *nextButton;
@property (retain, nonatomic) NSArray *WordsArray;
-(IBAction)nextButton:(id)sender;
-(IBAction)prevButton:(id)sender;
-(void)updateText;
@end
And my implemenation file:
Code:
#import "WordsPage.h"
@implementation WordsPage
@synthesize wordsText;
@synthesize prevButton;
@synthesize nextButton;
@synthesize WordsArray;
int num;
NSString *words1 = @"Words string 1";
NSString *words2 = @"Words string 2";
NSString *words3 = @"Words string 3";
NSString *words4 = @"Words string 4";
NSString *words5 = @"Words string 5";
NSString *words6 = @"Words string 6";
//ERROR Initializer element is not a compile-time constant
static NSArray *WordsArray = [[NSArray alloc] initWithObjects:
words1,
words2,
words3,
words4,
words5,
words6, nil]; //ERROR Initializer element is not a constant
-(IBAction)prevButton:(id)sender {
num--;
if (num<0) num=5;
[self updateText];
}
-(IBAction)nextButton:(id)sender {
num++;
if (num>5) num=0;
[self updateText];
}
-(void)updateText {
wordsText.text = [WordsArray objectAtIndex:num];
}
- (void)viewDidLoad {
int num = 0;
wordsText.text = [WordsArray objectAtIndex:num];
self.navigationItem.title=@"Words Page ";
[super viewDidLoad];
}
-(void)dealloc {
[wordsText release];
[favButton release];
[nonfavButton release];
[WordsArray release];
[super dealloc];
}
@end
Any ideas of what I am doing wrong?
Thanks!
-Andrew
The values assigned to static variables have to be constants.
Make wordsArray a retained property, not a static var, and initialize it in your init method.
use code like this:
Code:
self.wordsArray = [NSArray arrayWithObjects:
words1,
words2,
words3,
words4,
words5,
words6,
nil];
and make sure to set self.wordsArray to nil in your dealloc method.
10-04-2011, 09:25 PM
#3 (permalink )
Registered Member
Join Date: Aug 2011
Posts: 12
Aren't I retaining the array from the @property line of code in the header file? How do I retain it from the implementation file?
And can you explain the init method? I have not seen that before. Or are you refering to the ViewDidLoad method?
Thanks,
Andrew
Last edited by Peak Mobile; 10-04-2011 at 09:51 PM .
10-04-2011, 10:24 PM
#4 (permalink )
Just helping out.
Join Date: Feb 2011
Posts: 2,565
1. No, you aren't using your property. You are using whole entirely different variable and to top it off, you made it a constant.
2. The init method he posted does the exact same thing as initWithObjects: except it returns an autoreleased object instead of one with a retain count of 1.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.
New app - See screenshots and details at
www.globaclock.com .
If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
10-22-2011, 08:53 AM
#5 (permalink )
Registered Member
Join Date: Aug 2011
Posts: 12
It took me forever, but I finally got it working! Thank you for your help, guys!
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» Advertisements
» Online Users: 395
17 members and 378 guests
13dario13 , 7twenty7 , eski , EvilElf , glenn_sayers , HemiMG , iOS.Lover , jarv , LunarMoon , n00b , pbart , Pudding , sacha1996 , Sami Gh , UMAD , VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,672
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, yuncarl28