NoviceDeveloper
NoviceDeveloper

Reputation: 225

How to access superclass variable in subclass

I have created two viewController classes such that one is superclass of another.i have a nsstring variable in superclass which i have to access in subclass please guide me how to do it. Here is my code

  @interface Superclass : UIViewController{
  NSString *message
  }
  @property(nonatomic,retain)NSString *message;
  -(id)init;
  @end


 @implementation Superclass
 @synthesize message;
 -(id)init{
{
[super init];
message=@"Hello";
return self;

 }


  @interface Subclass : Superclass{

  }

 @end


 @implementation Subclass

 - (void)viewDidLoad {

   UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:self.message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];
[alert release];
  [super viewDidLoad];


 }

My alert promt but without message.

Upvotes: 2

Views: 3480

Answers (4)

Mark.zh
Mark.zh

Reputation: 15

just use

self.message = @"something"

, superclass's members will be inherited by subclass object

Upvotes: 0

Jignesh Chanchiya
Jignesh Chanchiya

Reputation: 803

declare variable as public into super class

than

UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alert" message:message delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
[alert show];

Upvotes: 0

Ilanchezhian
Ilanchezhian

Reputation: 17478

How you are initializing the Subclass?

Using initWithNibName:bundle: method?

In that case, your's superclass init method will not be called. So override initWithNibName:bundle: method in the super class and set the value to the variable there.

Upvotes: 3

Vignesh
Vignesh

Reputation: 10251

you can use super.variable to access the super class variable.

Upvotes: 0

Related Questions