Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
Hi at all I have a little problem, i'm a beginner of iphone sdk, this is my first application, I want to usa e simple var of a class in the appdelegate, example

class.h
int ID

class.m
-(void) xxx {
ID=5
}

appdelegate.m
NSLog (@"var is %d"ID)

how can do this?
thank you very much
kikko088
 
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C5115c Safari/6533.18.5)

What do you know about properties?
 
very little...looking in some forum i see tha I have to use the property, i try this code, but not work...


Code:
delegate.h
int variabileID;
@property int variabileID;

viewController.h
int PassTag;
@property int PassTag;

viewController.m
-(void) varID{
        PassTag = 1;
        iIngAppDelegate *passID = [[UIApplication sharedApplication] delegate];
        passID.variabileID = PassTag;
}


my problem is that all documents are in english and my english isn't very well :(


kikko088
 
i try this code, but not work...
It doesn't work how? Does it compile? If not, what errors or warnings are you getting? If it does compile, are you getting any run-time errors? If it's crashing, have you looked at the crash log?

The more specific you can be in providing us with what "not work" means, the better equipped we will be to provide help.
 
thank for the answer, you're right, the app not display any error, there isn't any crash, the code is valid but the variable not pass, the var need to pass an id for a database query and with this code not pass (the query not work), I'm sure that the query work because if I put a var before the query it work.
 
thank for the answer, you're right, the app not display any error, there isn't any crash, the code is valid but the variable not pass, the var need to pass an id for a database query and with this code not pass (the query not work), I'm sure that the query work because if I put a var before the query it work.
Debugging descriptions is hard. When you talk about the code doing things, please provide that code, as much of the code as possible. All the code you've provided so far is just very small snippets and wouldn't even compile (for example, it's missing the needed @synthesize directives). If you don't want to reveal what your actual app is doing, then put together a small test app to demonstrate the concept that you are trying to implement. And then post that code here. Also, it would probably help to apply some basic debugging skills to verify that what you think is going on is actually going on. Hope that helps.
 
I suggest some reading on basic OOP programming.

Before you look at properties, do you know what an instance method is? You need a method in your class that does nothing other than return the value of ID. Then in appdelegate, call that method to retrieve that value of ID. A property more or less does that work for you, but also with some memory management needed of you are passing objects around. But before you get that far, know that a method that returns the value is what you really need.

Something like:

class.m
// assume you have other code that sets the value of ID somewhere
-(int) ID {
return ID;
}

appdelegate.m
//somewhere before the following line an object of "class" is created named "myobject"
NSLog (@"var is %d",[myobject ID]);
 
I suggest some reading on basic OOP programming.

Before you look at properties, do you know what an instance method is? You need a method in your class that does nothing other than return the value of ID. Then in appdelegate, call that method to retrieve that value of ID. A property more or less does that work for you, but also with some memory management needed of you are passing objects around. But before you get that far, know that a method that returns the value is what you really need.

Something like:

class.m
// assume you have other code that sets the value of ID somewhere
-(int) ID {
return ID;
}

appdelegate.m
//somewhere before the following line an object of "class" is created named "myobject"
NSLog (@"var is %d",[myobject ID]);

I read something but in italian there are very few doc, :( I arrange with what I find and what I understand of english document. I think i understand what you say, I have to create a method like this

Code:
-(int) varID{
	PassTag = 1;
	return PassTag;
}

than create an object of my class, and put the on my object the result of the method, can be correct something like this?
In appdelegate I create a method that return a var

Code:
-(int) cambia {
	
	ListView *my_ID = [[ListView alloc] init];
	variabileID = [my_ID varID];
	return variabileID;

}


EDIT: with consolle i see that if I put this
Code:
-(int) varID{
	PassTag = 1;

	NSLog(@"PassTag = %d",PassTag);
	return PassTag;
}

after the method that create the button on my page the console not display PassTag :(


kikko088
 
EDIT: it work at 90% my only problem is that (i think) I can't use a method because the correct number is only on -(void) listener:(id) and I don't know haw pass this value to my method
 
Unfortunately, I can't debug your code. But here is a short working example demonstrating the concept. This is assuming simple data types like int or float. For objects memory management comes into play and it gets more complicated.

Also, I highly recommend trying to find a book in Italian, or struggle with English. While this is a simple easy to follow example, applying it to the MVC model that cocca uses might not work by example. An explanation of that model might be needed. Also, an explanation of how OOP works compared to procedural programming would be very useful.

If you can struggle through the English in this book it would be much faster than just looking at random examples on the web.

http://www.amazon.com/Programming-Objective-C-Stephen-Kochan/dp/0672325861


Code:
#import <Foundation/Foundation.h>

@interface myClass : NSObject 
	
	{
		int number;
	}
	
	-(void) setNumber: (int) newNumber;
	-(int) getNumber;
	
@end
	
@implementation myClass
	
-(id)init
	{
		number = 5;
		return self;
	}
	
	-(void) setNumber: (int) newNumber
	{
		number = newNumber;
	}
	
	
	-(int) getNumber
	{
		return number;
	}
	
	
@end
	


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

// create the object
	myClass * myObject = [[myClass alloc] init];

// getNumber right after creation to see what it is
	NSLog(@"on creation ID is set to 5");
	NSLog(@"ID = %d",[myObject getNumber]);
	
//set number to 15
	[myObject setNumber:15];
// getNumber again.	
	NSLog(@"ID = %d",[myObject getNumber]);
	
	
    [pool drain];
    return 0;
}
 
As an Amazon Associate, MacRumors earns a commission from qualifying purchases made through links in this post.
thank you very much! in the comming days i search a book and I'm starting to study :)
for now I have only a question, I see your code and I think to understand how it work, but my problem is that the method that display button has an action that when I touch the button it load another view, my var is inside the method where is load the view then if I put any other method outside this function the method just created is skipped (this is how I see on my code)


Code:
- (void) createButtons
{
    ...code for print button

    [button addTarget:self action:@selector( listener: ) forControlEvents:UIControlEventTouchUpInside];

    button.tag = TAG_INDEX + count;
    [self.view addSubview:button];
    ++count;

 }

- (void) listener:(id) sender
{
  int index = [sender tag] - TAG_INDEX;

	iIngAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	ContenutoObject *co = [delegate.array_contenuti objectAtIndex:index];
	
	TAG = [[co id] intValue];
	[self varID];
	
	// Push view controller as appropriate
	detail = [[ListaArgomentoController alloc] initWithNibName:@"ListaArgomento" bundle:[NSBundle mainBundle]];
	[self.navigationController pushViewController:detail animated:YES];

	// Set the title of the view to the argoment
	self.detail.title = [co id];
	[detail release];
}

- (int) varID
{

	PassTag = TAG;
	NSLog(@"PassTag = %d",PassTag);	

	return PassTag;
}

EDIT: the var was passed but if I click a button the var don't refresh, for example, if I init PassTag with 1 on my appdelegate the var work, if I put PassTag=TAG the passed variable is 0 and it don't change to cbutton click
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.