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?
The solution i've found:
Make your controller conform to protocol UIAlertViewDelegate
.
Example:
@interface YourViewController : UITableViewController
to
@interface YourViewController : UITableViewController <UIAlertViewDelegate>
.
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)
You will need 2 properties.
@property (strong, nonatomic) UITableView *selectedCellTableView;
@property (strong, nonatomic) NSIndexPath *selectedCellIndexPath;
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:
_selectedCellIndexPath
from _selectedCellTableView
,- (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 :)