使用委派无效的解雇视图

时间:2012-07-13 16:16:06

标签: objective-c ios uiviewcontroller delegates protocols

我正在尝试使用我所熟悉的代表团来驳回模态呈现的观点。我试图让它按照here看到的苹果文档的方式工作。到目前为止,我的代码如下:

  1. 将两个视图放在故事板上,使用模态segue连接第一个到第二个视图。 (segue到view2工作正常)

  2. 在第二个viewcontroller / create方法中创建委托,以便在返回时调用:

    //inside of view2ViewController.h
    @class view2ViewController;
    @protocol view2ViewControllerDelegate <NSObject>
    -(void)goBack:(OptionsViewController *)controller;
    @end
    
    @interface OptionsViewController : UIViewController
    @property (nonatomic, weak) id <view2ViewControllerDelegate>delegate;
    - (IBAction)return:(id)sender;//connected to button
    @end
    
  3. 在view1ViewController @interface view1ViewController : UIViewController <view2ViewControllerDelegate>

  4. 中实现委托
  5. 在view1Controller.m

    中编写委托方法goBack的代码
    -(void)goBack:(view2ViewController *)controller{
    [self dismissViewControllerAnimated:YES completion:nil];}
    
  6. 通过在view2ViewController.m中编写返回方法的代码来完成

    - (IBAction)return:(id)sender {
    [self.delegate goBack:self];}
    
  7. 我不确定这段代码出错的地方。调用return方法,但goBack则不然​​。我确实阅读了开发者文档,并且认为我理解,但我猜不是......

    PS我将StackOverflow上所有类/变量名的名称更改为更通用,因此如果变量名拼写之间存在轻微差异,可能是因为我键入了一个错误。

1 个答案:

答案 0 :(得分:1)

我可以尝试的最佳镜头 -

确保将SplashViewController指定为view2ViewController的委托。

通过代码你可以这样做(在SplashViewController m文件中):

view2ViewController.delegate = self;

或者你可以在故事板上做。

<强>顺便说一句 我不确定称你的功能“返回”是个好主意。