简体   繁体   English

如何返回上一个视图控制器?

[英]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.

相关问题 如何使后退按钮始终转到上一个视图控制器 - How to have a back button always go to the previous view controller 如何返回到先前在同一视图控制器上随机生成的数据 - How to go back to previous data randomly generated on same view controller 如何回到不同故事板的前一个视图控制器? - How to go back to previous view controller of different storyboard? 向后滑动(回到左侧)返回上一个视图控制器 - Go back to previous view controller with help of swipe back (to the left) 如何关闭选项卡栏控制器并单击按钮返回上一个视图控制器 - How to dismiss a Tab Bar Controller and go back to previous view controller on click of button 转到上一个视图控制器 - go to previous view controller 如何返回到IOS中的上一个视图? - How to go back to previous view in IOS? 返回上一个视图控制器不起作用 - Go back to previous view controller doesn't work 在我转到视图控制器的子节点并返回到上一个视图控制器后,SWRevealViewController将无法工作 - SWRevealViewController won't work after I go to the child of view controller and go back to the previous view controller 保存后如何返回上一个控制器并刷新该控制器 - How to go back to previous controller and refreshing that controller after saving
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM