0

I have a UINavigationController in which there is a MainViewController with UITableView and a ToolBar button. Selecting a UITableViewCell will push to SecondViewController and tapping Toolbar button will push to ThirdViewController, both with StoryBoard segue. Both ViewControllers are pushed OK for the first time, but when UINavigationController pushes the SecondViewController first and pop back to MainViewController, then pushing ThirdViewController will cause the app to crash. So I thought something has to be wrong with ThirdViewController's code, but the error message is:

SecondViewController respondsToSelector:]: message sent to deallocated instance 0x115621d0

The weird thing is why SecondViewController is in the error message while I'm pushing the ThirdViewController? I put breakpoint and prepareForSegue on MainViewController is getting called without error, viewDidLoad on ThirdViewController is getting called after that without error, but when I click continue at the end of viewDidLoad the app crashes. Using Instruments with Zombies enabled shows this: Instruments

I'm pushing ThirdViewController, why the code goes into SecontViewController segue? I couldn't get any idea what is wrong with the code? I'm using ARC, so I didn't released anything by mistake.

Screenshot of SB:

Class definition of all three View Controllers

// MainViewController.h
@interface MainViewController : UITableViewController <UIAlertViewDelegate, UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate, UIPageViewControllerDelegate, UIPageViewControllerDataSource, SecondViewControllerDelegate> {
    BOOL _thumbnailTapped;

}

- (void)showQuickTour:(id)sender;
- (void)Purchased;
- (IBAction) pickThumbnailImage:(id)sender;


@property (strong, nonatomic) UIPageViewController *pageViewController;
@property (strong, nonatomic) NSArray *pageTitles;
@property (strong, nonatomic) NSArray *pageImages;
@property (strong, nonatomic) NSArray *pageImages_3_5;


@property (nonatomic, weak)   IBOutlet UIBarButtonItem *editOptionsLabel;
@property (nonatomic, strong) NSCache* thumbnailCache;
@property (nonatomic, strong) NSCache* emailCountCache;
@property (nonatomic, weak)   NSIndexPath* currentIndexPath;
@property (nonatomic, readwrite) BOOL deleting;
@property (nonatomic, weak)   ABContact *returnedMailingList;
@property (nonatomic, strong) SoundEffect* deleteFX;

@end


//SecondViewController.h
@protocol SecondViewControllerDelegate <NSObject>
- (void)getBackCurrentMailingList:(id)controller didFinishEnteringItem:(ABContact *)currentMailingList;
@end


@protocol ModalViewDelegate
@optional
- (void) getBackGroup:(NSDictionary *) group;
- (void) getBackContacts:(NSArray *)c andEmails:(NSArray *)e;
- (void) getBackPastedContacts:(NSArray *)contacts;
- (void) getBackPullView:(BOOL)pullled;
@end

@interface SecondViewController : UITableViewController <ModalViewDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate, UIAlertViewDelegate, MFMailComposeViewControllerDelegate>
{
    ABContact *currentML;
    BOOL _isViewPulled;
}

- (IBAction) pickImage:(id)sender;
- (IBAction) composeEmail:(id)sender;
- (IBAction) PasteGroup:(id)sender;
- (IBAction) dismissPopUp:(id)sender;

@property (nonatomic, weak)     IBOutlet UIBarButtonItem *composeButton;
@property (nonatomic, strong)   NSCache*                thumbnailCache;
@property (nonatomic, strong)   ABContact*              currentML;
@property (nonatomic, strong)   NSMutableDictionary*    currentMailingList;
@property (nonatomic, strong)   NSArray*                pastedContatcs;
@property (nonatomic, strong)   NSDictionary*           groupDictionary;
@property (nonatomic, readwrite) ABRecordID             currentRecordID;
@property (nonatomic, strong)   UIView*                 fadingView;
@property (nonatomic, strong)   UILabel*                fadingLabel;
@property (nonatomic, strong)   UIActivityIndicatorView* fadingActivityIndicator;
@property (nonatomic, weak)     id <SecondViewControllerDelegate> delegate;
@property (nonatomic, strong)   SoundEffect* deleteFX;

@end


//ThirdViewController.h
@interface SettingsViewController : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>
{

}

@property (strong, nonatomic) SKProductsRequest *request;
@property (strong, nonatomic) SKProduct *product;
@property (strong, nonatomic) NSString *productID;
@property (weak,   nonatomic) IBOutlet UILabel *versionLabel;
@property (weak,   nonatomic) IBOutlet UILabel *productLabel;
@property (weak,   nonatomic) IBOutlet UITextView *productDescription;
@property (weak,   nonatomic) IBOutlet UIButton *purchaseButton;
@property (weak,   nonatomic) IBOutlet UIButton *buyButton;
@property (weak,   nonatomic) IBOutlet UISwitch *soundFXSwitch;
@property (weak,   nonatomic) IBOutlet UIActivityIndicatorView *loadingIndicator;

- (IBAction)buyProduct:(id)sender;
- (IBAction)restorePurchase:(id)sender;
- (IBAction)switchSoundFX:(id)sender;
- (IBAction)showQuickTour:(id)sender;

-(void)getProductID:(UIViewController *)viewController;

@end
Hadi Sharghi
  • 903
  • 16
  • 33
  • Can you show an image of your SB? – Khaled Barazi Jun 15 '14 at 15:26
  • Please don't post screen shots of code. Just copy and paste the code directly and use the `[]` button to format it. – Fogmeister Jun 16 '14 at 07:22
  • could you post code for the class declarations as well? – simply_me Jun 16 '14 at 07:46
  • @Spectravideo328 if you meant StoryBoard it's added. – Hadi Sharghi Jun 16 '14 at 08:54
  • @simply_me I've pasted definition code of all three view controllers – Hadi Sharghi Jun 16 '14 at 08:55
  • the declaration seem fine. just out of curiousity have you used "respondsToSelector" anywhere in the code? – simply_me Jun 16 '14 at 09:07
  • @simply_me No, but I've used `NSNotificatioinCenter addObserver: selector: name: object:` 3 times in the code of MainViewController for Entering foreground, showing quick tour at the first launch and showing App Store rating at 5th launch. – Hadi Sharghi Jun 16 '14 at 09:53
  • 1
    are you assigning secondVC as UINavigationControllerDelegate? if yes, try setting that value to nil in dealloc – simply_me Jun 16 '14 at 10:17
  • 1
    you can also try this http://stackoverflow.com/a/11171042/2365064 – simply_me Jun 16 '14 at 10:19
  • @simply_me Set `self.navigationController.delegate` to `nil` in `viewWillDisappear:` method when SecondViewController is Popped (not pushed) to its parent View Controller (MainViewController). Solved the problem. Thanks Man, you've saved my life! :) – Hadi Sharghi Jun 16 '14 at 10:29
  • I had set `delegate` value to nil in `dealloc` method before asking this question, But the point was when `dealloc` method is called, the `SecondViewController` has been removed from the hierarchy and setting `delegate` to `nil` was not going to solve the problem. But adding the code to `viewWillDisappear:` solved it! – Hadi Sharghi Jun 16 '14 at 10:36
  • @simply_me There is no answer in this thread! only comments! I up voted both of your comments. If you write an answer I'll mark it as correct answer. – Hadi Sharghi Jun 16 '14 at 10:38

1 Answers1

1

Do check if you assigning secondVC as UINavigationControllerDelegate? if yes, try setting that value to nil in viewWillDisappear

simply_me
  • 384
  • 1
  • 11