Global variables in Objective-C?

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

  1. Septimus P macrumors newbie

    Joined:
    Sep 29, 2008
    #1
    Hi,

    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;

    Implementation-File:

    - (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

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #2
    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

    Joined:
    Sep 29, 2008

Share This Page