Search code examples
iosxcodeuisplitviewcontrollerpass-by-reference

SplitViewController reference logic


I read allot about the SplitViewControllers but i am walking in circles because i dont understand something.

You have a masterviewcontroller and a popoverview as a bar button item (filter)

lets say masterviewcontroller is a tableview and in the popoverview is a uiview controller

On the iphone i always alloced the masterviewcontroller and update the reference after some modifications, when you hit the button "search", it pushed a new controller with new data (come to think of it,maybe this wasnt the best idea) now that logic doesnt work anymore.

I have read you have to reference the controllers to each other, so i did it like this.

in the filtercontroller (this is the popoverview)

.h

@property (strong, nonatomic) MasterViewController *masterviewController;
@property (weak, nonatomic) IBOutlet UISlider *filterPrice;

- (IBAction)filterSearch:(id)sender;


.m
- (IBAction)filterSearch:(id)sender {

self.masterviewController.filterSearchPrice = [NSNumber numberWithInt:self.filterPrice.value];
[self.masterviewController performFilterSearch];
}

the performFilterSearch checks the fields, makes a call to an url with the filternames and json objects come back,parse and reload data happens..

Now i expect the masterviewcontroller to show new data but that doesnt happen, in fact nothing happens...

Update this is FilterSearch:

-(void)performFilterSearch
{

[queue cancelAllOperations];
[[AFImageCache sharedImageCache] removeAllObjects];
[[NSURLCache sharedURLCache] removeAllCachedResponses];

isLoading =YES;
[self.tableView reloadData];

searchResults = [NSMutableArray arrayWithCapacity:10];

NSURL *url = [self urlFilterWithSearchPrice:filterSearchPrice];
NSLog(@"%@",url);

NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
                                     JSONRequestOperationWithRequest:request
                                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                         [self parseDictionary:JSON];


                                         isLoading = NO;
                                         [self.tableView reloadData];

                                     }
                                     failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                         [self showNetworkError];
                                         isLoading = NO;
                                         [self.tableView reloadData];
                                     }];

operation.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
[queue addOperation:operation];

}

btw when i Nslog in filterSearch to check if its updated:

NSLog(@"%d",self.masterviewController.filterSearchPrice);
NSLog(@"%d",[self.filterTypeSegmentedControl selectedSegmentIndex]);

the first one never gets updated the second one gets updated off course

Update 2: (how do i launch the popview):

I added a bar button item on the masterviewcontrollers navigation that has an action. I added a popover segue from the masterviewcontroller -> filtercontroller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

filterPopOver  = [(UIStoryboardPopoverSegue *)segue popoverController];

}

- (IBAction)filterPopButton:(id)sender {

if (filterPopOver){
    [filterPopOver dismissPopoverAnimated:YES];
}
else{
    [self performSegueWithIdentifier:@"showFilterPopover" sender:sender];
}
}

Solution

  • When you launch your filterController, you need to pass in a reference to the MasterViewController. You have a property for it in the filter controller, but you never assign a value to that property.

    After Edit:

    Your prepareForSegue method should look like this:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        FilterController *fc = (FilterController *)segue.destinationViewController;
        fc.masterViewController = self;
    }
    

    Make sure that you've imported MasterViewController.h into you FilterController.m