将变量发送到添加的子视图视图控制器
我知道如何从您的父视图中检索变量...... 这是通过做这样的事情
[(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,如果您已经像这样在子视图中设置了变量的属性。
<代码>
@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
你有
Message
的 setter 吗?如果不是的话,它是财产吗?不管怎样,我都会有一个 getter 方法setMessage:(NSString *)msg
,其实现如下:这样,您就可以将变量的赋值与视图的更新联系起来。
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:This way, you're tying the assignment of the variable with the update of the view.
您可以为 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
.