Question

So I'm creating a Settings view (SettingsViewController) for my app, and the view contains 5 switches. I'm looking to accomplish the following:

if switch is on, filter TableView to only display items that contain 'Arthritis'. if switch is off, display all items.

Note: the TableView is located on another view (ViewController).

Now even though I've imported ViewController.h onto my SettingsViewController.m file, it's telling me that StrainTableView is unidentified. Any idea as to why? See code below (you can ignore the PickerView references).

SettingsViewController.h

@interface SettingsViewController : UIViewController {


IBOutlet UISwitch *ArthritisSwitch;
IBOutlet UIView *CancerSwitch;
IBOutlet UISwitch *HIVSwitch;
IBOutlet UISwitch *InsomSwitch;
IBOutlet UISwitch *MigSwitch;
IBOutlet UILabel *mylabel;

       NSArray *arthritisResults;
     NSArray *Strains;
}


-(IBAction)switchtheswitch:(id)sender;

@property (nonatomic, retain) NSArray *arthritisResults;


@end

SettingsViewController.m

#import "SettingsViewController.h"
#import "ViewController.h"



@interface SettingsViewController ()

@end

@implementation SettingsViewController
@synthesize arthritisResults;


-(IBAction)switchtheswitch:(id)sender; {

    if (ArthritisSwitch.on) {


        NSPredicate *ailmentPredicate = [NSPredicate predicateWithFormat:@"title ==[c] 'Arthritis'"];

        arthritisResults = [Strains filteredArrayUsingPredicate:ailmentPredicate];


        // Pass any objects to the view controller here, like...

        [StrainTableView setSearchResults: [arthritisResults copy]];

        NSLog(@"%@", arthritisResults);
    }

    else {

          [Strains count];


    }
}

ViewController.h

#import "PickerViewController.h"


@interface ViewController : UIViewController <PickerViewControllerDelegate, UITableViewDataSource,UITableViewDelegate>
{

    NSArray *searchResults;
    // NSArray *Strains;
    NSMutableData *data;
    NSMutableArray *dataArray;
    NSArray *Strains;


}


@property (nonatomic, strong) NSMutableArray * favoritesArray;
@property (nonatomic, retain) NSArray *searchResults;
@property (strong, nonatomic) IBOutlet UITableView *StrainTableView;


@end
Was it helpful?

Solution

Just importing a class does not allow you to use a property from that class. You need to get an instance of the ViewController class (let's call it vc for example), and then use it like so:

[vc.StrainTableView setSearchResults: [arthritisResults copy]];

How you make that instance of ViewController depends on the structure of your app. You probably don't just want to alloc init one, but get a reference to one that you already have.

BTW, your code would be easier to read and understand if you conform to the naming convention of using lowercase letters to start properties and methods (and capitals for classes).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top