我有一个嵌入视图,在另一个视图中,如何在Childs viewController中获取父视图的当前实例,需要它在当前实例上调用父方法。
这就是我当前在父节点上调用方法的方法,这将分配一个新实例。
CalendarMonthViewParent *controller = [[CalendarMonthViewParent alloc] init];
[controller callChildChange];
我得到“没有可见的接口声明”callChildChange“,如果我这样称呼它:
[self.parentViewController callChildChange];
编辑1:
CalendarMonthViewParent.h
#import <UIKit/UIKit.h>
@interface CalendarMonthViewParent : UIViewController
-(void) callChildChange;
@property (strong, nonatomic) IBOutlet UINavigationItem *skemaNavigationItem;
@end
CalendarMonthViewParent.m
- (void)callChildChange { // this is called from the child
UINavigationItem *navBar = [self skemaNavigationItem];
NSLog(@"logging navItem: %@", navBar); // this logs null when called from the child, since its called on a new instance and not the old one, if i call this from the parent directly, it is not null
}
CalendarMonthViewChild.m
- (void) calendarMonthView:(TKCalendarMonthView*)monthView didSelectDate:(NSDate*)date{
NSLog(@"Date Selected: %@",date);
...
CalendarMonthViewParent *controller = [[CalendarMonthViewParent alloc] init];
[controller callChildChange]; // this calls it on a new instance, i need it to call it on the existing instance
...
}
答案 0 :(得分:3)
只需致电孩子self.parentViewController
ViewController
即可
答案 1 :(得分:0)
据我所知,你试图让parentViewController的实例只发送消息“你好,我是你的childViewController,我被改变了。”
我建议你实现ChildViewControllerDelegate
协议,而不是这样做,而是更改你的ParentViewController,使其符合该协议。
然后,在您的childViewController中,您需要调用类似[delegate childViewControllerChanged:self];
但我不知道如何为视图控制器设置委托,而不是以编程方式添加。如果你知道如何 - 祝你好运:)
答案 2 :(得分:0)
我通过从“自定义容器ViewController - Adding and Removing a Child”示例列表中注释1和3行来实现此目的:
- (void) displayContentController: (UIViewController*) content;
{
[self addChildViewController:content]; // 1
content.view.frame = [self frameForContentController]; // 2
[self.view addSubview:self.currentClientView];
[content didMoveToParentViewController:self]; // 3
}
并将它们放入prepareForSegue:sender:用于在故事板中嵌入视图控制器的容器控制器类。
因此
NDOMasterViewController *viewController = (NDOMasterViewController *)[segue destinationViewController];
[self addChildViewController:viewController]; // 1
[viewController didMoveToParentViewController:self]; // 3
现在在嵌入式视图控制器中我可以使用self.parentViewController。