传递数据和委托

时间:2016-01-28 17:03:09

标签: objective-c delegation childviewcontroller parentviewcontroller

我对代表团有疑问。我对编程很新,我刚刚完成了第一个使用委托的练习项目。它只是一个父视图控制器,它调用一个子视图控制器,里面有一个文本字段和一个按钮。我在该文本字段中写了一些内容,按下按钮,子VC被解除并将文本传回。然后它会显示在Parent上的标签中。我有一些过渡动画。一切正常。以下是一些图片和代码:

这是我的父视图控制器:

enter image description here

单击按钮,出现Child:

enter image description here

在文本字段中写一些内容,然后按按钮将其传回:

enter image description here

数据显示在父视图控制器的标签上。

以下是此文件:

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

关于代码,有一些我不确定的事情,因为我在学习我复制了很多代码等...所以例如我不确定如何使用子控制器动画回来我用它来制作动画的方法,或者它是否重要,什么是最佳实践等...同样,我在控制台中收到一条奇怪的消息:&#34; Interface Builder文件中的未知类ViewController。&#34 ;但它确实有效,所以我不确定它为什么会这样。为了记录,我还没有使用故事板,只有xibs。

我的问题如下:我想让按钮解除子视图控制器不在子视图控制器中但在父视图中,如下所示:

enter image description here

enter image description here

enter image description here

那就是我迷失的地方。我只是不知道如何触发从父视图控制器传回数据的方法。有人可以帮忙吗?非常感谢

0 个答案:

没有答案