类之间传递数据的差异 [英] Discrepancy in passing data between classes

查看:39
本文介绍了类之间传递数据的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码的第1部分工作正常.第2部分显示了代码中的细微更改,这些更改导致代码按预期停止工作(没有错误/警告).

Part 1 of the code works fine. Part 2 shows minor changes in the code which causes the code to stop working (without errors/warnings) as expected.

第1部分:(工作)

#import "ClassA.h"
#import "ClassB.h"

@implementation ClassA

- (void) sendData
{
    NSString *temp = [NSString stringWithFormat:@"HI!"];
    ClassB *classBObject = [[ClassB alloc] init];
    classBObject.dataToDisplay = temp;
    self.view = classBObject.view;
}

@end

ClassB的接口

#import <UIKit/UIKit.h>

@interface ClassB : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *textLabel;
@property NSString * dataToDisplay;

@end

B类的实现

#import "ClassB.h"

@implementation ClassB

@synthesize dataToDisplay, textLabel;

- (void)viewDidLoad
{
    [super viewDidLoad];

    textLabel.text = dataToDisplay;
}

@end

第2部分:

但是如果我将ClassA的-(void)sendData 更改为以下内容:

But if I change - (void)sendData of ClassA to the following:

- (void) sendData
    {
        NSString *temp = [NSString stringWithFormat:@"HI!"];
        ClassB *classBObject = [[ClassB alloc] init];
        classBObject.textLabel.text = temp; // statement changed from Part 1.
        self.view = classBObject.view;
    }

并从 ClassB implementation implementation 中删除 textLabel.text = dataToDisplay; ,该视图控件的 textLabel ClassB 未更新.你能建议,为什么会这样?

and remove textLabel.text = dataToDisplay; from implementation of ClassB, the textLabel on view controller of ClassB does not get updated. Can you please suggest, why is it so?

谢谢!

在语句中: classBObject.textLabel.text = temp;//声明与第1部分有所不同.,我在复制粘贴时错过了 .text .请原谅我.

In the statement: classBObject.textLabel.text = temp; // statement changed from Part 1., I had missed .text while copy pasting. Please excuse me for that.

推荐答案

第二种技术不正确的原因(除了 textLabel 末尾缺少的 .text 之外))是,当您从B类初始化程序返回时,无疑尚未创建与 textLabel 相对应的基础 UILabel .如果您查看此

The reason that the second technique is incorrect (besides the missing .text at the end of textLabel) is that when you return from the class B initializer, the underlying UILabel corresponding to textLabel undoubtedly has not been created yet. If you look at this diagram you'll see that the view configuration is not completed at the end of the the initialization methods, but rather upon access. So you must defer any access of the user interface controls until viewDidLoad.

更新:

运行以下代码时,我在日志中得到"0x0",证明第二个视图上的 UILabel 仍为 nil 且尚未初始化,正如我所期望的那样.当我的第二个控制器中的 viewDidLoad viewDidLoad 中设置 self.textLabel.text = self.dataToDisplay 时,它的工作原理类似于冠军(因为它确实为你).但是 UILabel IBOutlet 属性直到 viewDidLoad 才可靠.

When I run the following code, I get "0x0" in my log, proving that the UILabel on my second view is still nil and has not been initialized yet, as I would have expected. When the viewDidLoad in my second controller sets self.textLabel.text = self.dataToDisplay in viewDidLoad, it works like a champ (as it does for you). But the UILabel IBOutlet property is just not reliable until viewDidLoad.

SecondViewController *controller = [[SecondViewController alloc] init];

NSLog(@"%p", controller.textLabel);

controller.dataToDisplay = @"from first view";

[self presentViewController:controller animated:YES completion:nil];

这篇关于类之间传递数据的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