如何将数据从一个视图传回IOS中的另一个视图?

时间:2023-03-24
本文介绍了如何将数据从一个视图传回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->返回按钮->ViewB->返回按钮->查看 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?

我想你们可以帮助我.

编辑

在视图 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中的另一个视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:iOS 中流畅的视频循环播放 下一篇:iOS 6 UIGestures (Tap) 停止使用 QLPreviewController

相关文章