Colin
Colin

Reputation: 735

AlertView to Pause Code

This seems to be a white elephant for the iPhone. I've tried all sorts, even loops and I can't make it work.

I have a view that loads a table. However I've moved into object archiving and for development purposes I want to have an initial AlertView that asks if a user wants to use a saved database or download a fresh copy

(void)showDBAlert {

    UIActionSheet *alertDialogOpen;
    alertDialogOpen = [[UIActionSheet alloc] initWithTitle:@"DownloadDB?" 
                    delegate:self
                    cancelButtonTitle:@"Use Saved" 
                    destructiveButtonTitle:@"Download DB" 
                                         otherButtonTitles:nil];
    [alertDialogOpen showInView:self.view];
}

I'm using an ActionSheet in this instance. And I have implemented the protocol:

@interface ClubTableViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate>

Based on this I run this to check what button was pressed:

(void)actionSheet:(UIActionSheet *) actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {


    NSString *buttonTitle=[actionSheet buttonTitleAtIndex:buttonIndex];

    if ( [buttonTitle isEqualToString:@"Download DB"] ) {

        [self loadDataFromDB];

    }

    if ([buttonTitle isEqualToString:@"Use Saved"] ) {

        self.rows = [NSKeyedUnarchiver unarchiveObjectWithFile:[self archivePath]];

        if (self.rows == nil) {
            [self loadDataFromDB];
        }



    }
}

The problem is my code to build the table executes before the user has made there choice. This causes all sorts of havok. As a result, how can I pause the code until a user has made there choice?

Upvotes: 0

Views: 239

Answers (3)

lucas clemente
lucas clemente

Reputation: 6409

If you are using UITableView, you will not be able to "pause" the code, however this wouldn't be the method of choice anyways. A possible solution would be to load an empty table (return 0 in (UITableView *)tableView:numberOfRowsInSection) until the user has selected something, then reload the tableView's data with [tableView reloadData];

Upvotes: 2

Felix Lamouroux
Felix Lamouroux

Reputation: 7494

Add [self.tableView reloadData]; at the end of the actionSheet delegate method. This will trigger all UITableViewDataSource methods again.

Upvotes: 0

Firoze Lafeer
Firoze Lafeer

Reputation: 17143

In your tableView:numberOfRowsInSection: just return zero until the user has made a decision.

Upvotes: 0

Related Questions