从子视图控制器设置父视图控制器类的属性值? [英] Setting property value of parent viewcontroller class from child viewcontroller?

查看:26
本文介绍了从子视图控制器设置父视图控制器类的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何从子视图(子)视图控制器更新属性值?我在父视图控制器中使用 gettor/settor 定义了一个名为 statusid 的 int 属性.[self.view addSubview:detailsVC.view];

Does anyone know how to update a property value from the subview (child) view controller? I have a int property called statusid defined with gettor/settor in parent view controller. [self.view addSubview:detailsVC.view];

在子子视图中,我尝试调用 [super statusid:updatedValue];将 statusid 更新为新值,但这会产生错误.如何更新父级中的 statusid?有人知道怎么做吗?

In the child subview, I trying calling [super statusid:updatedValue]; to update statusid to a new value, but this creates an error. How can i update statusid in the parent? Anyone know how to do this?

推荐答案

使用super"可以访问基类,即当前类继承的基类

with "super" you access your base class, the one your current class has inherited from

要执行您所解释的操作,您需要访问父视图的一个属性,这相当复杂,因为这很可能以两个类尝试相互引用而告终.因此,您很可能必须创建一个委托模式,看起来有点像这样

to do what you've explained, you need to access a property of your parent view, which is rather complicated since this will most likely end with both classes trying to reference each other. thus you will most likely have to create a delegate pattern, looking somewhat like this

父视图.h

@protocol IAmYourFatherAndMotherProtocol

@class ChildView;

@interface ParentView : UIViewController <IAmYourFatherAndMotherProtocol>
{
NSInteger statusID;
}

@property (nonatomic) NSInteger statusID;

@protocol IAmYourFatherAndMotherProtocol
@property (nonatomic) NSInteger statusID;
@end

@end

在 ChildView.h 中

in ChildView.h

#import "ParentView.h"

@interface ChildView : UIViewController
{
  id<IAmYourFatherAndMotherProtocol> delegate;
}

@property (nonatomic, assign) id <IAmYourFatherAndMotherProtocol> delegate;

在ParentView.m中创建您的ChildView时,您必须将self"设置为委托,例如:

when creating your ChildView in ParentView.m, you have to set "self" as delegate, eg:

ChildView *newChild = [[ChildView alloc] init];
newChild.delegate = self;

通过这样做,您可以像这样在 ChildView.m 中访问 ParentView 的statusID":

by doing so, you can access "statusID" of your ParentView in ChildView.m like this:

delegate.statusID = 1337;

希望能帮到你

这篇关于从子视图控制器设置父视图控制器类的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