Hey I'm very new to Objective C programming and I'm stuck. How come when I create I function, it can't use the variables I created for the labels or textviews, etc. And whenever I call them in the viewDidLoad function, I have to do either self.(variableName) or _(variableName) and it won't let me do that outside of the viewDidLoad function. Is there a way to access them outside of it?
2 Answers
How come when I create I function, it can't use the variables I created for the labels or textviews, etc.
For one thing, you need to differentiate between a function and an instance method. In Objective-C, classes can have instance variables (variables that are part of an instance of that class) and instance methods (similar to functions that are associated with an instance of that class). Classes can also have properties, which are used rather like instance variables in that they're values associated with an object, but they're accessed through accessor methods. Functions, on the other hand, aren't part of any class. So, a class has an interface where instance variables and methods are declared, like this:
@interface Person : NSObject
{
NSString *firstName;
NSString *lastName;
}
@property (readonly) NSString *fullName;
@property (strong) NSArray *friends;
@property (assign) int age;
- (id)initWithFirstName:(NSString*)first lastName:(NSString*)last;
- (void)addFriend:(Person*)friend;
@end
And also an implementation, like this:
@implementation Person
- (id)initWithFirstName:(NSString*)first lastName:(NSString*)last
{ /* code goes here */ }
- (void)addFriend:(Person*)friend
{ /* code goes here */ }
- (NSString *)fullName
{ return [NSString stringWithFormat:@"%@ %@", firstName, lastName; }
@end
Those things in the implementation are instance methods, as denoted by the -
at the beginning and the fact that they're defined in an @implementation
block. (If they had +
instead of -
, they'd be class methods instead of instance methods -- I'll let you read about that in the docs.) Properties are accessed by calling an appropriate accessor methods using either normal method calls or dot notation, so if you have:
Person *george = [[Person alloc] initWithFirstName:@"George" lastName:@"Bailey"]
all of these are valid:
NSString *name1 = george.fullName;
NSString *name2 = [george fullName];
george.age = 45;
[george setAge:45];
int years1 = george.age;
int years2 = [george age];
Also, self
is a pointer to "the current object". You can use it in instance methods so that objects can call their own methods and access their own properties. For example, the Person
class could contain a method like this:
- (NSString *)nameAndAge { NSString *nameAndAge = [NSString stringWithFormat:@"%@: %d", self.fullName, self.age]; }
Functions, on the other hand, aren't part of any class, use C function syntax rather than Objective-C method syntax, and aren't defined in an @implementation
block:
BOOL isMiddleAged(Person* person)
{
return (person.age > 30) && (person.age < 60);
}
You can't use self
in function because a function isn't associated with an object, so there's nothing for self
to point to. You can, however, use properties of other objects you know about, such as person.age
in the example above.
And whenever I call them in the viewDidLoad function, I have to do either self.(variableName) or _(variableName) and it won't let me do that outside of the viewDidLoad function.
You must be accessing properties of your view controller. As explained above, self.(variableName)
is the way to access properties. _(variableName)
refers to a variable (often generated by the compiler) that stores the value of the property. (You shouldn't normally access those variables directly outside initialization methods and -dealloc
-- use the property accessors instead.) You can use those properties in any instance method of the class, not just -viewDidLoad
. You can also access properties of other objects by replacing self
with the name of a pointer to the object, just as I did with person
in isMiddleAged()
.

- 124,013
- 19
- 183
- 272
Seems like your are using autosythesized property. Using Auto Synthesized property you need not to @syhtesize objects.
@sythesize object = _object;
will be implicitly implement in this case.
So you can access object using self.object or _object.
You can @synthesize to avoid using objects via self.varName or _varName .You can directly use it using varName.

- 9,786
- 1
- 37
- 59
-
Im using a MKMapView and I made MKCoordinateRegion a global variable and I'm changing its attributes in the IBAction called by the UISlider but it doesn't change in the simulator? Is there anything I need to do to like reload the map? – Sanchit Gupta Apr 12 '13 at 08:17