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

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

问题描述

有没有人知道如何从子视图(子)视图控制器更新属性值?
我有一个名为statusid的int属性,在父视图控制器中使用gettor / settor定义。
[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

ParentView.h

ParentView.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,如下所示: / p>

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

delegate.statusID = 1337;

希望这会有所帮助

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

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