How can I replace the text in a round rect button with a spinner? - objective-c

I have a view with a generic rounded rect button that segues to another view. When users press the rounded rect it starts a fetch. I would like to have a spinning wheel replace the text ("Next") inside the rounded rect button while the fetch is processed.
I created the spinner:
UIActivityIndicatorView *spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[spinner startAnimating];
But I don't know how to make it replace the text in the rounded rect button.
Any suggestion? Thank you.

Create (or get a reference to) the button as usual, then, while handling the button click, create your spinner and set it as a subview of the button. It's probably also worth disabling the button to stop multiple clicks.
Something along 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...
}

I wrote a category that utilizes associated 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

Related

Program a UIButton to change UILabel

I am trying to use Objective-C in iOS Single View app to program a UIButton to change the text in a UILabel. Is there a way to run a function for this to be implemented in my "ViewController.m" file?
Here is my Source Code:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)loadView {
//UIBUTTON
//allocate the view
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//set the view's background color
self.view.backgroundColor = [UIColor whiteColor];
//create the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(50, 280, 280, 50);
//set the font size of the button
button.titleLabel.font = [UIFont systemFontOfSize:40];
//set the text color of the button, before it is pressed
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//set the text color of the button, while it is pressed
[button setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
//set the button's title
[button setTitle:#"3" forState:UIControlStateNormal];
//listen for clicks
[button addTarget:self action:#selector(buttonPressed)
forControlEvents:UIControlEventTouchUpInside];
//add the button to the view
[self.view addSubview:button];
//UILABEL
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
Question.text=#"How many horns does a Triceratops have?";
Question.backgroundColor = [UIColor grayColor];
Question.textColor = [UIColor whiteColor];
Question.font=[UIFont fontWithName:#"Helvetica" size:18 ];
[self.view addSubview:Question];
}
-(void)buttonPressed { }
#end
Is it possible to trigger the change in text in my buttonPressed function? Thanks so much for anybody's help!
The problem is that when you create your label and add it to the interface...
UILabel *Question=[ [UILabel alloc] initWithFrame:CGRectMake(10,200,350,60)];
// ...
[self.view addSubview:Question];
...you didn't keep a reference to it. So now, in buttonPressed, you have no way to talk about the label.
Make a property:
#interface ViewController ()
#property (nonatomic, weak) UILabel* questionLabel;
#end
... and when you create the label, remember to set that property to that label. Now you will be able to access the text of that label later on, in buttonPressed (or anywhere else), through the property.

Calling an IBAction in code and sending a tag through too

So I have an IBAction hooked up to multiple buttons in IB I want this action to be fired when another IBAction is called I also want it to recognise an int from the second action as the [sender tag].
// viewController.m
-(IBAction)buttonPress {
int getInter = [sender tag];
UIView *tmpView = [self.view viewWithTag:getInter];
NSString *title = [(UIButton *)sender currentTitle];
}
I then have another action that needs to call buttonPress.
-(IBAction)secondButtonPress {
int varInt = 1
[self buttonPress: nil] <--- How do I pass varInt that buttonPress would recognise as a sender Tag here?
}
I realise that I could just create the button like so and duplicate the code in secondButtonPress but that seems to messy...
UIButton *tmpButton2 = (UIButton *)[self.view viewWithTag:varInt];
So the question is, is there a way of tricking buttonPress into thinking it has been pressed and passing a variable through as the sender tag for that action. In a way programatically tricking buttonPress into thinking it has been pressed.
Thank you in advance.
I ended up hooking it up to the same IBAction and running an if statement to do something if that tag was called.
Seems simple now!
-(IBAction)buttonPress {
int getInter = [sender tag];
if ([sender tag] == 2) {
//Do something just for that button.
}
else
{
UIView *tmpView = [self.view viewWithTag:getInter];
NSString *title = [(UIButton *)sender currentTitle];
}
}
UIButton *btn1 = [[UIButton alloc] initWithFrame:CGRectMake(10, 10, 20, 20)];
[btn1 setTitle:#"Button 1" forState:UIControlStateNormal];
btn1.tag = 1;
[btn1 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
UIButton *btn2 = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 20, 20)];
[btn2 setTitle:#"Button 2" forState:UIControlStateNormal];
btn2.tag = 2;
[btn2 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton *btn3 = [[UIButton alloc] initWithFrame:CGRectMake(10, 90, 20, 20)];
[btn3 setTitle:#"Button 3" forState:UIControlStateNormal];
btn3.tag = 3;
[btn3 addTarget:self action:#selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
write button click method as following and get button tag in it..
-(IBAction)buttonClick:(id)sender {
UIButton *btn = (UIButton *) sender;
NSLog(#"%d",btn.tag);
}
Just set the tag in initWithNibName method. Follow the sample and see whether it is helpful for you!!
#property(nonatomic,strong)IBOutlet UIButton *btn1;
#property(nonatomic,strong)IBOutlet UIButton *btn2;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Set the Tag
self.btn1.tag=1;
self.btn1.tag=2;
}
return self;
}
-(IBAction)selectedButtonClick:(id)sender {
UIButton *btn = (UIButton *) sender;
NSLog(#"selectedButton%d",btn.tag);
}

UIButton added in code not responding to touch

I'm just adding a quick intro screen with a button, which compiles and displays properly, but my button doesn't respond.
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"viewDidLoad!");
[self showIntro];
}
// make a quick Intro screen
- (void) showIntro {
UIImageView *myBackImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
[myBackImage setImage:(UIImage *) [UIImage imageNamed:#"myBkgnd.png"]];
UIButton *clickBtn = [UIButton buttonWithType:UIButtonTypeCustom ];
[clickBtn addTarget:self action:#selector(myButton_pressed:) forControlEvents:UIControlEventTouchUpInside];
[clickBtn setImage:(UIImage *) [UIImage imageNamed:#"btnImage.png"] forState:UIControlStateNormal];
clickBtn.frame = CGRectMake(111, 200, 75, 60);
[myBackImage addSubview:clickBtn];
[self.view addSubview:myBackImage];
}
- (void)myButton_pressed:(UIButton*)button {
NSLog(#"Button Pressed!");
}
What am I overlooking?
Thanks!
As you have added UIButton in UIImageView but didn't you forgot imageView's userInteraction by default NO. So :
myBackImage.userInteractionEnabled = YES;
EDIT : Just formatted

How do I add resizing without IB

Since starting with iPhone app development (last 9 months) I have only used IB. I have a project to work on already built by another developer that I need to optimise for iPhone screen. No problem in IB, I know how to do that, in this project however the Nav bar is added using code only and is an image view. Could someone advise me how I go about resizing/positioning the nav bar when IB isnt used? Im trying to enhance this app for the iphone 5 screen.
#define BAR_FRAME CGRectMake(0,0,320.0f,43.0f)
#implementation ICNavbarView
#synthesize homeButton=__homeButton;
#synthesize prevButton=__prevButton;
#synthesize nextButton=__nextButton;
#synthesize delegate=__delegate;
- (id)initWithFrame:(CGRect)frame
{
LogCmd();
self = [super initWithFrame:BAR_FRAME];
if (self) {
self.backgroundColor = [UIColor clearColor];
self.alpha = 0.9f;
// Add Navigation bar background // <<<<<< navigation bar from ui image
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
// Add back button
__prevButton = [UIButton buttonWithType:UIButtonTypeCustom];
__prevButton.frame = CGRectMake(30.0f, 6.0f, 29.0f, 31.0f);
UIImage *prevButtonPressed = [UIImage imageNamed:#"BtnPrevPressed"];
[__prevButton setImage:[UIImage imageNamed:#"BtnPrev"] forState:UIControlStateNormal];
[__prevButton setImage:prevButtonPressed forState:UIControlStateSelected];
[__prevButton setImage:prevButtonPressed forState:UIControlStateHighlighted];
[__prevButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__prevButton];
// Add next button
__nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
__nextButton.frame = CGRectMake(262.0f, 6.0f, 29.0f, 31.0f);
UIImage *nextButtonPressed = [UIImage imageNamed:#"BtnNextPressed"];
[__nextButton setImage:[UIImage imageNamed:#"BtnNext"] forState:UIControlStateNormal];
[__nextButton setImage:nextButtonPressed forState:UIControlStateSelected];
[__nextButton setImage:nextButtonPressed forState:UIControlStateHighlighted];
[__nextButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__nextButton];
// Add home button
__homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
__homeButton.frame = CGRectMake(145.0f, 6.0f, 31.0f, 30.0f);
UIImage *homeButtonPressed = [UIImage imageNamed:#"BtnHomePressed"];
[__homeButton setImage:[UIImage imageNamed:#"BtnHome"] forState:UIControlStateNormal];
[__homeButton setImage:homeButtonPressed forState:UIControlStateSelected];
[__homeButton setImage:homeButtonPressed forState:UIControlStateHighlighted];
[__homeButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:__homeButton];
}
return self;
}
- (id)init
{
return [self initWithFrame:CGRectZero];
}
#pragma mark - Button handlers
- (void)buttonPressed:(id)sender
{
if (sender == __prevButton) {
[self.delegate performSelector:#selector(navBarPrevButtonPressed)];
} else if (sender == __homeButton) {
[self.delegate performSelector:#selector(navBarHomeButtonPressed)];
} else {
[self.delegate performSelector:#selector(navBarNextButtonPressed)];
}
}
#end
So far I tried UIViewAutoresizingFlexibleHeight; like this
// Add Navigation bar background
UIImageView *bgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"BgNavBarNew"]];
[self addSubview:bgView];
//resize
bgView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
This seems to make no difference and the nav bar hasn't moved
I've used this before, but I used the setter function instead of using the property. I don't know if that would change anything or not, but try it this way:
[bgView setAutoresizingMask:(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth)];
The parens are important, if you have multiple re-sizing options.

Button's action causes "invalid selector" crash -- why?

This code results in an "invalid selector" error when the button I create is pressed. Where is the test function fetched from?
Main.m
mainScreen = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
[self.view addSubview:mainScreen];
TaskButtons *tB = [[TaskButtons alloc] init];
[mainScreen addSubview:[tB TaskStart]];
TaskButtons.m
- (UIButton*)TaskStart {
CGRect buttonFrame = CGRectMake(500, 206, 400, 35);
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = buttonFrame;
[button setTitle:#"Task Button" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.titleLabel.textAlignment = UITextAlignmentLeft;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(test) forControlEvents:UIControlEventTouchUpInside];
return button;
}
- (void)test{
NSLog(#"test line");
}
It seems that the test function isn't being called. Doesn't setting the button's target to self here mean it should look in the TaskButtons class for the function called test?
The problem is ARC is releasing the instantiated object too soon. So to solve this I would need to retain it longer.
Main.h
#import "TaskButtons.m"
#interface ViewController : UIViewController {
TaskButtons *tB;
}
#property (nonatomic, retain) TaskButtons *tB;
[button addTarget:self action:#selector(test:) forControlEvents:UIControlEventTouchUpInside];
- (void)test:(id)sender{
NSLog(#"test line");
}
Syntax problem :) In your code replace these lines.