Iphone - UIView not displayed - iphone

I have a strange problem with a UIView :
I want to show an Activity Indicator View that I created with Interface Builder to indicate long running activity.
In the viewDidLoad function of my principal viewController I init the ActivityIndicator View like this :
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
load = [[ActivityIndicatorViewController alloc] init];
...
When I push a button it call this IBAction :
- (IBAction)LaunchButtonPressed{
// Show the Activity indicator view.
[self.view addSubview:load.view];
// eavy work
[self StartWorking];
// Hide the loading view.
[load.view removeFromSuperview];
}
In the StartWorking function, I ask a request to an internet serveur and parse the XML file that it return me.
The problem is that if I Call my StartWorking function, the application do not start by showing the Activity Indicator view but with the StartWorking function.
Whereas if I remove the call to StartWorking function, the view is shown.
Is someone able to explain me why? :s

Have you tried to call the StartWorking method on a different thread?
Maybe its heavy process prevents other instructions to take place.
Look at the NSThread class, especially the detachNewThreadSelector:toTarget:withObject: method.
EDIT: About the pool problem, you need to create a pool in your StartWorking method, if it's called on a different thread:
- ( void )StartWorking
{
NSAutoreleasePool * pool = [ [ NSAutoreleasePool alloc ] init ];
/* Code here... */
[ pool release ];
}

