Methods and Class issues.

Joined
Apr 27, 2011
Messages
10
Reaction score
0
Points
1
Here is the source for my project.
http://www.mediafire.com/?1koujayketou9xo

I solved the Passing of the text value issue that was one of the issues addressed in this post. Now i have isolated why my save system isn't working and whould like input on that.

in the class homeworld.m:
Code:
- (IBAction)tapSave:(id)sender {
    //NSLog(@"test");
    [saveViewController saveGame];
    //[saveViewController test];
    //NSUserDefaults *SaveDefaults = [NSUserDefaults
     //                               standardUserDefaults];
   
    //NSLog(@"%i",[SaveDefaults integerForKey:@"SaveCount"]);
   
}
the line [saveViewController saveGame] isn't working.

in SaveViewController.m:
Code:
-(void)saveGame{
    NSLog(@"test");
    [charData saveChar];
}/code]

the problem is somewhere inbetween this. it dosent log the test.
SaveViewController.h is outleted to saveViewController in Homeworld.h.
 

pigoo3

Well-known member
Staff member
Admin
Joined
May 20, 2008
Messages
44,213
Reaction score
1,424
Points
113
Location
U.S.
Your Mac's Specs
2017 15" MBP, 16gig ram, 1TB SSD, OS 10.15

pigoo3

Well-known member
Staff member
Admin
Joined
May 20, 2008
Messages
44,213
Reaction score
1,424
Points
113
Location
U.S.
Your Mac's Specs
2017 15" MBP, 16gig ram, 1TB SSD, OS 10.15
That was quite an edit you did on post #1...MUCH more concise after the edit.:)

Of course I still have no idea what you're doing!;)

Good luck,

- Nick
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
It doesn't work because you never instantiated a SaveViewController. If you put a breakpoint on that line, and inspect the variable values, you'll see that saveViewController is set to 0x0 - which is nil, which is why nothing is happening. The only place I see you instantiating a SaveViewController object is in FinalViewController.h - but you're trying to access that in an instance of HomeWorld which has NO reference to the SaveViewController object that you instantiated in the finalViewController instance.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Have you instantiated saveViewController? Otherwise saveViewController is nil and a message to nil does nothing. It is valid to call nil. At minimum you'd need this before calling your object...

Code:
	SaveViewController * saveViewController;
	saveViewController = [saveViewController new];


Normally view controllers are displayed on the screen. Like so...

Code:
- (IBAction) displaySaveViewController: (id) sender
{
	SaveViewController * saveViewController;
	saveViewController = [saveViewController new];
	
	[saveViewController setTitle: @"Save Game"];
	
	UIBarButtonItem * backButton = [[UIBarButtonItem alloc] initWithTitle:@"Game" style: UIBarButtonItemStyleBordered target: nil action: nil];
	self.navigationItem.backBarButtonItem = backButton;
	[backButton release];
	
[COLOR="Orange"]// And since the saveViewController object would have to know about the game, 
// you might have something like this line before pushing the saveViewController to display on the screen.
// Somehow, saveViewController needs to know what to save.[/COLOR]
[saveViewController gameToSave: [COLOR="Red"]charData[/COLOR]];

	[self.navigationController pushViewController: saveViewController animated:YES];
	[saveViewController release];
}

After displaying the screen, you'd have a choice to actually call the saveGame method within self.


So given the above, here is the minimum I think you'd need. I'd question why you made it a ViewController though if you are not displaying a screen.

Code:
	SaveViewController * saveViewController;
	saveViewController = [saveViewController new];
	[saveViewController gameToSave: charData];
	[saveViewController saveGame];
Those last two lines could of course be turned into one.



No, I did not download your zip file. Post what we need to know to help you.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Man, why do I bother. Nethfel downloads the code and beats me to the punch line. ;D
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
hahahahaha, but it's always entertaining to see how another person interprets the code :)

The OP isn't using a nav controller (which is a pity as it would make certain things so much easier) and instead manually adding/removing the views from the window... I think the OP is making the mistake of thinking that if an object is instantiated in one classes instance ie: ClassA has an iVar for class B and instantiates classB that an instance of ClassC will have access to that instance of ClassB by just creating an "identical" instance variable...
 
OP
B
Joined
Apr 27, 2011
Messages
10
Reaction score
0
Points
1
hahahahaha, but it's always entertaining to see how another person interprets the code :)

The OP isn't using a nav controller (which is a pity as it would make certain things so much easier) and instead manually adding/removing the views from the window... I think the OP is making the mistake of thinking that if an object is instantiated in one classes instance ie: ClassA has an iVar for class B and instantiates classB that an instance of ClassC will have access to that instance of ClassB by just creating an "identical" instance variable...

