I just started to learn iOS programming and I have a problem with inheritance. There are 2 files.
First file
Header
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController {
UILabel *myLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@end
Implementation:
#import "ViewController.h"
#import "NewClass.h"
@implementation ViewController
@synthesize myLabel;
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
myLabel.text = @"ViewController text!";
NewClass *myClass = [[[NewClass alloc] init] autorelease];
[myClass setLabelText];
}
@end
Second file
Header:
#import "ViewController.h"
@interface NewClass : ViewController
-(void) setLabelText ;
@end
Implementation:
#import "NewClass.h"
@implementation NewClass
- (void) setLabelText {
myLabel.text = @"NewClass text!";
}
-(id)init {
self = [super init];
if (self != nil) {
}
return self;
}
@end
And i set myLabel outlet in IB.
Why when i call [myClass setLabelText]; nothing happens? There also remained “ViewController text!” on label view.
Where is my problem? How can i change ViewController::myLabel.text in NewClass
This is not inheritance that you are doing. In your ViewController implementation, you have the following code
This is just creating an instance of the class NewClass and setting the labelText on that instance. You are never displaying it or adding it to a view.