Question

I need to open a ViewController when I click in a UIPopoverController cell. SeccionesViewController is the ViewController that I put in the UIPopoverController, that is a table. The function that not works is didSelectRowAtIndexPath from TableView.

Thank you for advance.

My code to show UIPopoverController is:

-(IBAction)seccionesButtonTapped:(id)sender
{

        if (_itemPicker == nil) {
            //Create the ColorPickerViewController.
            _itemPicker = [[SeccionesViewController alloc] initWithStyle:UITableViewStylePlain];

            //Set this VC as the delegate.
            _itemPicker.delegate = self;
        }

        if (_itemPickerPopover == nil) {
            //The color picker popover is not showing. Show it.
            _itemPickerPopover = [[UIPopoverController alloc] initWithContentViewController:_itemPicker];
            //[_itemPickerPopover presentPopoverFromBarButtonItem:(UIBarButtonItem *) sender  permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
            [_itemPickerPopover presentPopoverFromRect:CGRectMake(350, 902, 300, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
        } else {
            //The color picker popover is showing. Hide it.
            [_itemPickerPopover dismissPopoverAnimated:YES];
        }
}

The SeccionesViewController code is:

-(id)initWithStyle:(UITableViewStyle)style
{
    if ([super initWithStyle:style] != nil) {

        //Initialize the array
        _itemNames = [NSMutableArray array];

        //Set up the array of colors.
        [_itemNames addObject:AMLocalizedString(@"listaTareas", @"")];
        [_itemNames addObject:AMLocalizedString(@"EventosTab", @"")];

        //Make row selections persist.
        self.clearsSelectionOnViewWillAppear = NO;

        //Calculate how tall the view should be by multiplying the individual row height
        //by the total number of rows.
        NSInteger rowsCount = [_itemNames count];
        NSInteger singleRowHeight = [self.tableView.delegate tableView:self.tableView heightForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        NSInteger totalRowsHeight = rowsCount * singleRowHeight;

        //Calculate how wide the view should be by finding how wide each string is expected to be
        CGFloat largestLabelWidth = 0;
        for (NSString *itemName in _itemNames) {
            //Checks size of text using the default font for UITableViewCell's textLabel. 
            CGSize labelSize = [itemName sizeWithFont:[UIFont boldSystemFontOfSize:20.0f]];
            if (labelSize.width > largestLabelWidth) {
                largestLabelWidth = labelSize.width;
            }
        }

        //Add a little padding to the width
        CGFloat popoverWidth = largestLabelWidth + 100;

        //Set the property to tell the popover container how big this view will be.
        self.contentSizeForViewInPopover = CGSizeMake(popoverWidth, totalRowsHeight);
    }

    return self;
}

#pragma mark - View Lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [_itemNames 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];
    }

    // Configure the cell...
    cell.textLabel.text = [_itemNames objectAtIndex:indexPath.row];

    return cell;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *selectedItem = [_itemNames objectAtIndex:indexPath.row];

    NSUserDefaults *dispositivo = [NSUserDefaults standardUserDefaults];
    NSString *dis = [dispositivo stringForKey:@"dispositivo"];

    if ([selectedItem isEqualToString:AMLocalizedString(@"listaTareas", @"")]) {

        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:dis bundle:nil];
        ListaTareasViewController *tareas= [storyBoard instantiateViewControllerWithIdentifier:@"ListaTareasView"];
        tareas.communitykey = communitykey;
        tareas.tituloAsig = tituloAsig;
        tareas.infoH = infoH;
        if ((vengoDe != 1) && (vengoDe != 2) && (vengoDe != 3)) {
            tareas.vengoDe = 1;
        }else{
            tareas.vengoDe = vengoDe;
        }
        tareas.infoAsig = infoAsig;
        [self.navigationController pushViewController:tareas animated:YES];

    }else if ([selectedItem isEqualToString:AMLocalizedString(@"EventosTab", @"")]){

        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:dis bundle:nil];
        ListaEventosViewController *eventos= [storyBoard instantiateViewControllerWithIdentifier:@"listaEventosView"];
        eventos.communitykey = communitykey;
        eventos.nomAsig = tituloAsig;
        eventos.infoH = infoH;
        if ((vengoDe != 1) && (vengoDe != 2) && (vengoDe != 3)) {
            eventos.vengoDe = 1;
        }else{
            eventos.vengoDe = vengoDe;
        }
        eventos.infoAsig = infoAsig;
        [self.navigationController pushViewController:eventos animated:YES];

    }


}
Was it helpful?

Solution

You have this code:

        //Set this VC as the delegate.
        _itemPicker.delegate = self;

which makes it look like you're on the right track. But then you reload the storyboard, instantiate a controller and push it into a navigation controller that doesn't exist (so, no crash, but no result either).

When you should be doing is using that delegate relationship to pass the selectedItem back to the source view controller. Once there, that view controller does have access to the navigation controller so it can decide how to use the selectedItem to create a controller and push that controller for display.

It is possible that you could pass tareas / eventos back to the delegate instead of selectedItem. Who should own the knowledge of which view controller is required and how it should be configured is something for you to decide based on your app structure.

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