thats one confusing statement.....

SaveViewController * saveViewController;
saveViewController = [saveViewController new];
[saveViewController gameToSave: charData];
[saveViewController saveGame];

I will try adding the second line here on Wednesday when i have access to a mac again. But whats with the 3rd and 4th line? i don't understand how those are supposed to work..
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
In terms of what I was saying - you instantiated a SaveViewController object in one place, but then expected to be able to use it in a different place without passing a reference to it. Let's say you have:

Class1:
it contains an IVar for two other view controllers, Class2 and Class3. You instantiate both of those Classes during the course of the code in Class1.

Now, some time later, you make Class2 the view controller that has it's view currently displayed.

In Class2, you've created an IBOutlet IVar to Class3 thinking it's possible to access the instance of Class3 that is stored in Class1. This will not work. Class2 is independent of Class1 in terms of having access to objects instantiated in Class1 UNLESS you pass a reference of Class3 to Class2.

I know, it probably isn't worded as well as it can be - it's a difficult concept to explain with words on a page sometimes.



With regard to XSteps post, I think what he is trying to imply that there should be a method in your saveViewController to pass the information to that needs to be saved. That's what his 3rd and 4th line imply. Basically the lines:

Code:
// Declare a variable that is a pointer to a SaveViewController object 
SaveViewController * saveViewController;

// Instantiate a SaveViewController object, in his case using the new method.  You can
// use new or the more common way in current code of doing alloc/init.  There was one 
// typo - he should have had a capital on the SaveViewController in the []
saveViewController = [SaveViewController new];

// Set information within the saveViewController object - assumed to be an IVar or 
// something the information that will need saving...
[saveViewController gameToSave: charData];

// send the saveGame message to the saveViewController object to have it actually
// save the game...
[saveViewController saveGame];

The code won't necessarily work unless your framework supports it. Currently, you have no method -(void)gameToSave:(CharData *)cd; in your class for SaveViewController.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
hahahahaha, but it's always entertaining to see how another person interprets the code :)

Yes it is. We all think and interpret things a little different :)
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
With regard to XSteps post, I think what he is trying to imply that there should be a method in your saveViewController to pass the information to that needs to be saved. That's what his 3rd and 4th line imply.


The code won't necessarily work unless your framework supports it. Currently, you have no method -(void)gameToSave:(CharData *)cd; in your class for SaveViewController.

Yep, that explained it well. Darn typos!

Brad35309, I was demonstrating an idea. As I said, I haven't looked at your zip file to see all of your code. I have enough of my own coding problems to not want to look at that level of detail. I think I'll leave well enough alone.
 
OP
B
Joined
Apr 27, 2011
Messages
10
Reaction score
0
Points
1
In terms of what I was saying - you instantiated a SaveViewController object in one place, but then expected to be able to use it in a different place without passing a reference to it. Let's say you have:

Class1:
it contains an IVar for two other view controllers, Class2 and Class3. You instantiate both of those Classes during the course of the code in Class1.

Now, some time later, you make Class2 the view controller that has it's view currently displayed.

In Class2, you've created an IBOutlet IVar to Class3 thinking it's possible to access the instance of Class3 that is stored in Class1. This will not work. Class2 is independent of Class1 in terms of having access to objects instantiated in Class1 UNLESS you pass a reference of Class3 to Class2.

I know, it probably isn't worded as well as it can be - it's a difficult concept to explain with words on a page sometimes.



With regard to XSteps post, I think what he is trying to imply that there should be a method in your saveViewController to pass the information to that needs to be saved. That's what his 3rd and 4th line imply. Basically the lines:

Code:
// Declare a variable that is a pointer to a SaveViewController object 
SaveViewController * saveViewController;

// Instantiate a SaveViewController object, in his case using the new method.  You can
// use new or the more common way in current code of doing alloc/init.  There was one 
// typo - he should have had a capital on the SaveViewController in the []
saveViewController = [SaveViewController new];

// Set information within the saveViewController object - assumed to be an IVar or 
// something the information that will need saving...
[saveViewController gameToSave: charData];

// send the saveGame message to the saveViewController object to have it actually
// save the game...
[saveViewController saveGame];

The code won't necessarily work unless your framework supports it. Currently, you have no method -(void)gameToSave:(CharData *)cd; in your class for SaveViewController.

that actually clarified what you said and now it makes sense to me, about the classes. how do i make a reference of class 2 to class 3 then? i thought by creating an outlet to it after importing it was the class reference. Am i better of creating the CharData class within the first class which references class's 2 and 3 and just call a method from here?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
that actually clarified what you said and now it makes sense to me, about the classes. how do i make a reference of class 2 to class 3 then? i thought by creating an outlet to it after importing it was the class reference. Am i better of creating the CharData class within the first class which references class's 2 and 3 and just call a method from here?

