Search code examples
objective-ciphonesuperclass

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.


Solution

  • 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.