简体   繁体   English

如何在iOS sdk中使用继承

[英]How to work with inheritance in iOS sdk

I just started to learn iOS programming and I have a problem with inheritance. 我刚开始学习iOS编程,我的继承问题。 There are 2 files. 有2个文件。

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. 我在IB中设置了myLabel插座。

Why when i call [myClass setLabelText]; 为什么当我调用[myClass setLabelText]; nothing happens? 什么都没发生? There also remained "ViewController text!" 还有“ViewController文字!” on label view. 在标签视图上。 Where is my problem? 我的问题在哪里? How can i change ViewController::myLabel.text in NewClass 如何在NewClass更改ViewController::myLabel.text

This is not inheritance that you are doing. 这不是你正在做的继承。 In your ViewController implementation, you have the following code 在ViewController实现中,您有以下代码

 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. 这只是创建NewClass类的实例并在该实例上设置labelText。 You are never displaying it or adding it to a view. 您永远不会将其显示或添加到视图中。

This example may help u where u can change label text by clicking on button.implement the below code in ur example 此示例可以帮助您通过单击按钮更改标签文本。在您的示例中实现以下代码

//class1.h //class1.h

IBOutlet UILabel *text1;

@property(nonatomic,retain)UILabel *text1; @property(非原子,保留)UILabel * text1;

-(IBAction)press:(id)sender;

//class1.m //class1.m

-(IBAction)press:(id)sender

{ {

Class2 *method = [[Class2 alloc]init];
NSString *string = [method sayhello];

[method release];
text1.text = string;

} }

//class2.h //class2.h

{ {

} }

-(NSString *)sayhello;

@end @结束

//class2.m //class2.m

-(NSString *)sayhello

{ {

return @"hello"; return @“你好”;

} }

connect button to file owner.. 将按钮连接到文件所有者..

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. 在这里,您将通过alloc / init创建新的视图控制器(如果您有视图控制器的后备xib文件,则通常使用initWithNibName :),然后在IBOutlet上设置属性。 At this point, the outlet will be nil since you haven't loaded a nib. 此时,由于您没有装入笔尖,因此插座将nil

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. 因此,即使你确实使用了initWithNibName,除非你以某种方式呈现视图控制器,否则你不会看到你的更改。

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. 等等......从它的外观来看,你会遇到堆栈溢出的严重问题:当你加载一个ViewController时,它会创建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. 因为myClass是ViewController的子类,所以这反过来会在加载时创建它自己的变量'myClass'......等等。这将永远持续,或者直到你得到堆栈溢出错误。

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? 也许是时候重新思考一下你想要实现的目标以及你的代码应该做些什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM