理解委托

Understanding delegation

我正在尝试了解委派。我写了一个小项目来尝试解决这个问题。我也得到了 S.O 的帮助。我被困在它的最后一部分。我的项目很简单。我们有一个主视图控制器,它有一个按钮 "start"。此按钮触发一个容器视图,该视图与 ContainerViewController 挂钩。我做了一个小动画让容器从侧面滑动。我有另一个按钮 "back",它使容器视图随着相反的动画消失。注意,我复制了很多代码,其余的在学习中补上,所以可能会有多余的行,请大家评论。

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

好的,所以 Container View 有一个 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

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

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

这是一个显而易见的菜鸟问题,但我确实需要详细说明。我如何实际访问父级中的数据。我在评论区问了,回复是(我正在改class,原来是uicolor):

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

我试着一个字一个字地写 "NSString *myReceivedColor = theValue;" 但 "theValue" 无法识别。

最终,我想要的是将数据传回父级,这样当我点击按钮 "back" 时,在父级中,标签 "you chose" 会更新为所选颜色".

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

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

因此,我正在查看的是在 "back" 按钮的方法末尾添加

 - (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"。这对我来说是全新的,所以我只是复制人们在 S.O 上所说的内容。希望最终能理解。我是什么这里做错了吗?tx

看起来你的代表是零。

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

您创建了 "childVC" 的两个实例(可能是 copy/paste 拼写错误?)然后在 'controller' 上设置委托,但您使用 'childView'。只需将 childView 属性 更改为 ContainerViewController 并设置 self.childView.delegate=self.

(顺便说一句,这是一个很容易犯的错误,很多时候你在想 "why isn't this working??" 检查委托 属性 是否已设置)

编辑

您正在记录的 return 值 属性 为零 b/c 您从未设置它。您必须实现委托方法,即

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

此外,我所说的 chosenCol 操作是您调用委托的地方 - 您的 'back' 操作不调用此方法。