如何将数据从一个视图传回到IOS中的其他视图? [英] How to pass data back from one view to other View in 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.
我尝试使用推送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.
尝试使用模态segue ,它会转到ViewB但是我不能去任何地方。
Tried using modal segue, it goes to ViewB but then i can't go any where.
由于我是iOs的新手,我真的不知道如何实现这个目标?
如何将数据传回来自ViewC的ViewB?
我想你们可以帮助我。
编辑
在视图中我正在调用ViewB这样
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
}
}
}
在View 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中使用ViewC在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.
编辑:添加代码以演示:
Adding code to demonstrate:
ViewControllerBInterface:
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屋!