如何在视图之间传递文本

发布于 2024-09-11 17:51:17 字数 70 浏览 10 评论 0原文

我发表了 2 次观点 我想将主视图上的标签文本发送到子视图 想要将其打印在另一个标签的文本值上...... 如何传递该文本

i made 2 views
and i want to send text of label on main view to sub view to
an want to print it there on another label's text value....
how to pass that text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-09-18 17:51:17

我不会使用单例模式或任何其他“全局变量”。这将使您的视图控制器非常紧密地耦合并限制可重用性。我只需在第二个视图控制器中创建一个实例变量,并在呈现视图之前将其设置在主视图控制器中。

然后,第二个视图控制器将 label.text 设置为(例如)viewDidLoad 中的实例变量。

这样,第二个视图控制器不依赖于任何“全局变量”或包含,并且将更加可重用。

//SecondViewController.h
@interface SecondViewController : UIViewController {
    NSString *theLabel;
}

@property(nonatomic, copy) NSString *theLabel; //Synthesize in implementation

@end

然后在主视图控制器中:

//Create instance of secondViewController
instanceOfSecondViewController.theLabel = @"Nut";
//Present the second view here

I wouldn't use a singleton pattern or any other 'global variable'. This will make your view controllers very tightly coupled and restricts reusability. I would just create a instance variable in the second view controller and set this in the main one before presenting the view.

The second view controller then sets the label.text to the instance variable in (for example) viewDidLoad.

This way, the second view controller doesn't depend on any 'globals' or includes and will be more reusable.

//SecondViewController.h
@interface SecondViewController : UIViewController {
    NSString *theLabel;
}

@property(nonatomic, copy) NSString *theLabel; //Synthesize in implementation

@end

Then in the main view controller:

//Create instance of secondViewController
instanceOfSecondViewController.theLabel = @"Nut";
//Present the second view here
乖乖哒 2024-09-18 17:51:17

如果类 A 处理您的 view1,类 B 处理 view2,则在类 B 中定义一个接口以接受您的其中一个 UI 元素的新标签,然后从类 A 调用该接口。

If class A handle your view1 and Class B handle view2 then define a interfaces in class B to accept new label to your one of the UI element then call that interface from class A.

总以为 2024-09-18 17:51:17

查看单例模式。

我的 Objective-C 单例应该是什么样子?

然后你可以做类似的事情:

//view1 
#import "SingletonClass.h"
...
[SingletonClass sharedInstance].savedText = @"blah";

并且

//view2
#import "SingletonClass.h"
...
lbl.text = [SingletonClass sharedInstance].savedText;

Look into the Singleton pattern.

What should my Objective-C singleton look like?

Then you could do something like:

//view1 
#import "SingletonClass.h"
...
[SingletonClass sharedInstance].savedText = @"blah";

and

//view2
#import "SingletonClass.h"
...
lbl.text = [SingletonClass sharedInstance].savedText;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文