如何访问子类中的超类变量

时间:2012-03-26 13:01:17

标签: objective-c iphone superclass

我创建了两个viewController类,其中一个是另一个的超类。在超类中有一个nsstring变量,我必须在子类中访问,请指导我如何操作。这是我的代码

  @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];


 }

我的警告,但没有消息。

4 个答案:

答案 0 :(得分:3)

如何初始化Subclass

使用initWithNibName:bundle:方法?

在这种情况下,不会调用您的超类init方法。因此,在超类中覆盖initWithNibName:bundle:方法,并将值设置为那里的变量。

答案 1 :(得分:0)

您可以使用super.variable来访问超类变量。

答案 2 :(得分:0)

将变量声明为public为超类

大于

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

答案 3 :(得分:0)

只需使用

  

self.message = @“某事”

,超类的成员将由子类对象

继承
相关问题