Activate Activity Indicator On View Controller From NSObject class

583 views Asked by At

I am currently trying to activate an "Activity Indicator" on my view controller from a NSOBject class where I store global variables and global functions.

I put together this code below which is in on of the NSObject class functions. When I hit that function the Activity Indicator on my view does not animate. Please help.

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main"
                                              bundle:nil];
ScreenUIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ScreenUIViewController"];

[vc.activeDisplay startAnimating];

In my NSObject class I have the functions declared as below in the .h file

+ (GlobalTest*)sharedGlobalTest;

// global function
- (void) testFunction;

In my NSObject And in the .m file I have

-(void)testFunction
{
NSLog(@"testFunction RAN!!");

UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main"
                                              bundle:nil];
ScreenUIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"ScreenUIViewController"];

[vc.activeDisplay startAnimating];

}

I call it in a view controller by

[[GlobalTest sharedGlobalTest] testFunction];

Function does run, but the activity indicator does not aniamte

1

There are 1 answers

1
B-Rad On

A little late to the game but thought I'd offer a suggestion.

If you have embedded your view controller in a navigation controller, one approach you can try is identifying the top view controller on the stack and presenting the activity view controller from there.

For example:

// Get the current view controller
UIViewController *currentVc = [(UINavigationController *)[UIApplication sharedApplication].delegate.window.rootViewController topViewController];

// Alloc and init your activity view controller
NSArray *activityItems = @[Item1, Item2, Item3, Etc.];
UIActivityViewController *activityVc = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:nil];
activityVc.excludedActivityTypes = @[ExcludedItem1, ExcludedItem2, Etc.];

// Present the activity view controller from the current view controller
[currentVc presentViewController:activityVc animated:YES completion:nil];

Again, maybe a little late to help you but it might be useful to someone else.

Cheers!