Sharon Watinsan
Sharon Watinsan

Reputation: 9850

How to pass value to a variable in an inherited class

I have a class called TimeLineViewController which is inherited from MyViewController. I need to pass a value to a variable from MyViewController to TimeLineViewController. How can i do it ?

MyViewController.h

@interface MyViewController : TimeLineViewController {
.....
}

In TimeLineViewController.h i have a String *str assigned. From MyViewController.m i need to pass a value to the String *str variable in the TimeLineViewController class. How can i do this.

I tried the following from MyViewController.m but none worked.

[super str]=@"hi";

Upvotes: 1

Views: 74

Answers (4)

Josshad
Josshad

Reputation: 964

You should have setter or property in TimeLineViewController.

Then you can use

    [self setStr:@""];

or

    self.str = @"";

Upvotes: 0

manujmv
manujmv

Reputation: 6445

From the apple's doc,

The instance variable is accessible within the class that declares it and within classes that inherit it. All instance variables without an explicit scope directive have @protected scope.

So you can just use as

super.str = @"hi";

Upvotes: 0

giorashc
giorashc

Reputation: 13713

The point of inheritance is using existing functionality and extending it for specific needs by the sub class(es)

So... If your TimeLineViewController inherits from MyViewController there is no need to declare the member again in TimeLineViewController and you can just use it with since it was already declared for MyViewController:

self.str = @"hi";

Upvotes: 1

luiso1979
luiso1979

Reputation: 918

If str is a property inside the class TimeLineViewController you can access it via inheritance in MyViewController. So if you change it in MyViewController it changes also for the father.

Remember:

   A    

   |

   B

if in A you have a property c then you can do B.c.

Read this.

Upvotes: 0

Related Questions