performSegueWithIdentifier destinationViewController deallocs with ARC - ios

Odd issue here:
I created a storyboard with two view controllers, connected with a custom segue(a cross disolve). I make the segue happen by:
-(void)transitionToIntro
{
[self performSegueWithIdentifier:#"ToIntro" sender:self];
}
This transitions and I see the new view controller, After the initWithCoder and AwakeFromNib it sets up the UI and I see that the properties for data source and delegates are set. THen all of a sudden dealloc is fired! and this destination view controller is dealloc'd. Then of course everything is bunked. Delegates are nil and Datasources are nil because the containing viewcontroller is invalid to the subviews that contained it...
I dont know why this is happening, is the source viewcontroller not retaining a strong hold on the destination viewcontroller?
I have made sure to make the initial view controller set in the story board to my source view controller.
Any help is appreciated

Solution:
Make sure to have your source VC have a retained strong reference to the destination VC , and set that in prepareForSegue. If not you will see your destination VC dealloc itself after running initWithCoder and AwakeFromNib.
#property (nonatomic, strong) IntroViewController *destVC;
-(void)transitionToIntro
{
[self performSegueWithIdentifier:#"ToIntro" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"ToIntro"])
{
IntroViewController *dest = [segue destinationViewController];
self.destVC = dest;
}
}

Related

ViewController's private data set to nil after returning from segue

I have two views. The main view is ViewController and the next is AddItemViewController. ViewController has a tableview that lists items that you add when you go to AddItemViewController. There is a button on AddItemViewController that segues back to ViewController. The problem is, upon returning to ViewController expecting that an item be added, the private data of ViewController is suddenly set to nil. I have lost data and any chance to interact with my objects after returning from the segue.
Here is the data that's getting set to nil
#property (strong, nonatomic) costEstimator *myCost;
#property NSString *testString;
What am I doing wrong?
Here is my prepareforsegue code in the AddItemViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
ViewController *vC = [segue destinationViewController];
[vC addSomething:_selectedItem withQuantity:[_quantBox.text doubleValue]];
}
Any help is greatly appreciated. Thanks :)
You want to pop the AddItemViewController in this case. When you segued from ViewController to AddItemViewController, I am guessing you did a push. What this does effectively is it adds AddItemViewController ontop of ViewController in the memory stack. By 'segue-ing' again from AddItemViewController to ViewController, you are adding ANOTHER ViewController instance ONTOP of AddItemViewController. This is why you think you are losing your data when in actuality, you aren't. You are only seeing the wrong view controller.

Implementing delegation between views embedded in two separate container controllers

