xcode 使用导航后退按钮放松 segue
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13551778/
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
Unwind segue with Navigation back button
提问by Alan
I have watched the apple demo video on storyboard implementation with the unwind segues section and seen the demo using custom buttons on the main view. That works fine.
我已经观看了关于故事板实现的苹果演示视频,并在主视图上使用自定义按钮观看了演示。这很好用。
I have also seen a good example of the same thing here which works also. http://www.techotopia.com/index.php/Using_Xcode_Storyboarding_%28iOS_6%29#Unwinding_Storyboard_Segues
我也在这里看到了同样有效的一个很好的例子。http://www.techotopia.com/index.php/Using_Xcode_Storyboarding_%28iOS_6%29#Unwinding_Storyboard_Segues
However, I wish to have a normal push segue passing from parent (with main form data) to child (with advanced settings options) and then return to the parent view controller using the back button in the navigation bar rather than with a custom button on the view as shown in the demo. The parent controller will then save everything to an API server when a further save button on the parent controller is pressed.
但是,我希望有一个从父级(带有主表单数据)传递到子级(带有高级设置选项)的正常推送segue,然后使用导航栏中的后退按钮而不是使用自定义按钮返回到父视图控制器演示中显示的视图。当父控制器上的另一个保存按钮被按下时,父控制器会将所有内容保存到 API 服务器。
I dont seem to be able to override the navigation back button to point it down to my unwind segue action and since the back button doesnt appear on the storyboard, I cant drag the green Exit button to it
我似乎无法覆盖导航后退按钮以将其指向我的展开转场操作,并且由于故事板上没有出现后退按钮,我无法将绿色退出按钮拖到它上面
I tried this in viewDidLoad to overrride the action, but it didnt work [self.navigationItem.backBarButtonItem setAction:@selector(unwindSegueAction:)];
我在 viewDidLoad 中尝试了这个来覆盖操作,但它没有工作 [self.navigationItem.backBarButtonItem setAction:@selector(unwindSegueAction:)];
Is it possible to unwind a push segue with the back button?
是否可以使用后退按钮解除推动转场?
The best idea I had so far was to override the back button from the viewDidLoad method, but that removes the angled back button style and also seems like a rough hack to a simple problem
到目前为止,我最好的想法是覆盖 viewDidLoad 方法中的后退按钮,但这消除了有角度的后退按钮样式,而且似乎是对一个简单问题的粗略解决
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:self action:@selector(unwindSegue:)];
I know there are answers for using protocol and delegates, but I am interested in using the Xcode4.5 unwind segue method
我知道有使用协议和委托的答案,但我对使用 Xcode4.5 unwind segue 方法感兴趣
采纳答案by Alan
In the end I didn't need to unwind the segue since I could still get a reference to the parent controller methods by following the navigation controller.
最后,我不需要展开 segue,因为我仍然可以通过跟随导航控制器获得对父控制器方法的引用。
I was able to get a reference by doing the following in the - (void)viewWillDisappear:(BOOL)animated
method of the child controller
我能够通过- (void)viewWillDisappear:(BOOL)animated
在子控制器的方法中执行以下操作来获得参考
NSInteger currentVCIndex = [self.navigationController.viewControllers indexOfObject:self.navigationController.topViewController];
FirstViewController *parent = (FirstViewController *)[self.navigationController.viewControllers objectAtIndex:currentVCIndex];
parent.barcodeType = indexPath.row;
which passed the settings variable back to the original controller perfectly.
它将设置变量完美地传递回原始控制器。
I also added an import reference to the parent controller at the top of the childcontroller
我还在子控制器顶部添加了对父控制器的导入引用
回答by Chuck H
@Alan is on the right track. Just add a test to prevent the segue from firing when not going back to the first view controller. Something like this in viewWillDisappear will actually allow you to perform the unwind segue for a back button. You will need to create a manual unwind segue by dragging from the class down to the exit icon. Be sure to name it after you create it.
@Alan 走在正确的轨道上。只需添加一个测试以防止在不返回第一个视图控制器时触发 segue。viewWillDisappear 中的类似内容实际上允许您为后退按钮执行 unwind segue。您需要通过从类向下拖动到退出图标来创建手动展开转场。请务必在创建后为其命名。
UIViewController *vc = self.navigationController.topViewController;
if ([FirstViewController class] == [vc class])
{
[self performSegueWithIdentifier:@"unwindSegue" sender:self];
}
回答by Claudio Morsella
I'd like to add a Swift solution that follows @Chuck H suggestion above:
我想添加一个遵循上面@Chuck H 建议的 Swift 解决方案:
let rootVC = self.navigationController!.topViewController
if rootVC.isKindOfClass(TheNameOfYourViewController) {
performSegueWithIdentifier("yourNamedSegueIdentifier", sender: self)
}
回答by catalandres
An alternative you might have not considered is to make the parent a delegate of the child and use delegate methods in the child to update the parent.
您可能没有考虑过的替代方法是让父级成为子级的委托,并在子级中使用委托方法来更新父级。
That way you would bypass the need to add to the navigation unwind, because the parent is already up to date the moment you hit back.
这样你就可以绕过添加到导航展开的需要,因为父级在你回击的那一刻已经是最新的。