1

我试图理解代表团。我写了一个小项目来尝试解决这个问题。我也得到了帮助,所以我被困在了最后一部分。我的项目很简单。我们有一个主视图控制器,它有一个“开始”按钮。此按钮触发一个与 ContainerViewController 挂钩的容器视图。我做了一个小动画让容器从侧面滑动。我有另一个按钮“返回”,它使容器视图随着相反的动画消失。注意,我复制了很多代码,其余的都是我学习的,所以可能有不必要的行,请随时评论。

ViewController.h

#import <UIKit/UIKit.h>
#import "ContainerViewController.h"

@interface ViewController : UIViewController <ContainerViewControllerDelegate>

- (IBAction)Start:(id)sender;
- (IBAction)back:(id)sender;

@end

这是m文件:

#import "ViewController.h"

@interface ViewController ()

@property UIViewController *childView;
@property NSString *myReceivedValue;
@property ContainerViewController *controller;
@property IBOutlet UILabel *myLabel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.childView = [self.storyboard instantiateViewControllerWithIdentifier:@"childVC"];
   self.controller = [self.storyboard instantiateViewControllerWithIdentifier:@"childVC"];
    self.controller.delegate = self;

    self.childView = [self.childViewControllers lastObject];
    [self.childView.view removeFromSuperview];
    [self.childView removeFromParentViewController];
    self.childView.view.userInteractionEnabled = NO;

    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Start:(id)sender {

    self.childView.view.frame = CGRectMake(0, 84, 320, 210);

    [self.childView didMoveToParentViewController:self];

    CATransition *transition = [CATransition animation];
    transition.duration = 1;
    transition.type = kCATransitionPush;
    transition.subtype = kCATransitionFromLeft;
    [transition setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
    [self.childView.view.layer addAnimation:transition forKey:nil];

    [self.view addSubview:self.childView.view];
    self.childView.view.userInteractionEnabled = YES;

}

- (IBAction)back:(id)sender {

    [self.childView willMoveToParentViewController:nil];

    [UIView animateWithDuration:1
                          delay:0.0
         usingSpringWithDamping:1
          initialSpringVelocity:1
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{

                         self.childView.view.frame = CGRectMake(-320, 84, 320, 210);

                     } completion:^(BOOL complete){

                         [self.childView removeFromParentViewController];
                     }];

}

- (void) passValue:(NSString *) theValue
{
  // here is where you receive the data
}
@end

好的,所以容器视图有一个pickerView,它是它的委托,这个pickerView只有一个十种颜色的数组可供选择:

容器视图的 h 文件:

#import <UIKit/UIKit.h>


@protocol ContainerViewControllerDelegate;

@interface ContainerViewController : UIViewController <UIPickerViewDelegate>

@property NSArray *colors;

@property (weak)id <ContainerViewControllerDelegate> delegate;

@property (weak, nonatomic) IBOutlet UIPickerView *myPickerView;

- (IBAction)chosenCol:(id)sender;

@end

@protocol ContainerViewControllerDelegate <NSObject>

- (void) passValue:(NSString *) theValue;

@end

容器视图的 m 文件:

#import "ContainerViewController.h"

@interface ContainerViewController ()

@property NSString *selValue;

@end

@implementation ContainerViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.colors = [[NSArray alloc] initWithObjects:@"blue", @"red", @"green", @"purple", @"black", @"white", @"orange", @"yellow", @"pink", @"violet", nil];

    }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

        return 10;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    return self.colors[row];

}

- (IBAction)chosenCol:(id)sender {

    [self.delegate passValue:[self.colors objectAtIndex:[self.myPickerView selectedRowInComponent:0]]];

}
@end

在此处输入图像描述

这是它的样子的图片。请注意,“选择”按钮只是我放在那里的一个临时按钮,以确保一切正常,并且我可以使用容器中的按钮注销选择的颜色。我想要做的是能够将该颜色传递给父视图控制器。因此,在我使用它的选择器视图关闭容器后,我存储了选择什么颜色的数据。我从 SO 的某个人那里得到了帮助,他在帮助我启动这方面做得非常好。我唯一不明白的是当我收到数据时会发生什么,在父级的 m 文件末尾:

  - (void) passValue:(NSString *) theValue
    {
      // here is where you receive the data
    }

这是一个明显的菜鸟问题,但我真的需要把它拼出来。我如何实际访问父级中的数据。我在评论区问,回复是(我在换班,原来是uicolor):

“不,您将在方法内收到数据 - (void) passValue:(NSString *) theValue; 在该方法中放置一个断点以确保它正常工作,您可以像这样访问它: NSString *myReceivedColor = theValue;

我试着一个字一个字地写“NSString *myReceivedColor = theValue;” 但“theValue”未被识别。

最终,我想要的是将数据传递回父级,以便当我点击“返回”按钮时,在父级中,“您选择”的标签会更新为所选颜色”。

我以前从未接触过代表团,所以我迷路了。一个慈善的灵魂能花时间用非常明显的术语来解释这最后一点吗?非常感谢

更新 - - - - - - - - - - - - - - - - - - - - - - - - - ----------------------

所以,我正在看的是在我的“后退”按钮方法的末尾添加,

 - (IBAction)back:(id)sender {

        [self.childView willMoveToParentViewController:nil];

        [UIView animateWithDuration:1
                              delay:0.0
             usingSpringWithDamping:1
              initialSpringVelocity:1
                            options:UIViewAnimationOptionCurveEaseIn
                         animations:^{

                             self.childView.view.frame = CGRectMake(-320, 84, 320, 210);

                         } completion:^(BOOL complete){

                             [self.childView removeFromParentViewController];
                         }];

几行:

        self.myReceivedValue = theValue;

        self.myLabel.text = self.myReceivedValue;
}

为了能够将 myLabel 的文本更新为我在视图容器中选择的颜色。它返回错误:“使用未声明的标识符“theValue”。这对我来说是全新的,所以我只是复制人们在 SO 上所说的话,希望最终能理解。我在这里做错了什么?tx

4

1 回答 1

0

看起来你的代表是零。

self.childView = [self.storyboard instantiateViewControllerWithIdentifier:@"childVC"];
self.controller = [self.storyboard instantiateViewControllerWithIdentifier:@"childVC"];
self.controller.delegate = self;

您创建两个“childVC”实例(可能是复制/粘贴错字?)然后将委托设置在“控制器”上,但您使用“childView”。只需将 childView 属性更改为 ContainerViewController 并设置 self.childView.delegate=self。

(顺便说一句,这是一个简单的错误,很多时候当你在想“为什么这不起作用??”检查委托属性是否已设置)

编辑

您正在记录的返回值属性是 nil b/c 您从未设置它。你必须实现委托方法,即

-(void) passValue:(nsstring*)theValue
{
self.receivedValue = theValue
}

另外,我所说的 selectedCol 操作是您调用委托的地方 - 您的“返回”操作不会调用此方法。

于 2016-01-24T15:11:40.403 回答