委任について質問があります。私はプログラミングにかなり慣れていませんが、委任を使用する最初の演習プロジェクトを管理したばかりです。それはすべて、テキストフィールドとボタンを持つ子ビューコントローラーを呼び出す親ビューコントローラーです。そのテキスト フィールドに何かを書き込んでボタンを押すと、子 VC が閉じられ、テキストが返されます。その後、親のラベルに表示されます。トランジション用のアニメーションをいくつか用意しました。それはすべて機能します。最初にいくつかの写真とコードを次に示します。
これは私の親View Controllerです:
ボタンをクリックすると、子が表示されます。
テキスト フィールドに何かを書き、ボタンを押してそれを返します。
データは、親ビュー コントローラーのラベルに表示されます。
このためのファイルは次のとおりです。
ParentViewController.h
#import <UIKit/UIKit.h>
#import "ChildViewController.h"
@interface ParentViewController : UIViewController <ChildViewControllerDelegate>
@property (strong, nonatomic) IBOutlet UILabel *label;
@end
ParentViewController.m
@implementation ParentViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)Pressed:(id)sender {
ChildViewController *childViewController = [[ChildViewController alloc]init];
[self displayContentController:childViewController];
childViewController.delegate = self;
}
- (void) displayContentController: (UIViewController*) content {
[self addChildViewController:content];
content.view.frame = CGRectMake(0, 115, 320, 240);
content.view.backgroundColor = [UIColor redColor];
CATransition *transition = [CATransition animation];
transition.duration = 1;
transition.type = kCATransitionPush;
transition.subtype = kCATransitionFromLeft;
[transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[content.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
-(void) passDataBack:(NSString *)data{
self.label.text = data;
}
@end
ChildViewController.h
#import <UIKit/UIKit.h>
@protocol ChildViewControllerDelegate <NSObject>
-(void)passDataBack:(NSString*)data;
@end
@interface ChildViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextField *txtfield;
@property (nonatomic, weak) id <ChildViewControllerDelegate> delegate;
- (IBAction)passingBack:(id)sender;
@property NSString *data;
@end
ChildViewController.m
#import "ChildViewController.h"
@interface ChildViewController ()
@end
@implementation ChildViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)passingBack:(id)sender {
NSString *itemToPassBack = self.txtfield.text;
[self.delegate passDataBack:itemToPassBack];
[self.childViewControllers lastObject];
[self willMoveToParentViewController:nil];
[UIView animateWithDuration:1
delay:0.0
usingSpringWithDamping:1
initialSpringVelocity:1
options:UIViewAnimationOptionCurveEaseIn
animations:^{
self.view.frame = CGRectMake(-320, 115, 320, 240);
} completion:^(BOOL complete){
[self.view removeFromSuperview];
[self removeFromParentViewController];
}];
}
@end
コードに関しては、よくわからないことがいくつかあります。多くのコードをコピーしたことなどを学んでいるので、たとえば、同じ方法を使用して子コントローラーをアニメーション化する方法がわかりません以前にアニメーション化していたように、またはそれが重要であり、ベストプラクティスは何かなど...また、コンソールに奇妙なメッセージが表示されます。「Interface BuilderファイルのクラスViewControllerが不明です。」しかし、それは機能するので、なぜそこにあるのかわかりません。記録のために、私はストーリーボードを使用しておらず、xibs のみを使用しています。
私の質問は次のとおりです。次のように、子ビューコントローラーではなく親で子ビューコントローラーを閉じるボタンが必要です。
そして、それが私が迷っているところです。親ビューコントローラーからデータを返すメソッドをトリガーする方法がわかりません。誰でも助けることができますか?どうもありがとう





