0

I've read up on couple of questions here on SO about passing data between two View Controllers and seen different ways suggested of doing it. My problem is as follows:

I have a view controller (lets call it a "A") that has a searchbar in a toolbar. Nothing else. Then I have another view controller ("B") that is responsible for displaying a popover view when the user presses the search button on the keyboard when searching in the searchbar. This all works. The user enters text, presses the search button, the popover is shown. Great.

Now I have yet to display any search results in the popover tableview and am trying to pass a NSMutableArray to a function in B as an argument:

in B.h:

@property (nonatomic, retain) NSMutableArray *searchResults;

in B.m:

    @synthesize searchResults;

     -(void)setSearchResults:(NSMutableArray *)resultArray{
         [self.searchResults removeAllObjects];
         [self.searchResults addObjectsFromArray:resultArray];
     }

in A.h:

#import "B.h"
@property(nonatomic, retain) B *viewControllerObjectB;

in A.m:

@synthesize viewControllerObjectB;

 //The searchResultsArray is passed on from another function
-(void)communicateWithB:(NSMutableArray *)searchResultsArray{

    //I initialize the viewControllerObjectB in here

    NSMutableArray *temp = [[NSMutableArray alloc] initWithArray:searchResultsArray];
    [viewControllerObjectB setSearchResults:temp];
}

Now this all works except I do not get the content of temp passed on into the function in B.m. I'ts empty (nil). This is my problem.

I'm fairly new to iOS so all help would be appreciated.

EDIT: Forgot to mention that I'm using ARC.

3
  • i suggest you when you initalize the object of B view controller pass the result array that time. or you have to reload the tableview data after loading new results by calling [viewControllerObjectB.tableview reloadData] Commented Jun 29, 2012 at 12:14
  • Just to be clear about what is nil, what do you get from NSLog(@"Temp: %@", temp); in the communicateWithB method? Also, NSLog(@"Result: %@", resultArray); in setSearchResults? (And if "Temp" says it's nil, what about searchResultsArray?) Commented Jun 29, 2012 at 12:14
  • In searchResultsArray I get an array with 5 objects. In communicateWithB I get an empty array. I'm setting a brakepoint just below the addobjectsFromArray line and printing out the contents. Commented Jun 29, 2012 at 12:44

1 Answer 1

1

You didn't initialize your array (that's why it's nil). So in alloc method you should:

- (id)init {
    self = [super init];

     if (self) {
          self.searchResults = [NSMutableArray arrayWithCapacity:10];
     }
}

I also spotted 2 problems in your code:

@synthesize searchResults;

     -(void)setSearchResults:(NSMutableArray *)resultArray{
         //PROPERTY LOGIC IS RETAIN, SO YOU NEED TO RELEASE/RETAIN WHEN YOU OVERRIDE A SETTER
         if (searchResults != resultArray) {
              [searchResults release];
              searchResults = [resultArray retain];
         }
     }


-(void)communicateWithB:(NSMutableArray *)searchResultsArray{

    //I initialize the viewControllerObjectB in here
    //MEM. LEAK ON TEMP
    NSMutableArray *temp = [[[NSMutableArray alloc] initWithArray:searchResultsArray] autorelease];
    [viewControllerObjectB setSearchResults:temp];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, should have added that I'm using ARC so I do not need to designate autorelease nor manually release. searchResults is initialized in viewDidLoad so I don't think that is the problem. Thx though.
You have a commment //I initialize the viewControllerObjectB in here, so can it be that viewDidLoad was not called, so your self.searchResults is nil at this point? You should see if searchResults has any array initialized when putting a breakpoint in your setter
I'm a F-ing idiot. I was assigning the array incorrectly. Should have been doind searchResults = resultArray. Though this was not directly what you said, your answer set me straight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.