如何将信息从推送到的View Controller传输回View Controller? [英] How do I transfer information back to a View Controller from a View Controller it pushed to?

查看:51
本文介绍了如何将信息从推送到的View Controller传输回View Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自己的第一个iOS应用程序,并且一直在努力遵循良好的MVC惯例,并制定了约定,使我的应用程序易于维护和重新开发.我正在使用一个Storyboard,在很大程度上,我将使用Push segues来浏览我的应用程序.这是我的情节提要的快照.

I'm developing my first iOS app and I've been trying to adhere for good MVC practices and laid out conventions to make me app easy to maintain and redevelop. I'm making use of a Storyboard and largely I will use Push segues to navigate through my app. Here is a snapshot of my Storyboard.

单击添加员工"时,推送选择将移动到选择员工".在选择职员"上,我选择了所需的职员,并且可以返回到上一个VC.我应该提到,每个VC都有自己的 .m .h ,它们是从UIViewController继承而来的.这是使用后的样子.

When Add Staff is clicked, a push segue moves to Select Staff. Once on Select Staff, I select the staff I want and can return to the previous VC. I should mention that each VC has it's own .m and .h which I subclassed from UIViewController. Here is what it looks like when used.

现在,在将所选数据行传输回第一个VC时,我遇到了麻烦.我的问题是,我不知道如何最好地将数据从被其推送的VC中传输回VC.在Select Staff VC中,我有一个数组( currentSelected )可以正确保存所选行的字符串,但是回滚后,我想在Create VC中访问该数组.

Now I've hit a brick wall in transferring the selected data rows back to the first VC. My problem is that I don't know how to best transfer data back to a VC from one which has been pushed by it. In the Select Staff VC, I have an array (currentSelected) which correctly holds the Strings of the selected rows, but I want to access this array in the Create VC once I have rolled back.

如何最好地做到这一点?

How do I best go about doing this?

P.S.值得注意的是,我认识到与此类似的问题,但是它们或者是针对稍有变化的问题,有些复杂或者是讨论XIB和其他内容的XCode的较旧版本.过去,我一直在研究有关该问题的文献,但未能取得有效进展,所以我希望这可以解释我提出这个问题的原因.

P.S. It's worth noting that I realise there are similar questions to this one, but they are either for slightly varying issues, slightly complex or folder older versions of XCode which talk about XIBs and stuff. For the past while I've been researching the literature on the issue and can't make effective headway, so I hope this explains why I made this question.

推荐答案

您可以使用块或委托.我个人建议使用一个块,因为您可以按逻辑对代码进行分组.使用块,您必须执行类似的操作.在您的SelectStaff.h文件中,在接口之前添加typedef:

You can use block or delegate. I personally recommended a block because you keep your code grouped logically. With block you have to do something like that. In your SelectStaff.h file add typedef before interface:

// I use NSString as an example but for your project more accurate would be NSArray
typedef void (^ReturnBlock)(NSString *arg);

在@interface之后但在@end之前添加属性:

After that in @interface but before @end add Property:

@property(copy) ReturnBlock returnBlock;

在拥有数据时在SelectStaff.m中(可能在save:方法中或关闭:)用您要传递的数据调用该块;

In SelectStaff.m when you have your data (maybe in your save: method or dismiss:) Call the block with the data you want to pass;

if (returnBlock)
   returnBlock(@"Data will be passed do previous view controller");

在您的CreateViewController.m中的prepareForSegue中:添加块:

In your CreateViewController.m in prepareForSegue: add block:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"YOUR_IDENT_FROM_STORYBOARD"])
    {
        SelectStaff *selStuff = (SelectStaff*)[segue destinationViewController];
        [selStuff setReturnBlock:^(NSString *arg){
            // This are your data 
            NSLog(@"Arg: %@", arg);
        }];
    }
}

希望获得帮助.

这篇关于如何将信息从推送到的View Controller传输回View Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