Ok, before I answer you, let me ask this - the SaveViewController - what ultimate function do you intend it to provide? Just getting a name for the data to be saved as? Do you want it to handle the entire saving process? etc.
 
OP
B
Joined
Apr 27, 2011
Messages
10
Reaction score
0
Points
1
The Class SaveViewController was ment to provide an interface for loading a character or creating a new one. this was its only function. the only reason i have all these different subView Controllers was to sort out the code. if it was all in one xib file it'd get pretty thick and dense. Also, whats a nav controller? If thats a better way of handing different views, is it worth converting it to that, for lack of a better vocabulary, style?

Ok, before I answer you, let me ask this - the SaveViewController - what ultimate function do you intend it to provide? Just getting a name for the data to be saved as? Do you want it to handle the entire saving process? etc.
 
OP
B
Joined
Apr 27, 2011
Messages
10
Reaction score
0
Points
1
ReVamp

This project is due next week wednesday so i have maybe a good 5 more hours to work on it. in light of this and all the errors im encountering trying to develop this multi view class game. ive started over and have made more progress than i have over the last couple weeks. but now, with the new project, i am getting errors when it trying to load a subview.

with this line of code i am receiving the error

Code:
slideConverter = ([SaveDefaults integerForKey:@"charXP"])/([SaveDefaults integerForKey:@"charXPMAX"]);

slideConverter is a float and charXp and charXPMAX are int's.


Thread 1: Program received signal: "EXEC_ARITHMETIC"

is this because im using two ints to get a float?

Source >http://www.mediafire.com/?tx7h1a21iwy8djv alot less confusing. Ive tried a new Naming scheme and actually started commenting on what im doing!
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
I'll get to your question on page 1 later when I'm at home (I'm at work atm, so writing anything lengthy would be difficult).

In terms of the error you're getting now; I'm going to *GUESS* that you've got a divide by 0 error there. Either you haven't got a value saved in that NSUserDefaults, so charXPMAX doesn't exist in the defaults (which is what I assume you have bound to the SaveDefaults since I don't have your source in front of me) OR some how a 0 got stored there and so that's what's getting loaded.

What I'd probably do is do an NSLog to output the values of those two keys prior to that slider setting to see what they actually are.
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
The Class SaveViewController was ment to provide an interface for loading a character or creating a new one. this was its only function. the only reason i have all these different subView Controllers was to sort out the code. if it was all in one xib file it'd get pretty thick and dense. Also, whats a nav controller? If thats a better way of handing different views, is it worth converting it to that, for lack of a better vocabulary, style?

Nav controllers are one of the easiest forms of loading view controllers to be able to navigate back and forth. They work basically that you start with your root view controller, then push on additional view controllers as you need them (for example, you may start with your main menu, new game would push the game controller, if you exit to the main menu, you pop the game controller - you see them used all of the time like in the mail app or any other app where you drill down views)

In terms of saving or loading, there are various ways to handle passing the data between objects. Some of them include:

* create an IVar in the subview that needs to modify the data, have it set to assign, make sure in the primary view controller that the memory space is allocated and you modify a mutable object (instead of allocating new memory) - if you need to allocate new memory at the time of load (which is fine too) you need to pass the variable by reference which may be a bit beyond where you're at. (I can create an example, but if you need to access it by reference it may be confusing)

* create a delegate protocol for the save view controller (say, call it SaveDelegate), set up the invoking view controller to be the delegate and create a delegate method that you would use to "send" the data from the save view controller to its delegate (if this interests you let me know and I can whip up a sample for you)

* Use NSNotificationCenter. Have your view controller that needs the data to register as a listener for a custom message you create (like "dataLoaded"); then in the save view controller, send that message with in the user data, the data you loaded. NSNotificationCenter is fairly easy to use and very straight forward. The receiving view controller must exist for this to work (so although the view may not be displayed, the view controller must not have been deallocated). The only gotchya is that you need to make sure you un-register for messages in the dealloc of the view controller that is expecting messages otherwise your app can crash if the message appears but the controller is gone. (another fairly easy example can be created)

* Use a singleton object to store your data. This object would be available throughout the entire application lifecycle in any instance that needed access to it. Since it's available anywhere, you don't have to do any specially passing of data to get information from one object instance to another. The negative is that this is the equivalent of a global variable. As such, it is many times frowned upon, especially by instructors except in certain situations where they make sense to use.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top