【问题标题】:Set up Protocol with Modal View Controller -> ViewController使用 Modal View Controller -> ViewController 设置协议
【发布时间】:2013-10-22 18:40:48
【问题描述】:

我有一个初始的ViewController,我们称之为HomeViewController,它有一个调用模态视图控制器的按钮,我们称之为ModalViewController。在ModalViewController 里面,我有一个包含两个部分的表格视图。如果您单击第 0 部分中的任何单元格,它会将信息发送回HomeViewController(这部分我正在使用协议)。如果您单击第 1 节中的任何单元格,它会推送到另一个带有选项的视图控制器,我们称之为OptionsViewController。这对我来说很棘手。如果您单击任何这些选项,请关闭OptionsViewController 并关闭ModalViewcontroller 并将该信息发送到HomeViewController,就像ModalViewController 到HomeViewController 一样。我试图建立一个类似于ModalViewController 的协议,但它从未被调用过。

OptionsViewController 协议 & .h 文件

@protocol OptionsViewControllerDelegate <NSObject>
@optional
-(void) optionsInfo:(NSArray *)optionsViewArray;

@end

@interface OptionsViewController : UITableViewController
@property (retain) id delegate;
@property (nonatomic, strong) NSArray *sendArray;
@end

OptionsViewController.m 调用它以从堆栈中弹出。

{
    [self dismissOptionsView];
}
-(void) viewWillDisappear:(BOOL)animated
{
    NSLog(@"Send Array: %@", self.sendArray);
    [[self delegate] optionsInfo:sendArray];
}
-(void)dismissOptionsView
{
    [self.navigationController popViewControllerAnimated:YES];
}

在 ModalViewController.h 中

@protocol ModalViewControllerDelegate <NSObject>
@optional
-(void) sendInformation:(NSArray *)infoArray;
@end

@interface ModalViewController : UITableViewController <ConditionsViewControllerDelegate, UISearchBarDelegate>
{
    UISearchBar *searchDrugBar;
}
@property (retain) id delegate;
@property (nonatomic, strong) IBOutlet UISearchBar *searchDrugBar;
@property (nonatomic, strong) NSArray *infoArray;
@end

ModalViewController.m 其中 OptionsInfo 应该进来。

-(void) optionsInfo:(NSArray *)optionsViewArray
{
    //IT NEVER REACHES HERE :(
    NSLog(@"HERE");
    self.infoArray = optionsViewArray;
    NSLog(@"Finished");
    [self dismissViewControllerAnimated:YES completion:nil];
}

有没有人做过类似的事情或知道解决方案?任何有关正确方向的信息、链接、指导等将不胜感激。提前致谢。

【问题讨论】:

    标签: ios objective-c uiviewcontroller protocols


    【解决方案1】:

    您需要在下面的 OptionsViewController 中设置委托:-

    在您的 OptionsViewController.m 中,在您的方法中包含以下行

     [self setDelegate:ModalViewController];
    

    【讨论】:

    • 下面哪个方法? [self dismissOptionsView]; 或 [[self delegate] optionsInfo:sendArray];。还使用ModalViewController 意味着我必须在OptionsViewController.h 中使用#import "ModalViewController.h",对吗?我认为我们不允许使用协议、递归所有权或类似的东西来做这件事。
    • 是正确的,你必须导入 ModalViewController.h,因为除非你不会设置委托。你的方法不会被调用
    • 完成。现在我收到一个错误Cannot find protocol declaration for 'OptionsViewControllerDelegate', did you mean 'UISplitViewControllerDelegate' ? I am getting that error on the @interface for ModalViewController &amp;&amp; on @property (nonatomic, unsafe_unretained) id delegate1;`知道那是什么意思吗?
    • 我认为你需要像这样在 OptionsViewController.h 中声明你的委托属性 @property (retain) id delegate;
    • 我修复了这个错误,必须将协议放在#imports 之上。这修复了错误,但现在当我运行它时,它仍然没有 NSLog 或进入 optionInfo 方法。
    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多