如何在另一个视图(目标c)中传递更改变量的值?
有两个控制器
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题是,在
view2
的switchView2
方法中,您正在调用self
并尝试更改 ivar (或属性,不确定它是哪个)从显示的代码来看)是一个不同类。如果您想将某些内容从一个实例发送到另一个实例,您必须首先在它们之间建立某种连接。有几种方法可以让他们开口说话。能够传递此类内容的更好方法是设置委托协议或使用通知,然后view2
可以调用在view1
中进行更改的委托方法或发布一条通知,view1
将被注册,以便它可以进行更改。实现此目的不太理想的方法是为view2
提供一个 ivar(或属性),即view1
。然后您就可以使用类似view1.bg = @"blueButton.png";
的内容。Your problem is that in
view2
'sswitchView2
method you are callingself
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, thenview2
could call the delegate method that makes the change inview1
or post a notification thatview1
would be registered for so that it could make the change. The less desirable way to accomplish this would be to giveview2
an ivar (or property) that isview1
. You would then be able to use something likeview1.bg = @"blueButton.png";
.