ios - 获取 "Parent" View Controller 的当前实例

标签 ios objective-c

我有一个嵌入 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/

相关文章:

ios - 现有目标没有 'swift compiler code generation' 部分

ios - 更改 FPopover Controller 的颜色

ios - 显示来自新 xib 中 nsstring 中的 url 的图像

ios - Plist 使用可编码和静态变量来构造类

iOS - UIView animateWithDuration 中的完成 block 被过早调用

iphone - NString 显示 NSDate 是 000 而不是 iphone 应用程序中的实际日期

ios - CMPedometerData 中的结束日期不正确

ios - 父 UIView 不包含/剪辑 subview

iphone - UIView 调整 subview 大小

ios - 两 ListView ?