UIActionSheet frustration

Discussion in 'iOS Programming' started by Locker, Jun 9, 2009.

  1. Locker macrumors 6502

    Aug 22, 2007
    Staffordshire, UK
    I have a table view based application which loads data for the next view from a URL. The method for doing this is as follows:

    -(void)downloadItem {
    	// Start network activity indicator.
    	app = [UIApplication sharedApplication];
    	app.networkActivityIndicatorVisible = YES;
    	// Bring up loading sheet.
    	UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Please wait..." delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    	sheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    	[sheet showInView:self.view];
    	// Download data.
    	itemDict = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:http://something.com]];
    	// Dismiss loading sheet.
    	[sheet dismissWithClickedButtonIndex:0 animated:YES];
    	// Remove network activity indicator.
    	app.networkActivityIndicatorVisible = NO;
    To me, the above code should show the UIActionSheet at the same time as the network activity indicator, and then they should both be removed when the file has been downloaded.

    Now, the network activity indicator does exactly what I'm after. It displays when a user taps on a cell, and once the data for the next view has been downloaded and displayed it disappears. The UIActionSheet, however, pops up after the network activity indicator has been removed and immediately disappears. In effect, it appears after the download has finished, and so instantly dismisses itself too.

    Any ideas on how to get this working as I would expect? Am I doing something wrong?
  2. johnnyjibbs macrumors 68030


    Sep 18, 2003
    London, UK
    You code assumes the action sheet blocks the main thread until user input is required. However, this is not what it does because it waits for the delegate message to be sent after a user clicks a button.

    Remember that the main loop executes everything in a cycle until it gets to the end and has to wait for a response to a user action. To some extent, it doesn't matter which order you put things in in any particular method therefore.

    However, you shouldn't actually be using an action sheet here.

    You should never use an action sheet in the way you are using it and your app will almost certainly be rejected if you keep it. Action sheets are supposed to offer a choice of options (requiring a user action), not to provide an alert. You don't even need an alert view. An activity indicator + "Please wait" label would suffice.

    To configure action sheets properly you need to implement the delegate methods (remembering to set delegate to self - i.e. the view controller object). However, in your example you could implement a delegate method that updates your user interface after the loading has completed. In that method you would remove the "please wait" messages.

Share This Page