How to replace text in a round rectangular button using a counter?

I have a view with a common rounded right button that switches to another view. When users click on a rounded rectangle, it starts fetching. I want the spinning wheel to replace the text (β€œNext”) inside the rounded right button when processing the selection.

I created a counter:

UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [spinner startAnimating]; 

But I don’t know how to get him to replace the text in the rounded right button.

Any suggestion? Thanks.

+4
source share
2 answers

Create (or get a link to) the button, as usual, then, referring to the button, create your own counter and set it as a sub-item of the button. It might also be worth turning off the button to stop a few clicks.

Something in the following lines should do the trick:

  ... UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame = CGRectMake(10, 10, 70, 40); [button setTitle:@"Next" forState:UIControlStateNormal]; [button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; ... - (void)click:(UIButton *)button { [button setTitle:@"" forState:UIControlStateNormal]; UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; [spinner startAnimating]; spinner.frame = button.bounds; [button addSubview:spinner]; button.enabled = NO; // do your actual work... } 
+7
source

I wrote a category that uses related objects

UIButton + ActivityIndicator.h

 // Created by reynaldo on 1/16/14. #import <Foundation/Foundation.h> @interface UIButton (ActivityIndicator) - (void)startActivityIndicator; - (void)stopActivityIndicator; @end 

UIButton + ActivityIndicator.m

 // Created by reynaldo on 1/16/14. #import <objc/runtime.h> #import "UIButton+ActivityIndicator.h" static char TITLE_KEY; static char ACTIVITY_INDICATOR_KEY; @implementation UIButton (ActivityIndicator) - (void)startActivityIndicator { objc_setAssociatedObject(self, &TITLE_KEY, self.currentTitle, OBJC_ASSOCIATION_RETAIN_NONATOMIC); [self setTitle:@"" forState:UIControlStateNormal]; UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; [self addSubview:activityIndicatorView]; activityIndicatorView.frame = CGRectMake(self.frame.size.width / 2, self.frame.size.height / 2. - 2, 7, 7); [activityIndicatorView startAnimating]; objc_setAssociatedObject(self, &ACTIVITY_INDICATOR_KEY, activityIndicatorView, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } - (void)stopActivityIndicator { NSString *title = objc_getAssociatedObject(self, &TITLE_KEY); UIActivityIndicatorView *activityIndicatorView = objc_getAssociatedObject(self, &ACTIVITY_INDICATOR_KEY); if(activityIndicatorView) { [activityIndicatorView removeFromSuperview]; } if(title.length) { [self setTitle:title forState:UIControlStateNormal]; } } @end 
+1
source

All Articles