[英]How to go back to previous view controller?
I have an UIViewController A with push segue to UIViewController B, in some case i need to repeat this for example: 我有一个将UIViewController B推送到UIViewController B的UIViewController A,在某些情况下,我需要重复此操作,例如:
ViewController A -> ViewController B -> ViewController A -> ViewController B
Also i need to pass data from ViewController B -> ViewController A
我也需要从
ViewController B -> ViewController A
传递数据
I don't want to create 4 ViewControllers, so how can I recycle code in this case? 我不想创建4个ViewController,所以在这种情况下如何回收代码?
You simple push and pop the same view controllers. 您只需推入并弹出相同的视图控制器即可。
If you want B to communicate with A then setup a protocol at A that communitactes with the B. (or who ever implements the protocol) 如果您希望B与A进行通信,则在A处设置一个与B通信的协议(或实施该协议的人)
B code B码
@protocol MyProtocol <NSObject>
-(void)callFromB:(id)data;
@end
@property (weak) id <MyProtocol> delegate;
implementation: 执行:
if ([self.delegate respondsToSelector:@selector(callFromB:)]){
[self.delegate callFromB:data];
}
A code 一个代码
B.delegate = self;
-(void)callFromB:(id)data {
...
}
You should not recycle them. 您不应该回收它们。 By that I mean you should not recycle the concrete instances of those view controllers.
我的意思是,您不应回收那些视图控制器的具体实例。 Certainly there might her some technical implementation allowing that but it is (contrary to table view cells) not necessary.
当然,她可能会允许一些技术实现,但这不是必需的(与表视图单元相反)。
You are trying to optimise prematurely. 您正在尝试过早优化。 Just create new view controller instances as you need.
只需根据需要创建新的视图控制器实例。 Any iPhone supporting iOS6 and later will handle that without any problem.
任何支持iOS6及更高版本的iPhone都可以毫无问题地处理此问题。
Passing the data can be done by a convenient method for example 例如,可以通过一种方便的方法来传递数据
-(void)configureWithData:(WhateverDataType *)paramData
In prepare for segue, you would obtain a reference to the target view controller and pass the data like this: 在准备segue时,您将获得对目标视图控制器的引用,并像这样传递数据:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
CustomViewController *nextViewController = [segue destinationViewController];
[nextViewController configureWithData:someData];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.