1 Replies - 12175 Views - Last Post: 05 July 2011 - 06:18 AM

#1 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 200
  • Joined: 25-July 07

Creating an app to search and show results

Posted 05 July 2011 - 03:37 AM

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Creating an app to search and show results

#2 Alhazred  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 200
  • Joined: 25-July 07

Re: Creating an app to search and show results

Posted 05 July 2011 - 06:18 AM

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:@"http://www.sito.it/iosphp/responder.php"]
                                                                          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:@"http://www.sito.it/iosphp/img/%@",[[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?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1