使用最简单的方法将数据(字符串)从子VC传递到父VC [英] Passing Data(String) from Child VC to Parent VC using easiest Way

查看:104
本文介绍了使用最简单的方法将数据(字符串)从子VC传递到父VC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用最简单的方法将数据(字符串)从子级View Controller传递到父级VC. 我尝试了几种方法,但迷路了,有人可以告诉我最好的方法.

Passing Data(String) from Child View Controller to Parent VC using easiest Way . i tried a Couple of ways , but got lost , can some one tell me the best way .

推荐答案

Srikanth是正确的.如果您有从视图控制器(我们的第一个"视图控制器)到另一个(我们的第二个"视图控制器)的序列,那么您要做的就是在第二个属性中创建一个指向第一个属性的属性.您将只有第一个视图控制器确保在执行转换之前设置该指针.完成此操作后,第二个控制器可以更新第一个控制器的属性或调用其方法.

Srikanth is correct. If you have a segue from a view controller (our "first" view controller) to another (our "second" view controller), all you need to do is to create a property in the second one that points to the first one. You will just have the first view controller make sure to set that pointer before it performs the transition. Having done that, the second controller can update properties or invoke methods of the first controller.

有关详细信息,请参见 View Controller编程指南中的rel ="nofollow">配置目标控制器(当触发Segue时).该属性将是prepareForSegue中指向第一个控制器的弱指针.然后,如Srikanth所说,第二个控制器可以使用该指针来更新第一个控制器中的属性.

For details, see Configuring the Destination Controller When a Segue is Triggered in the View Controller Programming Guide for information on how to set a property in the second view controller (in this case, that property will be a weak pointer to the first controller) in prepareForSegue. Then, as Srikanth says, the second controller can use that pointer to update properties in the first one.

因此,在您的第二个视图控制器中,有一个属性(请注意,应为weak),该属性指向第一个视图控制器:

So, in your second view controller, have a property (note carefully, it should be weak) that points back to the first view controller:

在SecondViewController.h中:

In SecondViewController.h:

@class FirstViewController;

@interface SecondViewController : UIViewController

@property (nonatomic, weak) FirstViewController *firstViewController;

@end

然后在FirstViewController.m中:

Then in the FirstViewController.m:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"YourSegueIdentifierHere"])
    {
        [segue.destinationViewController setFirstViewController:self];
    }
}

因此,例如,如果您的第一个视图控制器具有favoriteColor属性:

So, if your first view controller had, for example, a property of favoriteColor:

@interface FirstViewController : UIViewController

@property (nonatomic, strong) NSString *favoriteColor;

@end

然后,第二个视图控制器可以使用其firstViewController属性来更新此favoriteColor,如下所示:

Then, the second view controller could use its firstViewController property to update this favoriteColor, like so:

self.firstViewController.favoriteColor = @"Blue";

很明显:

  1. 用适当的类名替换FirstViewControllerSecondViewController

确保第二个视图控制器的.m文件与第一个视图控制器的.h做#import;和

Make sure that your second view controller's .m file does an #import of the first view controller's .h; and

确保已在Interface Builder中为从第一个控制器到第二个控制器的序列指定了序列标识符,并调整上面的prepareForSegue,用您的标识符替换YourSegueIdentifierHere.

Make sure you've specified a segue identifier in Interface Builder for your segue from the first controller to the second one and adjust the prepareForSegue above, replacing YourSegueIdentifierHere with your identifier.


在iOS 6中,您还可以通过展开搜索来完成此操作.您只需让第二个视图控制器的prepareForSegue更新展开窗口目标服务器(即第一个控制器)的属性即可.令人欣慰的是,放宽序列可以返回任意数量的级别,因此对于更复杂的场景而言,这非常好.不过,它仅是iOS 6或更高版本.


In iOS 6, you can also accomplish this via an unwind segue. You'd just have the prepareForSegue of the second view controller update the property of the unwind segue's destination controller (i.e. the first controller). What's nice is that unwind segues can go back an arbitrary number of levels, so for more complicated scenarios, it's very nice. It is iOS 6, or higher, only, though.

要进行展开动作,首先必须在第一个视图控制器中定义一个展开动作(通过IBAction返回类型和UIStoryboardSegue参数的组合来标识),例如:

To do unwind segue's, first you must define an unwind action in the first view controller (identified as such by the combination of the IBAction return type and the UIStoryboardSegue parameter), e.g.:

- (IBAction)done:(UIStoryboardSegue *)segue
{
    // do any clean up you want
}

然后,第二个(或第三个或...)视图控制器可以通过在界面生成器中进行 control 拖动(从按钮到场景底座中的退出图标)来创建展开序列.您可以让要从中展开的控制器执行逻辑prepareForSegue,以将信息传递回第一个视图控制器.

Then, the second (or third or ...) view controller can create an unwind segue by control-dragging in Interface Builder from a button to the exit icon in the scene's dock. You can have the controller from which you're unwinding do the logical prepareForSegue to pass information back to the first view controller.

顺便说一句,您使用了父母"和孩子"这两个词,但是我想明确地说,我假设您不是在谈论

By the way, you used the terms "parent" and "child", but I wanted to make it clear that I assumed that you were not talking about the more advanced topic of view controller containment, in which a view controller is invoking other view controllers to facilitate the presentation of a single screen of information (as opposed to transitioning between different scenes in an app). As rdelmar notes in our comments below, the terms "parent" and "child" controllers, strictly speaking, more properly imply that one is using view controller containment.

很明显,如果您使用的是视图控制器遏制功能,那么显然不适合讨论segues,prepareForSegue等.此外,正确实现的容器视图控制器可确保子控制器实际上可以使用parentViewControllerUIViewController属性,而无需定义我们自己的属性来引用父级.您需要做的就是将parentViewController强制转换/定义为正确的子类,然后您可以非常轻松地访问您的子类属性.

Obviously, if you are using view controller containment, then clearly the discussion of segues, prepareForSegue, etc., don't apply. Furthermore, a properly implemented container view controller guarantees that the child controllers can actually use the UIViewController property of parentViewController, without needing to define our own property to reference the parent. All you need to do is to cast/define parentViewController to be the right subclass, and then you can access your subclassed properties very easily.

这篇关于使用最简单的方法将数据(字符串)从子VC传递到父VC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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