0

TL;DR: I have an object in a VC that needs initiate an unwind method from its VC. Anyone know how?


Details & Code:

I have a custom UIImageView called UIImageViewQuestion. When the viewController it is contained in (PreKnowledgeViewController) is loaded I run this standard creation code:

UIImageViewQuestion *imageView = [[UIImageViewQuestion alloc] initWithImage:questionsArray[[myVariables sharedGameData].currentQuestionInt]]; imageView.contentMode = UIViewContentModeScaleAspectFit; imageView.clipsToBounds = YES; imageView.frame = CGRectMake(0,0, 320, 504); [self.view addSubview:imageView]; imageView.userInteractionEnabled = true;

Standard. But, when that UIImageViewQuestion receives a tap, I perform some logic in there to determine the position of the tap and decide if the user has selected the proper position. After this is where I need to perform an unwindSegue from PreKnowledgeViewController.

Also, if I seem to have any glaring errors in my logic or a silly line of code, please let me know. I definitely don't fully know what I am doing.

Thanks for the help!

cfo
  • 17
  • 3
  • Just to be clear, question (could a silly one) hasn't been answered. Other tutorials generate the object performing the unwind in IB, I'm generating mine programmatically. – cfo May 03 '14 at 21:22

2 Answers2

0

First of all, I'd recommend renaming your image view to not use the UI prefix. In general naming classes that start with an existing framework prefix could cause conflicts and will definitely cause confusion if someone else needs to read your code.

Otherwise this question has been answered before.

How to perform Unwind segue programmatically?

What are Unwind segues for and how do you use them?

Community
  • 1
  • 1
Brandon Schlenker
  • 5,078
  • 1
  • 36
  • 58
  • good advice! Regarding the link, I had already looked at that one and a few others before posting on SO. The issue I noticed was the the segue did not work for a push only a modal. Further, the image that I am generating programmatically isn't something that I can link using the storyboard since it isn't created at that point. I'm using a navigation controller in my app and so I need to do an unwind from a push segue along with calling it from a programmatically generated object versus one created on the storyboard. Any ideas? – cfo May 03 '14 at 18:43
  • This answer is actually much better. You need to hook up the unwind segue in your storyboard to an IBAction method in your class. Then you can assign an identifier to that unwind segue in the storyboard. With the identifier you can call performSegueWithIdentifier programatically. http://stackoverflow.com/a/15839298/324464 – Brandon Schlenker May 03 '14 at 23:23
  • Thanks! About 2 hours ago, I was able to find another method that worked (since I was using a nav controller) but this is similar and would work too. Thanks so much! I didn't post because SO wouldn't let me within ~8 hours of my original post. – cfo May 04 '14 at 00:47
0

Found my solution. Passing the ViewController to a property when creating the object allows you to call something like below when needed.

In the View Controller

`UIImageViewQuestion *imageView = [[UIImageViewQuestion alloc] initWithImage: (... other init info)
imageView.preKnowledgeViewController = self;`

In the object created

[_preKnowledgeViewController.navigationController popViewControllerAnimated:true];

Definitely a pretty obvious answer but I was hoping for something a little more clean. This works for now.

Thanks everyone

cfo
  • 17
  • 3