iPhone app - Show UIActivityIndicator while an IBAction is performed - iphone

I want a button in my navbar which lets the user refresh the contents of a tableview. The tableview is already populated and once the user presses the refresh button, I want to show a UIActivityIndicator while I fetch the items to be displayed on the table.
I tried putting a [indicator startAnimating] before calling the method to get the new data of the table (where indicator is of type IBOutlet UIActivityIndicator* and mapped to a control in IB) but the indicator does not show up. Instead, the navbar refresh button is in the pressed position till control returns from this IBAction method.
How can I show the indicator while the method execution completes?
Thanks.

You need to get the new data for the table in the background. That could be an asynchronous network call or a thread depending on what your refresh code does but the UI kind of assumes that button presses are pretty much instantaneous.
You'll note that your whole UI locks up (not just the refresh button), so this is a good thing to do anyway albeit much harder to implement.

Related

iPhone - SVProgressHUD - Can not get to appear on UIView

I am using the SVProgressHUD progress indicator, and it works very well most of the time.
I am having some issues with it not appearing as expected in certain situations, however.
For example, I would like to show the indicator when I click a button on a view. This button launches a synch call to a web service. At the end of the svc call, the SVProgressHUD indicator window is removed.
What is happening is - the progress window pops up and disappears immediately after the svc call finishes.. even though my code to display the SVProgressHUD window is the first line in my 'buttonPressed' action.
I think there are some UIView concepts I am missing here... any insight?
Thanks
You shouldn't attempt to show user interface elements on a background thread. If you're in a background thread, access the main thread to update your interface.

Any way to prevent UIActionSheet from being dismissed when user clicks a button in it?

I added a UIProgressView to my UIActionSheet to indicate the download progress. So I do not want my UIActionSheet to be dismissed when user click the download button in it.
But is there a way to do that ?
Right now I implement didDismissWithButtonIndex delegate method to show UIActionSheet again after it was dismissed. But I was hoping there is a better way.
UIActionSheet is a modal view, which means that it blocks UI completely. Since blocking UI is considered a bad practice, preventing an action sheet from dismissing is not supported. Instead, you should make a completely separate progress indicator outside the action sheet.
#Yar, thanks for the answer, I will try your solution some other time.
But I am also satisfied with my current solution. UIActionSheet.h said
// Called when a button is clicked. **The view will be automatically dismissed after this call returns**
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;
So I guess hit any buttons will always dismiss action sheet. Then my solution is first hide progress bar; when user hits download button, show action sheet again but hide the download button and show progress bar.
The cancel button is always there in case user wants to cancel download even after download is started.

Tableviewcell controller uiviewimage slow

I have a uitableview cell that has an image like twitter app that I pull from the web via nsurl. I have it loading them in via the cells controller and of course when the user scrolls the app cOme to a crawl. How can only grab the images if/when scrolling stops if that is the best approach.
The other issue is I have that I call data refresh on the cell on viewdidload but that causes lag when the user comes out of the detail view back to the table. What method should I put my refresh call in. As I only need refresh if user pushes a refresh button or on initial tableview load.
Have a look at the http://developer.apple.com/library/ios/#samplecode/LazyTableImages/Introduction/Intro.html
The idea is to download images row per row and then display them when loaded in their rows without calling refresh ([tableView reloadData]) all the time - which causes "crawling".

Where should I "save" changes in my iPhone view to 'create new' of an object?

I have a view that creates a new core data managed object, and fills in all the required properties and also allows optional ones. I originally had a "Done" button on the top left, and when that was pressed, I validated the object then saved and removed the view.
Now I have an edit/done type setup on the top right, so sometimes there are two identical "Done" buttons on the top of the view. I want to switch the left side button so that it just has the normal "Back" button, then somehow validate and stop the view from being removed if it doesn't validate. I can't find any way to capture the method called by that back button and modify it, and viewWillDisappear doesn't work cause there's no way to abort the disappearing.
How can I make this work? I need to validate this, then save, then remove the view if validate and save worked only.
It sounds like your view is a perfect candidate to be pushed modally instead of through the navigation controller stack.
Push the view that creates your NSManagedObject modally:
[self presentModalViewController:yourViewController animated:YES]
Then continue to use your top right EDIT/DONE button for editing/validation as you currently are and when validation is successful simply save your object and dismiss the modal view controller from the parent view controller:
[[self parentViewController] dismissModalViewControllerAnimated:YES];
For more details check http://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html#//apple_ref/doc/uid/TP40007457-CH111-SW14
If you still want to use a button on the left hand side perhaps you can change the right button to say EDIT/CANCEL and add a DONE button on the left side that is only visible when you're not in EDIT mode. If appropriate you can point the DONE button to run through the same validation process before dismissing the modal view using the code above but it probably makes sense that the EDIT/CANCEL button takes care of it.
I hope this helps.
Rog
There is no documented way to intercept the standard back button of UINavigationController. If you want this functionality, your only option would be to customize leftBarButtonItem with a custom button.
When the user taps that button, you can first validate your object and then call popViewControllerAnimated:.
It's hard to mimic the look of the built-in back button, though.

Change UIActionSheet after doing it's job

I have to import some XML data into my app.
Now I open a UIActionSheet and add as a subview a UIActivityIndicatorView. Initially I show a progress wheel and a button with 'Cancel'.
When the XML has been imported, I want to hide the progress wheel (this it's easy) and change the button into a 'Done' button, all in the same UIActionSheet.
What I'm doing now is closing the UIActionSheet with the option dismissWithClickedButtonIndex, but I don't like this way because it's the same as pressing 'Cancel', and then I show an UIAlertView displaying "All data has been imported".
Is this possible ?
You shouldn't be doing that, when it loads correctly just dismiss the ActionSheet. On the other hand if an error occurs then display an alert.
Think about the user who will use the app multiple times a day, a Done message each time will be a waste of time.
UPDATE
As i understand your goal is to use the ActionSheet just as a popup (with Cancel ability), if so, just call dismissWithClickedButtonIndex:animated: when your XML loading is done.
If its successful then just call the dismiss method, if its unsuccessful then call the dismiss and popup an alert
This is a bit of a hack but should work. Note that there is a good chance that this will result in your app not being accepted into the app store as you're messing around with the action sheet in ways Apple didn't intend.
Initially display the action sheet with both the 'Done' and 'Cancel' buttons. Before displaying the sheet hide the 'Done' button using its hidden property. To see how to access the 'Done' button see this question.
Then when you're hiding the UIActivityIndicatorView, also change the hidden property of both the 'Cancel' and 'Done' buttons, so that the 'Done' button becomes visible. If the 'Done' button appears in the wrong position move it by modifying its centre property.