Error when presenting ViewController - ios

I have a problem with presenting a ViewController. The Presented ViewController contains:
UIImageView
UIScrollView which contains a UICollectionView.
In the
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
I pass a value from the first to the second ViewController like this:
SecondViewController *secondVC = segue.destinationViewController;
secondVC.value = passValue;
When I launch the App and present the SecondViewController, everything works fine. If I go back to the first ViewController and then present the second ViewController again I get the following error message:
*** -[SecondViewController isKindOfClass:]: message sent to deallocated instance 0x170224e0
Of course the App breaks.
What could that mean and how could I solve this.
I already got much trouble with this problem and thank you for help.
Jannes

Related

UINavigationController does not release memory after back button is tapped

I have a mainViewController and a secondViewController. The SecondViewController is pushed using segue (iOS 7 app).
The problem is that when the back button is tapped and the app is back to mainViewController, the secondViewController memory is not released.
In prepareforSegue, I don't do any allocation. I merely set the delegate for the SecondViewController (aka cvc below). The CollectionViewController (aka SecondViewController) is quite big so as the user goes back-and-forth between the main VC and the SecondViewController memory usage keeps increasing and the app crashes eventually.
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"Show Gallery"]) {
CollectionViewController *cvc = (CollectionViewController *)segue.destinationViewController;
cvc.delegate = self;
}
}
Also, the delegate is a weak reference:
#property (weak, nonatomic) id <collectionCellSelectedDelegate> delegate;
It's not enough information to localize the issue.
Are you sure CollectionViewController object is leaking? I mean it's possible that the issue is caused by non-released sub-object used in CollectionViewController code. You can try Profiler Xcode Instrument to clarify it.
Also the issue may be caused by custom UIStoryboardSegue subclass (of course if you are using it).
Anyway, it would be great the take a look at storyboard and CollectionViewController code

unrecognized selector sent to instance error when a button is clicked

I'm not entirely sure why I am getting this error. It occurs when I click the button on my FirstViewController:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[FirstViewController 1ButtonPress:]: unrecognized selector sent to instance 0x10954bbd0'
My FirstViewController has a button (1ButtonPress) which performs a segue with another ViewController which has a embed NavigationController (so in my storyboard, the segue is from FirstViewController to the NavigationController). On this segue, some data is transferred to the other ViewController by:
First.m
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"abc"]) {
UINavigationController *navController = [segue destinationViewController];
SecondViewController *BVC = (SecondViewController *)(navController.topViewController);
BVC.BLLImg = [UIImage imageNamed:#"SomeImage.jpg"];
BVC.BLLTitle = #"SomeText";
}
}
The segue has the identifier "abc" in storyboard. Looking around it, I believe it has something to do with an incorrect class, but everything on first inspection looks fine. Any other reason why I would be getting this error?
Let's go through the error you saw. It says that an unrecognized selector was sent to an instance of FirstViewController.
From what I deduce, you hooked up your button to your view controller, and removed the implementation from your view controller later, assuming that it'd remove the connection you earlier created from your button to your view controller. Unfortunately, that's not how it works. You need to remove the connection from the button too.
To do that, right click on your button in the storyboard, and remove the connection which says 1ButtonPress:.
Side note: You're doing too much for a beginner to understand. Please try going through the Cocoa world step by step. Also, read up this article to learn about how to name your methods and variables in a more easy-to-understand way.
Right-click the 1ButtonPress button in your Storyboard. Remove the action to 1ButtonPress:.

pass data to view controller using storyboard segue iOS

I know there are lots of postings about this but I have tried everything and nothing has worked. So I have tried to pass an object between two view controllers, to a DBKIngredientsViewController embedded in a navigation item. I have a push segue with the identifier "showIngredientsSegue" to the DBKIngredientsViewController. The error message I receive is:
'NSInvalidArgumentException', reason: '-[DBKIngredientsViewController topViewController]: unrecognized selector sent to instance 0x8a92450'
The view controller to which I am segueing is embedded in a navigation controller, which I think is messing it up. What's the way around this? To be clear, the DBKViewController is already embedded in a navigation controller, and the push segue pushes the DBKViewController, not the navigation controller embedding it. I have tried it different ways but none seem to work.
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"showIngredientsSegue"]){
UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)navController.topViewController;
controller.targetRecipe = selectedRecipe;
}
}
Are you sure that segue.destinationViewController is a UINavigationController? It seems like it's just a DBKIngredientsViewController so this should work:
DBKIngredientsViewController *controller = (DBKIngredientsViewController *)segue.destinationViewController
Also if DBKViewController already has a navigation controller then you do not need a second one if you are pushing DBKIngredientsViewController. You would only need a second one if you are modally displaying DBKIngredientsViewController.

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;
}
}

iPhone app crashes when passing data in prepareForSegue:sender

I've been trying to pass data from one tableViewController to another, but as soon as I assign a value to a property in my second tableViewController the app crashes, giving me the message "...unrecognized selector sent to instance 0x715c340...".
Here is the code I have used:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"displayWordlist"]){
DisplayWordlist *dwl = (DisplayWordlist *)[segue destinationViewController];
dwl.apa = #"hej";
}
}
As soon as the program reaches 'dwl.apa = #"hej";' it crashes. I have both made a property of apa and synthesized it.
Make sure you have set the class of this view controller to DisplayWordlist in the Storyboard. If you don't, dwl will be assumed to be an instance of UIViewController despite the cast.

Resources