variable to appdelegate

Discussion in 'iOS Programming' started by kikko088, Oct 13, 2010.

  1. kikko088 macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    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?
     
  3. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #3
    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
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    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.
     
  5. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #5
    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.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    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.
     
  7. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #7
    This is my "app" is a simple database :) http://www.endurodoc.net/myApp.zip

    in listView there is a var "TAG" that I want to pass in appDelegete as "variabileID"


    kikko088
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Before I take the time to download and look at your project, what debugging have you done already and what have you discovered?
     
  9. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #9
    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]);
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Except your code shows an instance method (-) and not a class method (+). :)
     
  11. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #11
    True, nice catch of my mistake. I meant instance method.
     
  12. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #12
    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
     
  13. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #13
    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
     
  14. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
  15. wlh99 macrumors 6502

    Joined:
    Feb 7, 2008
    #15
    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;
    }
    
     
  16. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #16
    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
     

Share This Page