如何在IOS中将数据从一个视图传回另一个视图? [英] How to pass data back from one view to other View in IOS?

查看:30
本文介绍了如何在IOS中将数据从一个视图传回另一个视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以毫无问题地将数据从一个视图传递到另一个视图.但这是我面临的问题.

I am able to pass data from one view to other view without any problem. But here is my situation i am facing problem.

让我们说-> 将数据传递给其他视图

ViewA->ViewB This working perfectly fine 
ViewB->ViewC This working perfectly fine
ViewC->ViewB Here is the problem.

我尝试使用 push segue ,它转到 ViewB,但是当我按下后退按钮时,它转到 ViewC->back button->ViewB->back button->View A.从当我按下返回按钮时,ViewB 必须转到 ViewA.

I tried using push segue ,it goes to ViewB but when i press back button it goes to ViewC->back button->ViewB->back button->View A. From ViewB when i press back button it must goes to ViewA.

尝试使用modal segue,它转到ViewB,但后来我不能去任何地方.

Tried using modal segue, it goes to ViewB but then i can't go any where.

由于我是 iO 的新手,我真的不知道如何实现这一目标?

如何将数据从ViewC传回ViewB?

我认为你们可以帮助我.

I think you guys can help me.

编辑

在视图 A 中,我像这样调用视图 B

In View A i am calling ViewB like this

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"RemixScreen"])
{

    if([self.recipeFrom isEqualToString:@"ViewB"])
    {

        ViewB *reciepe = [segue destinationViewController];
        //somedata i will pass here

    }  
}
 }

在视图 B 中,我是这样调用的

In View B i am calling like this

-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
  {
if([segue.identifier isEqualToString:@"ViewA"])
{  
    ViewA *data = [segue destinationViewController];
    //some data  
}
}

感谢你们的帮助.

推荐答案

正确的做法是使用委托.您使用 prepareForSegue 中的属性向前传递数据,并使用委托将数据传回.

The correct way to do this is to use delegates. You use properties in prepareForSegue to pass data forward and delegates to pass data back.

它的工作方式是在 ViewC 中有一个委托属性,该属性由 ViewB 在 prepareForSegue 中设置.这样 ViewC 就可以通过您设置的协议与 ViewB 通信.

The way it works is to have a delegate property in ViewC that is set by ViewB in prepareForSegue. That way ViewC can communicate with ViewB via a protocol you set up.

添加代码来演示:

ViewControllerB 接口:

ViewControllerBInterface:

@protocol ViewBProtocol

- (void)setData:(NSData *)data;

@end

@interface ViewBController: UIViewController <ViewBProtocol>
...
@end

这里我们让 ViewBController 遵循我们的协议,ViewCController 将与之通信.

Here we make ViewBController follow our protocol that ViewCController will communicate to.

接下来是ViewCController接口:

Next up is ViewCController interface:

@interface ViewCController: UIViewController

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

...

@end

现在我们看看 ViewBController 的 prepareForSegue:

Now we look at ViewBController's prepareForSegue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    UIViewController* controller = [segue destinationViewController];
    if ([controller isKindOfClass:[ViewCController class]])
    {
        ViewCController* viewCController = (ViewCController *)controller;
        viewCController.delegate = self;
    }
}

如您所见,我们通过委托属性将 ViewC 控制器链接到 ViewB 控制器.现在我们在 ViewC 中做一些事情:

As you can see we link the ViewC controller to the ViewB one via the delegate property. Now we do something in ViewC:

- (void)sendData:(NSData *)data
{
    [self.delegate setData:data];
}

如果需要,您可以在 ViewCController 的 viewWillDisappear 方法中使用它.

And you can use that in your viewWillDisappear method of ViewCController if you wanted to.

这篇关于如何在IOS中将数据从一个视图传回另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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