如何在另一个视图(目标c)中传递更改变量的值?

发布于 2024-11-10 12:12:01 字数 307 浏览 0 评论 0原文

有两个控制器

我在 view1.hi 中

NSString *bg;
@interface Lottery5ViewController : UIViewController {...}

,在 view2.mi 有,

-(IBAction) switchView2{
self.bg = @"blueButton.png";
}

但它无法编译并说 请求非结构或联合中的成员“bg”

我只想从 view2 修改 view1 中变量的值

i have two controller

in view1.h i have

NSString *bg;
@interface Lottery5ViewController : UIViewController {...}

in view2.m i have

-(IBAction) switchView2{
self.bg = @"blueButton.png";
}

but it cannot compile and said
request for member 'bg' in something not a structure or union

i just want to modify the value of a variable in view1 from view2

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

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

发布评论

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

评论(1

陌若浮生 2024-11-17 12:12:01

您的问题是,在 view2switchView2 方法中,您正在调用 self 并尝试更改 ivar (或属性,不确定它是哪个)从显示的代码来看)是一个不同类。如果您想将某些内容从一个实例发送到另一个实例,您必须首先在它们之间建立某种连接。有几种方法可以让他们开口说话。能够传递此类内容的更好方法是设置委托协议或使用通知,然后 view2 可以调用在 view1 中进行更改的委托方法或发布一条通知,view1 将被注册,以便它可以进行更改。实现此目的不太理想的方法是为 view2 提供一个 ivar(或属性),即 view1。然后您就可以使用类似 view1.bg = @"blueButton.png"; 的内容。

Your problem is that in view2's switchView2 method you are calling self and trying to change an ivar (or property, not sure which it is from the code shown) of a different class. If you want to send something from one instance to another you have to establish some sort of connection between them first. There are a few ways you can get them talking. The better ways to be able to pass something like this is to set up a delegate protocol or use notifications, then view2 could call the delegate method that makes the change in view1 or post a notification that view1 would be registered for so that it could make the change. The less desirable way to accomplish this would be to give view2 an ivar (or property) that is view1. You would then be able to use something like view1.bg = @"blueButton.png";.

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