我能够毫无问题地将数据从一个 View 传递到另一个 View 。但这是我遇到的问题。
假设 -> 将数据传递给其他 View
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。
尝试使用 modal segue,它转到 ViewB,但我不能去任何地方。
作为 iO 的新手,我真的不知道如何实现这一点?
如何将数据从ViewC传回给ViewB?
我想你们可以帮助我。
编辑
在 View A 中我这样调用 ViewB
-(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 中我这样调用
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:@"ViewA"])
{
ViewA *data = [segue destinationViewController];
//some data
}
}
感谢您的帮助。
最佳答案
做到这一点的正确方法是使用委托(delegate)。您使用 prepareForSegue 中的属性将数据向前传递,并使用委托(delegate)将数据传回。
它的工作方式是在 ViewC 中有一个委托(delegate)属性,该属性由 ViewB 在 prepareForSegue 中设置。这样 ViewC 就可以通过您设置的协议(protocol)与 ViewB 进行通信。
编辑:添加代码来演示:
ViewControllerB接口(interface):
@protocol ViewBProtocol
- (void)setData:(NSData *)data;
@end
@interface ViewBController: UIViewController <ViewBProtocol>
...
@end
这里我们让 ViewBController 遵循我们的协议(protocol),ViewCController 将与之通信。
接下来是 ViewCController 接口(interface):
@interface ViewCController: UIViewController
@property (nonatomic, weak) id<ViewBProtocol> delegate;
...
@end
现在我们来看ViewBController的prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
UIViewController* controller = [segue destinationViewController];
if ([controller isKindOfClass:[ViewCController class]])
{
ViewCController* viewCController = (ViewCController *)controller;
viewCController.delegate = self;
}
}
如您所见,我们通过委托(delegate)属性将 ViewC Controller 链接到 ViewB Controller 。现在我们在 ViewC 中做一些事情:
- (void)sendData:(NSData *)data
{
[self.delegate setData:data];
}
如果需要,您可以在 ViewCController 的 viewWillDisappear 方法中使用它。
关于iphone - 如何将数据从一个 View 传回 IOS 中的另一个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11885673/