0

i'm trying to pass data to a viewcontroller when the back button is pressed for that i'm using the viewDidDisappear method, which works fine. The problem is that even though i've created the object in the targeted viewController it cant find it?

property 'rentTimeArray' not found on object of type ViewControllerA

i'm trying to send a nsarray from viewcontrollerB to viewcontrollerA

viewcontrollerB.m

first i import the targeted header file

import "ViewControllerA.h"

viewDidDissapear method:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingFromParentViewController) {

        NSArray * values = [pickedArray allValues];

        ViewControllerA *newHome = [[ViewControllerA alloc] init];


        newHome.rentTimeArray = newHome;
}

ViewControllerA.m

@property (nonatomic, weak) NSArray *rentTimeArray;

ViewControllerA.h

@Synthenize rentTimeArray,
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

If I good understood your problem, you want to set value of rentTimeArray in your ViewControllerA when user tap backButton in ViewControllerB right? If yes, then your code has several problems. First of all:

yours rentTimeArray should be declared in ViewControllerA.h because it should be visible to other instances like yours viewcontrollerB class.

The second problem:

You cannot initialize other ViewControllerA in viewWillDisappear:animated method because it will create other instance than current existing ViewControllerA in navigation stack. If you want to achieve this in that way you should make something like this:

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];

    if (self.isMovingToParentViewController) {

        for (UIViewController *controller in self.navigationController.viewControllers) {
            if ([controller isKindOfClass:[ViewControllerA class]]) {
                ViewControllerA *viewControllerA = (ViewControllerA *)controller;
                controller.rentTimeArray = [pickedArray allValues];
                break;
            }
        }
    }
}

Nevertheless you should learn about delegate or block pattern to pass values between 2 viewControllers.

Neru
  • 679
  • 6
  • 19
  • would it be more effecient to use delegate or block patterns? –  Aug 10 '14 at 11:20
  • Efficient, much faster, more readable and also in "good programming style". If you learn delegate and/or block pattern once, you will fall in love to use them! – Neru Aug 10 '14 at 12:03
  • Okay i'm doing the delegate pattern now, i'll accept this answer since it worked. Thank you in advance –  Aug 10 '14 at 12:30
  • Use protocols in such cases! – rohan-patel Aug 10 '14 at 17:19