我有一个嵌入 View ,在另一个 View 中,我如何在 Childs viewController 中获取父 viewController 的当前实例,需要它在当前实例的父级上调用方法。
这就是我当前调用父方法的方式,这会分配一个新实例。
CalendarMonthViewParent *controller = [[CalendarMonthViewParent alloc] init];
[controller callChildChange];
如果我这样调用它,我得到“没有可见接口(interface)声明“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
...
}
最佳答案
只需在子ViewController
中调用self.parentViewController
关于ios - 获取 "Parent" View Controller 的当前实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18161966/