Global variables in Objective-C?

Discussion in 'iOS Programming' started by Septimus P, Sep 29, 2008.

  1. Septimus P macrumors newbie

    Sep 29, 2008

    I have written a class and declared a variable. How can I reach it with different messages?

    Interface-File Deklarations:

    int value;

    - (void)valueHasChanged:(int)v;
    - (void)showIt;


    - (void)valueHasChanged:(int)v {
    value = v;

    - (void)showIt {
    NSString *sendtext = [NSString stringWithFormat:mad:"%i", value];
    UIAlertView *alert = [[UIAlertView alloc]
    initWithTitle:sendtext message:mad:" Here it is! " delegate:self
    cancelButtonTitle:mad:"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
  2. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Obj-C is just a wrapper around C. You can declare and instantiate global variables (and functions) outside the class definitions, just as in standard C. Then access them from anywhere they are declared (as an extern maybe).

    Look for a C programming language primer for examples.

  3. Septimus P thread starter macrumors newbie

    Sep 29, 2008

Share This Page