2

I know how to implement UISearchController for a table view. But can anyone help me in implementing search bar for complete app.

In my app i have 3 view controllers a)students VC b)teachers VC c)parents VC

and I have a search button. Here i want to implement complete in app search functionality. If we type in a name it should appear in a table view either it is in students/parents/teachers. I am not getting any idea how to start this. Please give some KT.

dumb
  • 57
  • 6
  • 4
    you need an global array that contain all the data about students, teachers and parents then you can get the result as per your expectation. – Nitin Gohel May 30 '16 at 10:52
  • hi Nitin.. I have 3 different json/dictionary for parents/students/teachers.. how can i make one global array with 3 json files? – dumb May 30 '16 at 11:16
  • Check this create singlaton global class or NSObject, you can also create mutablearray in appdelegate and use it http://stackoverflow.com/questions/19995395/how-and-where-do-i-initialize-an-global-nsmutablearray-in-xcode-5 – Nitin Gohel May 30 '16 at 11:23
  • i just added an answer about global array hope you got the solution about. – Nitin Gohel May 30 '16 at 12:24

1 Answers1

0

I just try to create singleton class that store value and use as a global. So first you need to create a Singleton-class using following code:

#import <Foundation/Foundation.h>

@interface GlobalData : NSObject
{
    NSMutableArray *ClassArray;
}
+(GlobalData *) getInstance;
-(void)saveClassARray:(NSArray *)array;
-(NSMutableArray *)getGlobalArray;

@end

It's .M Class

#import "GlobalData.h"
@implementation GlobalData
static GlobalData *instanceGlobalData;

+(GlobalData *) getInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
       instanceGlobalData = [[GlobalData alloc] init];
        instanceGlobalData->ClassArray = [[NSMutableArray alloc]init];;
    });

    return instanceGlobalData;
}

-(void)saveClassARray:(NSArray *)array
{
    [ClassArray addObjectsFromArray:array];

}
-(NSMutableArray *)getGlobalArray
{
    return ClassArray;
}

@end

Now you need to store your Students, Teachers and Parents data by following code.

First you need to import #import "GlobalData.h" in your ViewController and create define variable that like following:

#import "GlobalData.h"
#define USERDATASINGLETON (GlobalData *)[GlobalData getInstance]

And in Web services Call back Success you need to store your data in globle array using following code:

NSArray *array =[[NSArray alloc]initWithObjects:@"Nitin",@"Nit", nil];
[USERDATASINGLETON saveClassARray:array];

Above array is just for your referance do same things for all your 3 ViewController and store the result using saveClassARray. now finally you need to get full array by following code:

NSMutableArray *FinalGlobalArray = [USERDATASINGLETON getGlobalArray];
NSLog(@"==%@",FinalGlobalArray);

That will be NSlog allof the data that have you stored. but make sure use some check that once you add teacher,student,parents data and if saveClassARray calling again that old data wont be overtire that will be create duplicate entry so take care about.

Now you have finalArray you can do your search code that you have did for individual.

Hope that help

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144