了解授权

时间:2016-01-24 14:23:26

标签: objective-c delegation childviewcontroller parentviewcontroller

我想了解代表团。我写了一个小项目试图解决这个问题。我也得到了S.O.的帮助。我被困在最后一部分。我的项目很简单。我们有一个主视图控制器,它有一个“开始”按钮。此按钮触发一个挂钩到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

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

用于容器视图的文件:

#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

容器视图的文件:

#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

enter image description here

这是一幅它的样子。请注意,“选择”按钮只是一个临时的按钮,我放在那里以确保所有内容都挂钩,并且我可以使用容器中的按钮注销所选颜色。我想要做的是能够将该颜色传递给父视图控制器。因此,在我用其选择器视图关闭容器之后,我存储了选择了什么颜色的数据。我得到了S.O.的帮助。并且他在帮助我开始这项工作方面做得很好。我唯一不理解的是当我收到数据时,在父文件的m文件末尾会发生什么:

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

这是一个明显的noob问题,但我确实需要它拼写出来。我如何实际访问父级中的数据。我在评论部分询问,回复是(我正在改变课程,原来是uicolor):

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

我试着逐字逐句地写“NSString * myReceivedColor = theValue;”但是“theValue”是无法识别的。

最终,我想要的是将数据传回父级,这样当我按下“后退”按钮时,在父级中,“你选择”的标签会以所选颜色更新“。

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

UPDATE ---------------------------------------------- -------------------------

所以,我正在看的是,在我的方法结束时添加“后退”按钮,

 - (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

1 个答案:

答案 0 :(得分:0)

看起来你的代表是零。

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

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

(BTW这是一个容易犯的错误,很多次你在想“为什么这不起作用?”检查是否设置了委托属性)

修改

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

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

我所说的selectedCol操作也就是你在调用你的委托的地方 - 你的'后退'动作不会调用这个方法。