Nicole SD
Nicole SD

Reputation: 121

Back Button to tableView

I have a karaoke app . On the first screen are all the songs. There are menu button for categories. If "Classic Music" button is selected the content of the first screen is changed.

In the moment when I select a cell, a new view is presented with a back button .

My problem is : when i press back button , in the first screen are all songs , even if before there was only classic music , and i don't want this.

This is the method for back action:

- (IBAction)back:(id)sender {
    ViewController *xyz=[[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"first"];
    [self presentViewController:xyz animated:YES completion:^(void)
     {

     }];
}

And this is the implementation for first view :

-(void)viewDidLoad {

    categoryArray =[[NSMutableArray alloc]init];
    categoryArray = [allKeys valueForKey:@"category" ];



    feeds=[[NSMutableArray alloc]init];
    feeds=[allKeys valueForKey:@"feed_api"];

   if ([stories count] == 0) {
        for (int i=0; i<[feeds count]; i++) {
            NSString * path =[feeds objectAtIndex:i];
            [self parseXMLFileAtURL:path];

        }
       .............
    }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (tableView.tag==0) {

    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"];
    }
    // Set up the cell
    int storyIndex = [indexPath indexAtPosition: [indexPath length] - 1];

    cell.title.text=[[stories objectAtIndex: indexPath.row] objectForKey: @"title"];
   NSString * aux=[[stories objectAtIndex: indexPath.row] objectForKey: @"url"];
    NSString *   videoHTML =[NSString stringWithFormat: @"<iframe type=\"text/html\" width=\"100\" height=\"80\" src=\"%@\" frameborder=\"0\"></iframe>",aux] ;
    [cell.WEB loadHTMLString:videoHTML baseURL:nil];

    cell.title.numberOfLines=6;
    return cell;}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (!tableView.tag==0) {
        int row =indexPath.row;
        if (row==0) {
            self.title1.text=@"All Songs";
            [stories removeAllObjects];
            [self parse];
        }else
        {
        self.title1.text=[categoryArray objectAtIndex:indexPath.row-1];
        [stories removeAllObjects];
        NSString * path =[feeds objectAtIndex:indexPath.row-1];
        [self parseXMLFileAtURL:path];}
        i=0;
        [self showMenu:nil];//sender should be settingsButton;
    }

- (void)parseXMLFileAtURL:(NSString *)URL
{
    //stories = [[NSMutableArray alloc] init];

    //you must then convert the path to a proper NSURL or it won't work
    NSURL *xmlURL = [NSURL URLWithString:URL];

    // here, for some reason you have to use NSClassFromString when trying to alloc NSXMLParser, otherwise you will get an object not found error
    // this may be necessary only for the toolchain
    rssParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];

    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [rssParser setDelegate:self];

    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [rssParser setShouldProcessNamespaces:NO];
    [rssParser setShouldReportNamespacePrefixes:NO];
    [rssParser setShouldResolveExternalEntities:NO];

    [rssParser parse];

}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    NSLog(@"all done!");
    NSLog(@"stories array has %d items", [stories count]);
    if (self.tableView.tag==0) {
        [self.tableView reloadData];

    }
}

Upvotes: 0

Views: 766

Answers (1)

JonLOo
JonLOo

Reputation: 4939

Yo are making a new view controller every time you press back, you should embed your mainViewController in a navigation controller. This way you can go forward pushing view controllers and backward popping them.

Upvotes: 1

Related Questions