I have two ViewController and use a (tableview click) seque for opening the second ViewController. My Problem is, the Second View Controller load much Data. So the time between switch is <> 10 Seconds. In this 10 Seconds the App freeze. Thats OK, but HOW can i insert a "Popup" or "Alert" Message like "Please Wait..." BEVOR . I have testing much tutorials for Popups and Alerts, but the Popup/Alter shows only, when the SecondView Controller is complete loaded. I will show the Message BEVOR the SecondViewController is compled loaded.

Example:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

        // IF i set here the ALERT, the Alter was only show, when the Second View Controller is complete loaded!

        NSDictionary *rowVals = (NSDictionary *) [SearchNSMutableArray objectAtIndex:indexPath.row];

        [self performSegueWithIdentifier:@"Foo" sender:self];


    }

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {

        if ([[segue identifier] isEqualToString:@"Foo"]) {

            // Get indexpath from Tableview;
            NSIndexPath *indexPath = [self.SearchUITableView indexPathForSelectedRow];

            // Get Data from Array;
            NSDictionary *rowVals = (NSDictionary *) [self.SearchNSMutableArray objectAtIndex:indexPath.row];

            // Destination View;
            [MySecondViewController alloc];
            MySecondViewController *MyView = (MySecondViewController *)segue.destinationViewController;

        }
}
有帮助吗?

解决方案

You are trying to fix the problem with the wrong solution. That solution is just as bad because the popup will also freeze for 10 seconds. What if you add more data and it takes 30 seconds or 10 minutes? Are you going to expect your users to see a dialog they can't dismiss for 10 minutes?

Are you fetching the data from the internet? If so you need to fetch your data asynchronously in the background.

If you're loading it from disk then there's too much being loaded that could possibly be displayed on one screen, you need to load only a small portion of it, and if that still takes a long time you need to load it asynchronously.

  • UPDATED -

You should have a model class for your application that is responsible for fetching the data from the internet. Google Model View Controller to get some background information on what a Model is.

As soon as the app launches the model can start to download the data which needs to be down in the background (that's too big a topic to answer how to do that here).

The View controller can launch while the data is being downloaded and it can display a spinning activity indicator wheel or progress bar or dialog etc. while waiting. The important thing is the GUI will not freeze.

When the model has downloaded the data it needs to tell the view controller the data is now available, which it can do using NSNotification center.

There's lots for you to investigate and learn, to do it without GUI freezing it needs to be done properly, there's no shortcut, you have a lot to study.

其他提示

@Martin,

i found a solution:

// Send the Request;
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

So the request are asynchrony. Thanks for your answer. Great +1

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top