2

Context: I have two VC, A and B. VC A contains several buttons and several labels. When pressing the button in VC A a segue will display VC B/C/ and so forth. Now, when finished with VC A/B/C so forth, the segue is being unwind so that VC A appears. For most of the VC B/C/D so forth, I am using the unwind method which I trigger through a button in that VC (ctrl + drag to "exit" icon). This works perfect, because upon returning to VC A, the following action is being called automatically:

- (IBAction)returned:(UIStoryboardSegue *)segue {
// Here I do some stuff 
}

Problem: Now, in one of the secondary VCs (e.g. D), things are a bit special. In this VC I generate some hundred buttons through a loop programmatically, then detect which button is being pressed and finally unwind back to VC A (without a specific button; any of the buttons will trigger the unwind). I know I can do this eg by using this

[self dismissViewControllerAnimated: YES completion: nil]

but this does not trigger the above action when returning to VC A, or by using this

[self performSegueWithIdentifier:@"UnwindSegueIdentifier" sender:self]

but this will generate a new instance of VC A, which I do not want (because labels in the instance of VC A already contains some information).

So, what I want is to be able to return to the same instance of VC A which generated the VC D, and also trigger the "returned" action listed above. Thus, I want to achieve the same effect as when using a button connected to the "exit" icon, but I want to do this programmatically "inside the code" when one the many buttons in VC D is pressed.

Any thoughts?

4

3 回答 3

6

[self performSegueWithIdentifier:@"UnwindSegueIdentifier" sender:self]应该可以正常工作。Unwind segues 不会创建其目的地的新实例,无论它们如何执行。它们的独特之处在于它是唯一不创建目标新实例的 segue。

于 2013-07-23T21:59:30.803 回答
1

您可以通过控制+从场景的视图控制器图标拖动到其退出图标以编程方式使用来创建 unwindsegue。在为这个 unwindseque 分配一个标识符之后,您可以通过编程方式调用它。

参考: https ://developer.apple.com/library/ios/technotes/tn2298/_index.html

于 2014-04-15T06:33:30.843 回答
0

Ulas has properly understood and answered the question. The original poster wanted to ensure that the unwind '(IBAction)myCustomName:(UIStoryboardSegue*)segue' selector was called upon unwind without having to 'manually' link a button to the unwind segue. To emphasize this point, assume that you programmatically create a button at runtime based on some condition. You do not have the ability to manually wire that button to the unwind segue. You need to do something like this within VC B...

UIBarButtonItem *dynamicButton = [[UIBarButtonItem alloc]
                       initWithTitle:@"Cool"
                       style:UIBarButtonItemStyleBordered
                       target:self
                       action:@selector(killView)];
self.navigationItem.leftBarButtonItem = dynamicButton;


- (void) killView
{
    [self performSegueWithIdentifier:@"TEST" sender:self];
} 

The 'TEST' segue was established from the VC B to Exit manually (i.e the view controller itself not a button). When the button is clicked, it calls the killView method and the segue is triggered via the identifier.

于 2014-05-17T20:54:50.377 回答