Creating an app to search and show results

Discussion in 'iOS Programming' started by Alhazred, Jul 5, 2011.

  1. Alhazred macrumors member

    Jul 5, 2011
    I have to create an app which should work like this:
    - as it starts it shows a view with a text field and a button
    - user writes something into the text field and taps the button
    - the app sends the string to a php script using a POST request
    - the php script returns a JSON formatted data to the app
    - the app changes the view and show a table with the results received

    I can do almost all of this, my only problem are the views.
    Which kind of project do I have to use? Navigation or Windows based?
    If I use a navigation-based project it shows the table as 1st view, isn't it?
    I need to show the text field and the button instead.
    If I'm going wrong, how do I show at first the text field and button view and then switch to the table as I receive the results?
  2. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    This is only thing i didn't like about Xcode (and that it crashed randomly..)
    I created my own Template. to create a navigation based without table view at start.
    Because, u can perfectly start a navigation based app. delete the tableview, add a view, do some IB connections, and do some .h files changes.
    Because this is what you need, if u want, i can explain what to do here -->

    1. Start clean navigation based app.
    2. Go to xib, delete the tableview, add a clean VIEW. control click on files owner, and drag to the view (or right click on files owner..) Then connect the VIEW property to your freshly view. and start editing it.
    3. Your app will crash if you will build that, so this is what you do. You go to your .H file of your RootViewController, and where it says

    Change it to

    @interface RootViewController : UIViewController {
    If you still want a tableview, but still change the view around a bit, then do all above. but add a Tableview to the VIEW u created.
    and add this code.

    @interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
    And in IB, you have to right click the tableview, and drag to files owner, and give it the delegate/datasource method :)

    So now your good to go.
    On your RootViewController you can set a search field with a button, and a small loading screen/popup, w/e. Then if that's done, sending the scripts etc. Then push a second screen, with a tableview, or whatever. And show it in there (the code you need to push is in the didSelectRowMethod in your RootViewController, but instead put it on the button.. kktnxbb :)

    Regards, Noxx
  3. Alhazred thread starter macrumors member

    Jul 5, 2011
    While waiting for a reply I went on working on my Window-based project named RecuperoInfo.
    I think to be close to the solution, here what I have now:
    inside RecuperoInfoViewController.m
    - (IBAction)performSearch:(id)sender {
        NSMutableURLRequest *richiesta = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@""]
                                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:120.0];
        [richiesta setHTTPMethod:@"POST"];
        [richiesta setHTTPBody:[[NSString stringWithFormat:@"search=%@", textInput.text] dataUsingEncoding:NSASCIIStringEncoding]];
        NSURLResponse *response;
        NSError *error;
        NSData *dati = [NSURLConnection sendSynchronousRequest:richiesta returningResponse:&response error:&error];
        //JSON formatted string received from the php script
        NSString *esitoRicerca = [[[NSString alloc] initWithData:dati encoding:NSASCIIStringEncoding] autorelease];
        TableResultViewController * myTable = [[TableResultViewController alloc] init];
        //JSONValue parses the JSON string into an array of dictionaries
        myTable.response = [esitoRicerca JSONValue];
        [self.navigationController pushViewController:myTable animated:YES];
        [myTable release];
    inside TableResultViewController.m
    @synthesize results;
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.title = @"Recupero Info";
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        return 1;
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
        return results.count;
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.text = [[results objectAtIndex:indexPath.row] valueForKey:@"testo"];
        NSString *path = [NSString stringWithFormat:@"",[[results objectAtIndex:indexPath.row] valueForKey:@"img"]]; //path all'immagine...
        cell.imageView.image = [UIImage imageWithContentsOfFile:path];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        return cell;
    Into TableResultsViewControler.xib I've linked dataSource, delegate and view to File's owner.

    The problem is that the view don't change to the table, do I miss anything?
    Shouldn't the last lines inside the performSearch method call the new view?
    Do I have to add anything to the RecuperoInfoAppDelegate.m and .h files?
  4. Alhazred thread starter macrumors member

    Jul 5, 2011
    I've found that the navigationController results to be nil, how do I set it?
  5. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    I explained it in the above tutorial.. how to do it with the standard navigationController setup.
    If u want to have a window setup, u need to add one completely manual.
    U need to add a NAVIGATION contorller in your mainwindow, instead of a view controller. and combine all that.
  6. Alhazred thread starter macrumors member

    Jul 5, 2011
    Thanks, I've soled by adding the navigation controller in the MainWindow.xib.

Share This Page