从子viewcontroller设置父viewcontroller类的属性值?

时间:2009-09-02 16:26:02

标签: objective-c iphone uiviewcontroller scope

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

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

2 个答案:

答案 0 :(得分:6)

使用“super”访问您的基类,即当前类继承自

的基类

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

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中

#import "ParentView.h"

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

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

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

ChildView *newChild = [[ChildView alloc] init];
newChild.delegate = self;
通过这样做,您可以在ChildView.m中访问ParentView的“statusID”,如下所示:

delegate.statusID = 1337;

希望这会有所帮助

答案 1 :(得分:0)

在super上调用方法调用超类的方法实现,它会调用superview的/ super视图控制器的实现。

您需要在子视图控制器中保留对父级的引用,并在父级上调用setStatusId:方法,或者在两者之间创建委托模式,以便让子级委托(可能设置为父级)知道状态ID已更改。

相关问题