Pregunta

I have a storyboard with a navigation controller and a uiviewcontroller as my root, after this i have several uitableviewcontrollers connected by a push seague on cell click.

What i need, is to show an UIAlertView or some progress dialog (like MBProgressHUD) while the present controller is dismissed and the new one is showed.

I have tried set an UIAlertView on the click cell:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"I am dismissing"
                                       message:nil
                                      delegate:nil
                             cancelButtonTitle:nil
                             otherButtonTitles:nil];
    [alert show];
}

But the alert is showed until the next controller appears, how can i show the alert when the click is executed on the cell?

¿Fue útil?

Solución

The solution i've found:

Preparations:

  1. Make your controller conform to protocol UIAlertViewDelegate.

    Example:
    @interface YourViewController : UITableViewController
    to
    @interface YourViewController : UITableViewController <UIAlertViewDelegate>.

  2. In storyboard, set the segue identifier. Let me call it CellSegue for this example.
    (To to so, click the segue in the storyboard, go to Attributes Inspector and there is Identifier field)

  3. You will need 2 properties.
    @property (strong, nonatomic) UITableView *selectedCellTableView;
    @property (strong, nonatomic) NSIndexPath *selectedCellIndexPath;


Coding:

The method below, displays alertview and prevents the cell from being selected by returning nil. We want to remember which cell was to become selected, so we set properties prepared before as follows:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:nil message:@"alert" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];

    _selectedCellTableView = tableView;
    _selectedCellIndexPath = indexPath;

    [av show];

    return nil;
}

Lastly, in UIAlertViewDelegate method we handle a button click:

  1. We get the cell at _selectedCellIndexPath from _selectedCellTableView,
  2. We perform the CellSegue segue and set the cell as a sender.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

    //1
    UITableViewCell *cell =[_selectedCellTableView cellForRowAtIndexPath:_selectedCellIndexPath];

    //2
    [self performSegueWithIdentifier:@"CellSegue" sender:cell];

}

I hope it was what you were looking for :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top