将变量发送到添加的子视图视图控制器

发布于 2024-10-11 12:10:44 字数 400 浏览 1 评论 0原文

我知道如何从您的父视图中检索变量...... 这是通过做这样的事情

[(mainViewController *)[self.superview] variableName];

但现在我想知道如何将变量数据发送到子视图...

特别是如果我在 screenTwo 中执行类似的操作

ScreenTwoViewController *screen2 = [[ScreenTwoViewController alloc] init];
...
...
[self.addSubview:screen2.view];

,我有一个变量名称消息:

我想将变量消息发送到实际视图,以便它可以显示该视图中的消息基于将 screen2 视图添加为子视图时父视图发送的内容?

I know how to retrieve variables from your parent view...
which is by doing something like this

[(mainViewController *)[self.superview] variableName];

but now i would like to know how to send variable data to a subview...

specially if i do something like this

ScreenTwoViewController *screen2 = [[ScreenTwoViewController alloc] init];
...
...
[self.addSubview:screen2.view];

in screenTwo i have a variable name Message:

I would like to send a variable message to the actual view so it can display the message in that view based on what the parent view sends it when adding the screen2 view as a subview??

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

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

发布评论

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

评论(3

烟燃烟灭 2024-10-18 12:10:44

好吧,如果您已经像这样在子视图中设置了变量的属性。

<代码>
@property (非原子,保留) NSString *message;

并将其合成到您的 .m 文件中,这样您就可以始终使用此


screen2.message = [[NSString alloc] initWithFormat:@"使用你想要的初始化方法"];

并且不要忘记在方法结束之前释放 screen2 以避免内存泄漏

Well if you have set the property for the variables in the subview like this.


@property (nonatomic, retain) NSString *message;

and also synthesis it in your .m file than you can always use this


screen2.message = [[NSString alloc] initWithFormat:@"use what ever init methode that you want"];

and dont forget to release the screen2 before the end of the method to avoid memory leaks

冷心人i 2024-10-18 12:10:44

你有 Message 的 setter 吗?如果不是的话,它是财产吗?不管怎样,我都会有一个 getter 方法 setMessage:(NSString *)msg ,其实现如下:

- (void)setMessage:(NSString *)msg {
    [message release];
    message = [msg retain];
    // put your code to modify your UI here
}

这样,您就可以将变量的赋值与视图的更新联系起来。

Do you have a setter for Message? If not, is it a property? Either way, I would have a getter method, setMessage:(NSString *)msg with the following implementation:

- (void)setMessage:(NSString *)msg {
    [message release];
    message = [msg retain];
    // put your code to modify your UI here
}

This way, you're tying the assignment of the variable with the update of the view.

小帐篷 2024-10-18 12:10:44

您可以为 ScreenTwoViewController 创建一个新的 init 方法。例如,- (id) initWithMessage:(NSString *)message

要更新消息,只需创建一个 setter 方法 - (void)setMessage:(NSString *)newMessage

You could create a new init method for ScreenTwoViewController. For example, - (id) initWithMessage:(NSString *)message.

To update the message just create a setter method - (void)setMessage:(NSString *)newMessage.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文