xcode 像后退按钮一样自定义 segue push
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11789106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom segue push like with back button
提问by Jad Gift
I'm trying to build my custom segue, similar in behavior to standard Push segue, but in my case I want to use custom buttons to switch between screens (no tab bars will fit to this app, any modal segue is no good either). The main screen is a custom graphics and the screen to segue to is a map. User to choose the point on the map, hit done and come back to the main screen with it's coordinates. I'm trying to connect ViewControllers with custom segue.
我正在尝试构建我的自定义 segue,其行为类似于标准 Push segue,但在我的情况下,我想使用自定义按钮在屏幕之间切换(没有标签栏适合这个应用程序,任何模式 segue 也不好) . 主屏幕是自定义图形,要转入的屏幕是地图。用户选择地图上的点,点击完成并返回主屏幕及其坐标。我正在尝试将 ViewControllers 与自定义 segue 连接起来。
I research a lot of solution across the Internet and found one, which can fit, but the code is scatter all around ViewControllers and AppDelegate (I'm not certain if Apple will accept it). I'm trying put whole code to a CustomSegue.m /h. The success is partial because using technique (below) it segues to the second ViewControler but I can't figure out how to dismiss the second and come back to main screen. I tried various techniques but when I run it the compiler terminates app with bunch of errors. Neither errors nor warnings before building and running. I also tried to dismiss it with a button using [self dismissModalViewControllerAnimated:YES]; or [self dismissViewControllerAnimated:YES completion:nil]; they work when I try modal segues but with my code with bad result.
我在互联网上研究了很多解决方案,找到了一个,它可以适合,但是代码分散在 ViewControllers 和 AppDelegate 周围(我不确定 Apple 是否会接受它)。我正在尝试将整个代码放入 CustomSegue.m /h。成功是部分的,因为使用技术(下面)它会转到第二个 ViewControler,但我不知道如何关闭第二个并返回主屏幕。我尝试了各种技术,但是当我运行它时,编译器会因一堆错误而终止应用程序。在构建和运行之前既没有错误也没有警告。我还尝试使用 [self deniedModalViewControllerAnimated:YES] 用按钮关闭它;或 [自我解雇ViewControllerAnimated:YES 完成:nil]; 当我尝试模态转场但我的代码结果不好时,它们会起作用。
Does anyone have a suggestion on a neat way of doing this. I'm stack on this problem for the last couple of days. I see that kind of segues transitions in a lot apps so it seems to be quite popular, easy solution. I'm not much experienced in Xcode only couple of mo. This is my custom segue (with QuartzCore animation).
有没有人有关于这样做的巧妙方法的建议。在过去的几天里,我一直在解决这个问题。我在很多应用程序中看到这种转场转换,所以它似乎很受欢迎,简单的解决方案。我在 Xcode 中的经验并不多,只有几个 mo。这是我的自定义转场(使用 QuartzCore 动画)。
#import "CustomTabBarSegue.h"
@implementation CustomTabBarSegue
-(void)perform {
UIViewController *src = (UIViewController*)[self sourceViewController];
UIViewController *dst = (UIViewController*)[self destinationViewController];
UIView *currentView = src.view;
UIView *theWindow = [currentView superview];
UIView *newView = dst.view;
[theWindow addSubview:newView];
CATransition *transition = [CATransition animation];
transition.delegate = self;
transition.duration = 1;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionMoveIn;
transition.subtype = kCATransitionFromRight;
[[theWindow layer] addAnimation:transition forKey:@"MySegue"];
}
@end
Below is code for my button back from destinationVC. (doesn't work)
下面是我的按钮从 destinationVC 返回的代码。(不起作用)
- (IBAction)dismiss:(id)sender
{
{
[self dismissViewControllerAnimated:YES completion:^() {
[self performSegueWithIdentifier:@"MySegue" sender:self];
}];
}
}
回答by Zachary Christopoulos
I think I MAY understand what you are trying to do. Correct me if I'm wrong though. You want to go to one view then hit a back button to come back. Correct? To do this with story boards is easy. Put a NavigationController as your initial view and set the root view controller for the navigation controller to the main menu. From there you want to do a PUSH segue and to go back you want to do a POP.
我想我可以理解你想要做什么。如果我错了,请纠正我。您想转到一个视图,然后单击后退按钮返回。正确的?用故事板做到这一点很容易。将 NavigationController 作为初始视图,并将导航控制器的根视图控制器设置为主菜单。从那里你想要做一个 PUSH 转场,然后你想要做一个 POP。
Calling the push segue is the normal way to call a segue, but the pop is a little different. The code to pop back one view is: [self.navigationController popViewControllerAnimated:YES];
. The code to pop back to the root view controller is: [self.navigationController popToRootViewControllerAnimated:YES];
.
调用 push segue 是调用 segue 的正常方式,但 pop 有点不同。以退流行一种观点认为的代码是:[self.navigationController popViewControllerAnimated:YES];
。到弹出回到根视图控制器的代码是:[self.navigationController popToRootViewControllerAnimated:YES];
。
I believe this is what you wanted to do. No need for custom segues.
我相信这就是你想做的。无需自定义转场。