0

別のビュー内にビューが埋め込まれています。子ビューコントローラーで親ビューコントローラーの現在のインスタンスを取得するにはどうすればよいですか。現在のインスタンスで親のメソッドを呼び出す必要があります。

これは、現在親でメソッドを呼び出す方法です。これにより、新しいインスタンスが割り当てられます。

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

...

}
4

3 に答える 3

0

私が見る限り、parentViewController のインスタンスを取得して、「こんにちは、私はあなたの childViewController です。変更されました」というメッセージを送信しようとしています。

そうではなく、プロトコルを実装し、ChildViewControllerDelegateそのプロトコルに準拠するように ParentViewController を変更することをお勧めします。

次に、 childViewController で次のようなものを呼び出す必要があります[delegate childViewControllerChanged:self];

しかし、プログラムではなく追加されたView Controllerのデリゲートを設定する方法がわかりません。あなたが方法を知っていれば-幸運を祈ります:)

于 2013-08-10T13:31:24.323 に答える