Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-11-2011, 02:59 AM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 4
jhyde01 is on a distinguished road
Smile Using a array from another class

Hello. I need some help passing an array to another class for use. To keep it simple ill only give 2 of my classes, but just in case more coding is needed, this array is going to be used for around 50 classes.

So i have HowToPlay.h (which of course has a .m) the array is set up in HowToPlay.

Then i also have another class called Question 1.h (of course has a .m). This class is a subClass of HowToPlay, and i have also imported HowToPlay into this class.

Now let me explain a bit about this array. This array is being used to select a random NIB file, and then if the NIB has already been used before it deletes it from the array.

The array itself looks like this (this is in HowToPlay)

Code:
nibs = [[NSMutableArray alloc]initWithObjects:@"Question 2", @"Question 3", nil];
	self.unusedNibs = nibs;
	[nibs release];
Hopefully your not confused when you see the Question 2 and Question 3, just ignore them, their irrelevant.

Now lets jump into Question 1,

Inside Question 1 I have a IBAction that uses this array, it looks something like this

Code:
-(IBAction)continueAction:(id)sender{

	random = arc4random() % [self.unusedNibs count];
	NSString *nibName = [self.unusedNibs objectAtIndex: random];
	[self.unusedNibs removeObjectAtIndex: random];


	if (nibName == @"Question 3") {

	Question_3 *Q3 = [[Question_3 alloc] initWithNibName:@"Question 3" bundle:nil];
	
	Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:Q3 animated:YES];
	[Q3 release];
	}

	if (nibName == @"Question 2") {

	Question_2 *Q2 = [[Question_2 alloc] initWithNibName:@"Question 2" bundle:nil];
	
	Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:Q2 animated:YES];
	[Q2 release];
	}
}
Again the Question 2 and Question 3 parts of this code are irrelevant. So as you can see the code picks a random NIB to load, then deletes it from the array. The problem is, its not deleting it from the array.

I think the array isnt be passed to the other class. Any Ideas?
The app runs fine, but it still loads those NIBs that should have been deleted out of the array.

Thanks,
Jacob

Last edited by jhyde01; 07-11-2011 at 03:02 AM.
jhyde01 is offline   Reply With Quote
Old 07-11-2011, 05:12 AM   #2 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

I haven't read all, however the first thing that is not ok is for sure that
Code:
if (nibName == @"Question 3") {
should be
Code:
if ([nibName isEqualToString:@"Question 3"]) {
because == in an object compare pointers
__________________
dany_dev is offline   Reply With Quote
Old 07-11-2011, 10:51 AM   #3 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by jhyde01 View Post
Hello. I need some help passing an array to another class for use. To keep it simple ill only give 2 of my classes, but just in case more coding is needed, this array is going to be used for around 50 classes.

So i have HowToPlay.h (which of course has a .m) the array is set up in HowToPlay.

Then i also have another class called Question 1.h (of course has a .m). This class is a subClass of HowToPlay, and i have also imported HowToPlay into this class.

Now let me explain a bit about this array. This array is being used to select a random NIB file, and then if the NIB has already been used before it deletes it from the array.

The array itself looks like this (this is in HowToPlay)

Code:
nibs = [[NSMutableArray alloc]initWithObjects:@"Question 2", @"Question 3", nil];
	self.unusedNibs = nibs;
	[nibs release];
Hopefully your not confused when you see the Question 2 and Question 3, just ignore them, their irrelevant.

Now lets jump into Question 1,

Inside Question 1 I have a IBAction that uses this array, it looks something like this

Code:
-(IBAction)continueAction:(id)sender{

	random = arc4random() % [self.unusedNibs count];
	NSString *nibName = [self.unusedNibs objectAtIndex: random];
	[self.unusedNibs removeObjectAtIndex: random];


	if (nibName == @"Question 3") {

	Question_3 *Q3 = [[Question_3 alloc] initWithNibName:@"Question 3" bundle:nil];
	
	Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:Q3 animated:YES];
	[Q3 release];
	}

	if (nibName == @"Question 2") {

	Question_2 *Q2 = [[Question_2 alloc] initWithNibName:@"Question 2" bundle:nil];
	
	Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
	[self presentModalViewController:Q2 animated:YES];
	[Q2 release];
	}
}
Again the Question 2 and Question 3 parts of this code are irrelevant. So as you can see the code picks a random NIB to load, then deletes it from the array. The problem is, its not deleting it from the array.

I think the array isnt be passed to the other class. Any Ideas?
The app runs fine, but it still loads those NIBs that should have been deleted out of the array.

Thanks,
Jacob
It sounds like you have a single array that is used throughout multiple objects in your app.

If there are any objects in your app design where the app creates one and only one instance, and that object persists throughout the app, use that object to hold the array. Add a property and refer to the array through the property.

For example, if your app is a game, and you have a game controller object that runs the flow of the game, you might put the nibArray into the game controller, and make it a property of the game controller.

Then, all your objects can refer to the array in the game controller:

Code:
[gameController.nibArray count]
Then the problem becomes making sure that all the different objects in your app have a pointer to your central object (the gameController in my example.)

The cleanest way to do this is to implement a data container singleton object. Take a look at the password generator tutorial in my signature. That tutorial explains how to use a data container singleton to share data across your entire app in a clean, maintainable way.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-11-2011, 02:32 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 4
jhyde01 is on a distinguished road
Smile

Thank You guys for your answers! Very much appreciated.

When you are talking about objects, are you meaning a class that has each Question as a subclass? Through the class HowToPlay, i have that class initialize the variable score, which is passed though the whole app, that seems to work.

Last night while going to bed I had a though, I have the array set up in the viewDidLoad part of HowToPlay, Maybe that was a bad idea to put it their, maybe I should make a separate method for it?
jhyde01 is offline   Reply With Quote
Reply

Bookmarks

Tags
array, inheritance, ios, nib

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 368
9 members and 359 guests
apatsufas, chemistry, Kirkout, leostc, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:37 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0