将值从presentingViewController 传回presentingViewController

Posted

技术标签:

【中文标题】将值从presentingViewController 传回presentingViewController【英文标题】:pass value from presentedViewController back to presentingViewController 【发布时间】:2013-06-29 00:58:23 【问题描述】:

ios 6 制作 APP - 我有一个 mainViewController,当点击按钮时会显示一个新的 modalViewController。那个modalViewController 有一个tableView。当用户点击一个单元格时,我想获取该单元格的值并将其分配给 mainViewController 上的变量。

问题是我在 viewDidAppear 方法中输出时总是得到 NULL。

这是modalViewController中的委托方法:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 
    NSMutableString *event_id = [[self.events valueForKey:@"event_id"] objectAtIndex:indexPath.row];
    NSLog(@"EVENT ID = %@", event_id); //This gives me actuall ID


    [self.presentingViewController dismissViewControllerAnimated:YES completion:^
        [self.mainViewController setEventID:event_id];
    ];

如果你看到我使用完成块来设置它。在我的 mainViewController 中,我有一个 eventID 属性。在我的 modalViewController 我也有

@class MainViewController

@interface ModalViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate>
    IBOutlet UITableView *eventsTable;


@property (strong, nonatomic) MainViewController *mainViewController;

@end

由于某种原因,当我尝试在 viewDidAppear 中输出它时,我得到的 eventID 总是 NULL,但是当我第一次设置它时它确实输出了它。我在那里错过了什么??

任何帮助将不胜感激。

编辑:

在我的 MainViewController 中,我已经设置了其他委托,如下所示:

@interface MainViewController : UIViewController <UICollectionViewDataSource, UICollectionViewDelegate, UIAlertViewDelegate, CLLocationManagerDelegate>

使用协议委托方法,我将如何在那里添加我的自定义委托声明,如 ModalViewControllerDelegate。

【问题讨论】:

为什么要在-dismissViewControllerAnimated:completion:的完成块中设置事件ID,而不是在关闭视图之前设置?另外,您确定mainViewController 实际上持有对主视图控制器的有效引用吗?是在 -viewDidAppear 之前还是之后调用完成块? 我已经把它放在完成块中,因为我在关闭模式视图控制器之前尝试过设置它,但这也不起作用。它只是没有设置它。 【参考方案1】:

向您的 ModalViewController 添加一个协议/委托,并通过订阅该委托使用它与 MainViewController 进行通信。类似于以下内容。 (未经测试,但给你的想法)。当你像这样展示你的模态视图时,一定要设置你的委托。

Study this link 如果您仍有问题

ModalViewController *modal = (ModalViewController *)[[ModalViewController alloc]init];
modal.delegate = self;
[self presentModalViewController: modal animated:YES];

ModalViewController.h

@protocol ModalViewControllerDelegate <NSObject>
-(void)setEventID:(id)event_ID;
@end

@property (nonatomic, weak) id<ModalViewControllerDelegate> delegate;

ModalViewController.m

[_delegate setEventID:event_ID];

MainViewController.h

#import "ModalViewController.h"

MainViewController @interface ModalViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, ModalViewControllerDelegate>

MainViewController.m

-(void)setEventID:(id)event_id

    [self setEventID:event_id];

【讨论】:

我现在试试这个并告诉你。谢谢。 我认为这是正确的方法,但是由于我是第一次自己使用协议,所以我无法完成它。我仍然是无效的。我相信这是因为我没有正确设置代表。我已经在同一个文件中有其他代表。我编辑了我的问题,也许你可以在那里给我一些提示。将不胜感激。 检查我的编辑。另外,不要忘记像我的示例中那样添加委托。 MainViewController @interface ModalViewController : UITableViewController 如果我输入 MainViewController @interface ModalViewController : 等等,它会抛出 2 个错误。一种说法是“不能与以前的类型名称声明说明符结合”,另一种说法是“类 ModalViewController 的接口定义重复”。所以这对我来说真的不起作用。 您刚刚输入的内容和我输入的内容是两件不同的事情。请用您的确切代码更新您的问题。【参考方案2】:

试试这个:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
 
    NSMutableString *event_id = [[self.events valueForKey:@"event_id"] objectAtIndex:indexPath.row];
    NSLog(@"EVENT ID = %@", event_id); //This gives me actuall ID

    [self.mainViewController setEventID:event_id];
    [self.presentingViewController dismissViewControllerAnimated:YES completion:^

    ];

【讨论】:

我在将代码放入完成块之前尝试过,但它没有设置它。无论哪种方式都行不通。

以上是关于将值从presentingViewController 传回presentingViewController的主要内容,如果未能解决你的问题,请参考以下文章

将值从 ngOnInit 传递到模板

将值从 php 传递到 modal

通过 Laravel 中的 AJAX 发布请求将值从一个视图传递到另一个视图

将值从 uitableview 传递到 uicollectionview

如何将值从javascript变量传递给剃刀变量?

如何将值从angularjs传递给节点[重复]