Replace :
[self.view addSubview:load.view];
With :
[self performSelector:#selector(addLoadingSubview) afterDelay:0.1f];
And create the method :
-(void)addLoadingSubview{[self.view addSubview:load.view];}

Ok, I found a solution based on santoni answer :
- (IBAction)LaunchButtonPressed{
// Show the Activity indicator view.
[self performSelector:#selector(ShowActivityIndicatorView) withObject:nil afterDelay:0];
// eavy work
[self performSelector:#selector(StartWorking) withObject:nil afterDelay:2];
// Hide the loading view.
[load.view removeFromSuperview];
}
The Activity Indicator view is dislayed before the call to the eavy function.
Thank's for answering.

Related

How to release/deallocate/return-from a viewcontroller class that have been popped?

I have a UINavigationController in my application where I am pushing a view controller. On that viewcontroller, there is a preloader view that shows animation while the data is being downloaded and parsed in the background. Below the animation is a button that should redirect the user to some other view controller.
Now, I can accomplish this by popping the viewcontroller since the view I need to be directed to is the view the current view was pushed from. The problem here, however, is that the downloading, parsing, timer, etc.. everything keeps happening in the background.
The functionality I need is for the viewcontroller to completely stop working as soon as I pop the VC by clicking on the button.
Thanks in advance.
What are you using to run the background operations?
You can simplify background operations by subclassing NSOperation and using NSOperationQueue to run it. You just have to release NSOperationQueue and iOS will clean up for you automatically.
https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperation_class/Reference/Reference.html
https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html
Here is an example.
#interface MyBackgroundOperation : NSOperation
#end
#implementation MyBackgroundOperation
- (id)init
{
self = [super init];
if(self != nil)
{
// stuff that needs to be done at init
}
return self;
}
- (void)main
{
#try
{
// run my background operations
}
#catch (NSException *e)
{
// catch the exception
}
}
- (void)dealloc
{
// clean up
[super dealloc];
}
#end
Then you start the operation like this
NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
MyBackgroundOperation *myOperation = [[MyBackgroundOperation alloc] init];
[operationQueue addOperation:myOperation];
[myOperation release];
[operationQueue release];
The OS will retain the operation until it's finished so that releasing the view controller will not effect it. However, if you want to stop the operation after it's already started you're going to have to use a bit more fancy techniques, maybe notifications or KVO.

Activity Indicator is not showing with SVProgressHUD

I am trying to implement the SVProgressHUD progress activity indicator. I copied the class from the [demo].1
My app loads up but the activity indicator doesn't show up. This is my first time trying to use one of these, so any help would be appreciated.
Here is the code:
#import "SVProgressHUD.h"
#implementation QuotesAppDelegate
- (void)startLoading
{
//call this in your app delegate instead of setting window.rootViewController to your main view controller
//you can show a UIActivityIndiocatorView here or something if you like
[SVProgressHUD show];
[self performSelectorInBackground:#selector(loadInBackground) withObject:nil];
}
- (void)loadInBackground
{
//do your loading here
//this is in the background, so don't try to access any UI elements
[self populateFromDatabase];
[SVProgressHUD dismiss];
[self performSelectorOnMainThread:#selector(finishedLoading) withObject:nil waitUntilDone:NO];
}
- (void)finishedLoading
{
//back on the main thread now, it's safe to show your view controller
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[self copyDatabaseIfNeeded];
[self startLoading];
}
For anyone else having a similar problem, this can also happen because you have a long loop or a piece of code that takes a long time to execute. If this happens, your progress bar wont be shown until after the loop, which kind of defeats the purpose.
To solve this issue you need to you this:
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
Basically your code would look something like this:
- (IBAction)submitPost:(id)sender {
//now we show the loading bar and submit the comment
[SVProgressHUD showWithStatus:#"Submitting post" maskType:SVProgressHUDMaskTypeGradient];
SEL aSelector = #selector(submitDataOfPost);
[self performSelectorInBackground:aSelector withObject:sender];
}
This will basically load the progress bar, and in a background thread, the method you want to execute will be called. This makes sure that the UI is updated (shows the progress hud) at the same time that your code is executed.
First of all you were not adding your SVProgressHUD to the view.
If your class inherited from UIViewController then [self.view addSubview:]; or if your class is simple UIView then [self addSubView:];
I do not understand your requirement but as far as i can understand through your code that you are showing [SVProgressHUD show]; in your startLoading method and then you are calling loadInBackground method in that method where you are hiding your hud using [SVProgressHUD dismiss];
I will suggest you to trace it by using breakpoint and figure it out.
I had the same problem. When I changed a version of SVProgressHUD to the later one the problem disappeared. My current version supports ARC.
=>
(IBAction)fetchData:(id)sender
{
[SVProgressHUD showWithStatus:#"Loading..." maskType:SVProgressHUDMaskTypeGradient];
[self performSelectorOnMainThread:#selector(getDataFromSomeWhere) withObject:nil waitUntilDone:NO];
}
=>
(void)getDataFromSomeWhere
{
//Do your data populating here. and dismiss the ProgressHud.
[SVProgressHUD dismiss];
}

How can i refresh a view every time when a tab bar item is clicked?

I am making an application with TabBar. But TabBarController is not RootViewController. In the Tab Bar there are 4 tabs. One of those tabs is history which is linked with a table view which shows history. I want to refresh that view every time when i click that so that i can get updated tableview. How can i do that? Thanks in advance.
use - (void) viewWillAppear:(BOOL)animated to update any content in your view.
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// You code here to update the view.
}
This method will be called every time the view is about to be displayed.
below code added plz ^^
If you change a tabBarIndex, At the same time -(void)viewWillAppear called.
-(void)viewWillAppear:(BOOL)animated
{
// force the tableview to load
[tableView reloadData];
}
refer a Apple Sample Code: that is amazing great tutorial for you about UITabBarController
http://developer.apple.com/library/ios/#samplecode/TheElements/Introduction/Intro.html
Apple Sample Code is no added [super viewWillAppear:animated];
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
if ([Globals sharedInstance].isFromFindSimiler)
{
[self.view addSubview:_findSimilerView];
[_historyTableView setFrame:CGRectMake(0, 85, 320, 490)];
}
else
{
[self history_webservice];
}
}
I have achieved this. In this case, whenever user tabs on History screen I call web service for history and update tableview.
instead of using (void)ViewDidLoad use viewWillAppear:(BOOL)animated
- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
//your code
}

presentModalViewController in viewDidLoad on first launch

I've been searching around but unfortunately have had no luck.
My app requires the user to sign in/sign up the first time he or she launches the app. I know how to determine first launch (using NSUserDefaults) but whenever I try to present the modal containing the sign in/ sign up controls, nothing happens.
Here's what I have:
-(void)viewDidLoad {
[self showLogin];
[super viewDidLoad];
}
-(void)showLogin {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"AccountView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:controller animated:YES];
[controller release];
}
However, nothing happens. The main view just loads as normal. Any help is greatly appreciated.
-Giles
[UPDATE]
Fixed simply by using..
-(void)viewDidAppear:(BOOL)animated
{
}
instead of
-(void)viewDidLoad
{
}
Thanks anyway!
/idiocy
I had the same issue and ended up using viewDidAppear as well. The only problem with the viewDidAppear approach is that if you load other UIViewControllers on top, then reshow the base, then your setup code gets called over and over. I ended up having to add a boolean value (initialised to YES) to this view controller and check that value before deciding what to do. Hope this helps someone...
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:(BOOL)animated];
if(justLaunched)
{
justLaunched = NO;
if(settingsFileExists)
{
[self displayMainView];
}
else
{
[self displaySetupView];
}
}
}
How about using performSelector:withObject:afterDelay in the viewDidLoad function? That's how I do it, with a short delay of 0.1s.
And invoking this in the viewDidLoad isn't very safe : the sequence viewDidLoad / viewDidUnload can occur at runtime when the iPhone needs to release some views in order to get back some free memory.
The side effect of such sequence would be that your login controller would be shown...
As you said the viewDidAppear looks better but not simply put it at the end of the appDidFinishedLaunching the delegate of your UIApplication?

UITabBar appearance problem + NSThreads

I'm having a problem when trying to add a UITabBar as a subview of my AppDelegate's window.
The link above shows a screenshot of the messy state of the screen.
TabBarInAMessyState.png
The results are unpredictable. In this picture only the UITabBarItem's titles were affected, but sometimes the TabBar background is not shown (consequently we can see the window's background). Sometimes the NavigationBar is also affected (not show in this picture).
When I start the Application I first have to check if there's network connection, so It is called a method (verifyNetworkAvailability:) that will run in a thread different from the main thread. This is done in order not to freeze the application.
- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window makeKeyAndVisible];
// check if there's network connection in another thread
[NSThread detachNewThreadSelector: #selector(verifyNetworkAvailability:) toTarget:self withObject:self];
}
- (void) verifyNetworkAvailability:(MyAppDelegate*) appDelegate {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// Check if there's network connection..
// If so, call the verifyNetworkAvailabilityDidEnd method
[appDelegate verifyNetworkAvailabilityDidEnd];
[pool release];
}
- (void) verifyNetworkAvailabilityDidEnd {
[window addSubview:tabBarController.view];
}
I'd like to know if it is possible to add the tabBarController.view in this way (by a method call done in thread other than the main thread).
Thanks in advance
Try this
- (void) verifyNetworkAvailability:(MyAppDelegate*) appDelegate {
// other code here ...
[appDelegate performSelectorOnMainThread:#selector(verifyNetworkAvailabilityDidEnd) withObject:nil waitUntilDone:NO];
}
UIKit has some trouble when you try to access it from any thread but the main thread. Think about dispatching a notification to have your primary app thread to add the view rather than adding the view directly in your secondary thread.