Reputation: 1151
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
Upvotes: 0
Views: 1984
Reputation: 10378
Wait... from the looks of it you are going to have some serious issues with stack overflows: when you load a ViewController, it in turn creates myClass. Because myClass is a subclass of ViewController, this in turn will create it's own variable 'myClass' when loaded... etc. This will continue forever, or until you get a stack overflow error.
In fact, I'm pretty confused by your code. Perhaps it might be time to rethink what you are trying to achieve and what your code should be doing?
Upvotes: 0
Reputation: 119282
NewClass *myClass = [[[NewClass alloc] init] autorelease];
[myClass setLabelText];
Here you are creating the new view controller via alloc / init (not usual if you have a backing xib file for the view controller, you would usually use initWithNibName:) and then setting a property on an IBOutlet. At this point, the outlet will be nil
since you haven't loaded a nib.
Also, as Nick Bull points out, you are just creating an instance, not displaying it anywhere. So even if you did use initWithNibName, you wouldn't see your changes unless you presented the view controller somehow.
Upvotes: 0
Reputation: 801
This example may help u where u can change label text by clicking on button.implement the below code in ur example
//class1.h
IBOutlet UILabel *text1;
@property(nonatomic,retain)UILabel *text1;
-(IBAction)press:(id)sender;
//class1.m
-(IBAction)press:(id)sender
{
Class2 *method = [[Class2 alloc]init];
NSString *string = [method sayhello];
[method release];
text1.text = string;
}
//class2.h
{
}
-(NSString *)sayhello;
@end
//class2.m
-(NSString *)sayhello
{
return @"hello";
}
connect button to file owner..
Upvotes: 0
Reputation: 4286
This is not inheritance that you are doing. In your ViewController implementation, you have the following code
NewClass *myClass = [[[NewClass alloc] init] autorelease];
[myClass setLabelText];
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.
Upvotes: 2