The relevant part of my storyboard appears as follows:
You can see the custom "Container Controller" view houses two Container Views, one which links to a Navigation Controller via embedded segue, and another that links to a custom "Master View Controller" (which implements a Table View Controller) via embedded segue. The Navigation Controller component further has a relationship with a custom "Location Filter Controller."
I need to implement delegation such that when one of the UISteppers in the Location Filter Controller is incr./decr., the table view in the Master View Controller knows to update the data it displays accordingly.
I am not unaccustomed to working with protocols/delegates, but this unique situation of talking between views housed in segues is really tricking me! For the most part I have had success following the example here: Passing Data between View Controllers. In this case however, I am not able to directly link instantiated views as he indicates to do in 'Passing Data Back' step 6.
I had considered using a singleton object from which each of these views could get/set the necessary data, but the issue here is that the table view would not necessarily know when to update its contents, despite having data with which it could/should update.
Here is a code snippet from ContainerController.m where I setup the embedded segues to function:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
DataHold *data = [[DataHold alloc] init]; // <-- this actually is a singleton object
if([segue.identifier isEqualToString:#"locationEmbedSegue"])
{
}
else if([segue.identifier isEqualToString:#"tableEmbedSegue"])
{
[[segue destinationViewController] setDelegate:data.detailTableViewController];
// ^ This part actually sets up a delegate so that the table view (Master View Controller)
// delegates to the detail view controller of the overarching split view controller
// and tells it what to display when a row is pressed.
}
}
Thanks for any help!
I think you are on the right track setting the table view delegate to your Location Filter Controller.
I found that a simple way to work with embeded view controller is to add "placeholders" property for them, and set these property when the segue is "performed".
// MyContainerController.h
#property (strong, nonatomic) MyLocationFilterController *detailViewController;
#property (strong, nonatomic) UITableViewController *masterViewController;
// MyContainerController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"locationEmbedSegue"])
{
UINavigationViewController *dest = (UINavigationViewController *)segue.destinationViewController;
self.detailViewController = dest.topViewController;
}
else if([segue.identifier isEqualToString:#"tableEmbedSegue"])
{
self.masterViewController = (UITableViewController *)segue.destinationViewController;
[self.masterViewController.tableView setDelegate:self.detailViewController];
}
}
I came to this question recently and found there may be one problem with the answer above.
Move the setDelete: method out. This makes sure no controller is nil.
Then code becomes:
// MyContainerController.h
#property (strong, nonatomic) MyLocationFilterController *detailViewController;
#property (strong, nonatomic) UITableViewController *masterViewController;
// MyContainerController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"locationEmbedSegue"])
{
UINavigationViewController *dest = (UINavigationViewController *)segue.destinationViewController;
self.detailViewController = dest.topViewController;
} else if([segue.identifier isEqualToString:#"tableEmbedSegue"])
{
self.masterViewController = (UITableViewController *)segue.destinationViewController;
}
[self.masterViewController.tableView setDelegate:self.detailViewController];
}

Can't access parentviewcontroller's properties

Here's a quick and dirty question:
I have a "main" view controller (VC) which is opened up from a parent VC, that uses a navigation controller. Then I have a "sub" VC that is opened (modal segue) from the "main" VC.
I have set a property in the main VC's interface:
#property (nonatomic) int myVar;
Then set it from the button's action that is touched to display the "sub"VC from the "main"VC's interface:
self.myVar=1;
I imported the mainVC.h in the subVC.h
Then at the viewDidLoad method of the subVC, I'm trying to access myVar's value, but can't do that with:
NSLog(#"Myvar is %i", ((mainVC*)self.parentViewController).myVar);
Which returns the value as 0.
And when I try presentingViewController method instead, I get an error (which did not cause the error when I pushed the segue instead of making it a modal:
[MainVC myVar]: unrecognized selector sent to instance
I'm trying to code for iOS 5, and needless to say that I'm still a noob.
What am I missing?
Thanks!
The parentViewController of the "sub" view controller is not the mainVC, it's the navigation controller. The mainVC is not accessible - for all you know, it may be deallocated to save memory.
If you need to pass data from the main controller to the sub controller on the segue, add an instance variable to the "sub" view controller, and set it in the prepareForSegue method:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"open_sub"]) {
subVC *sub = segue.destinationViewController;
sub.myVar = 1;
}
}

segue: destination view controller weirdness

In my app, I use a storyboard and segues, and I often pass data to the destination view controller before doing the segue, as follows:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController respondsToSelector:#selector(setMyData:)]) {
[segue.destinationViewController performSelector:#selector(setMyData:)
withObject:myData];
}
}
It works everywhere except in one place. The selector gets called, the data gets set, but when the segue completes and the destination controller appears, it doesn't have the data I just set. After printing the view controller's id in both the source and destination view controllers, I found that the segue.destinationViewController in the code above is a DIFFERENT instance of the view controller than the one that gets displayed. What's going on here?
[UPDATE 1]
I looked into the lifecycle of the destination view controller, and it first gets loaded during the segue execution, but AFTER I set the property on it! This means, that when I call performSelector on it, the view controller object is not initialized! This is why the data I set doesn't stick. t don't understand why is this the case, and why this same approach works in the other parts of my app.
[UPDATE 2]
Posting the code of setMyData by request. At first I didn't have this method at all, because locationToOpen is a public property. I only added it to ensure it gets called and to print the debug info.
- (void)setMyData:(MyData *)myData
{
DLog(#"self: %#", (id)self);
_myData = myData;
}
I would do it as follows -
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"NextSegue"])
{
DestinationViewController *dest = [segue destinationViewController];
dest.myData = self.myData;
}
}
And in the DestinationViewController.h, you should create a property -
#import <UIKit/UIKit.h>
#interface DestinationViewController : UIViewController
#property (nonatomic, strong) NSObject *myData;
#end
And also, make sure to synthesize the myData property in DestinationViewController.m -
#interface DestinationViewController ()
#end
#implementation DestinationViewController
#synthesize myData = _myData;
// other methods here
#end
I had this same issue. It turned out for me that the target ViewController property I was setting in my prepareForSegue: code was declared as weak because I had copied and pasted the property from one that InterfaceBuilder auto-created, but mine was not a Storyboard object. So my property was being released and zeroed by ARC on exit from prepareForSegue:. Making it a non-weak property fixed it.
I had a similar problem where the ViewController I was doing the segue to changed at some odd point,
after a looking around a bit it seems that the segue created a new ViewController.
To solve the data passing problem I used notifications.
I know this thread is old and solution should be found. But since the final solution is not posted here, I would like to list one of the possible root cause (which is the one in my case). I had the same issue, trying to set the variable in the destination view controller of the segue. The root cause is that I forgot to instantiated ([[Class alloc]init]) the object variable. I need it in my case because I am setting the properties of the object instead of pointing to other object. Hope this help.

Storyboard - setting delegates

Before storyboards I was able to set delegates and datasources just by dragging an outlet to a class. With storyboards, I cannot drag the outlet to another view controller; there is no destination that will respond to it.
If I click on a view controller object, I am able to see the class owner at the bottom, but as soon as I select the other view controller containing the outlet, the old selection is gone, so I cannot connect the two.
Is this Apple's way of saying we should only connect them programmatically?
Correct. Set the delegate or other data in your prepareForSegue:sender: method. Here is an example:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check the segue identifier
if ([segue.identifier isEqualToString:#"showDetail"])
{
// Get a reference to your custom view controller
CustomViewController *customViewController = segue.destinationViewController;
// Set your custom view controller's delegate
customViewController.delegate = self;
}
}
If your storyboard segue destination View Controller is an UIViewController then #Marco answer is right. But if your destination View Controller is a UINavigationViewController then you have to get the UIViewController from UINavigationViewController :
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Check the segue identifier
if ([segue.identifier isEqualToString:#"chooseCategoryType"])
{
// Get a reference of your custom view controller if your segue connection is an UIViewController
// CustomViewController *customViewController = segue.destinationViewController;
// Get a reference of your custom view controller from navigation view controller if your segue connection is an UINavigationViewController
CustomViewController *customViewController = [[[segue destinationViewController] viewControllers] objectAtIndex:0];
// Set your custom view controller's delegate
customViewController.delegate = self;
}
}

Resources