How to reuse properties - ios

I have many different buttons in my app, yet most of them have the same properties assigned to them:
login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[login setTitle:#"Login" forState:UIControlStateNormal];
[login.titleLabel setFont:[UIFont fontWithName:#"Avenir Next" size:18]];
[login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[login setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[login setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
Is there any way to create a class or something of a button that already has these default properties assigned? So I could simply go something like:
CustomButtom *btn = [CustomButton alloc]init];
Then the btn will have all of the above properties assigned?
Thanks.

Another way of handling this, is you can create a private method that will return the UIButton with the same properties. I think creating a subclass of UIButton is a little unnecessary.

You can do this by creating a CustomButton class
Xcode -> New File -> Cocoa Touch Class -> Next -> Name Your Button-> Select Subclass of UIButton
CustomButton.h file
#import <UIKit/UIKit.h>
#interface CustomButton : UIButton
#end
CustomButton.m file
#import "CustomButton.h"
#implementation CustomButton
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
//login = [[UIButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[self setTitle:#"Login" forState:UIControlStateNormal];
[self.titleLabel setFont:[UIFont fontWithName:#"Avenir Next" size:18]];
[self setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithWhite:0.7 alpha:1] forState:UIControlStateHighlighted];
[self setTitleColor:[UIColor colorWithWhite:0.5 alpha:1] forState:UIControlStateDisabled];
}
#end
Now, Call you button
CustomButton *customButton = [[CustomButton alloc]initWithFrame:CGRectMake(8, CGRectGetMaxY(password.frame) + 16, loginView.frame.size.width - 16, 40)];
[customButton addTarget:self action:#selector(loginButtonPressed:) forControlEvents:UIControlEventTouchDown];
[YourView addSubview:customButton];

You've got two options:
Make a function that you can pass in a UIButton to that sets the properties for you
Write a Category for UIButton that does the same as the above. Categories allow you to add functionality to a class without subclassing. See https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html

Yes. You can subclass the UIButton. When you override the init method setting the properties you could get the button's with the same properties.

Related

Create a button program programmatically in a seperate class

I am developing a snake game and for that I created 3 classes: Player, Control, and Grid. For my question, I would like to create the play button programmatically in which the function of creating a button is in the Control class and this function is called in the ViewController.m
In ViewController.h, I defined
#property (nonatomic, strong) Control *control; //object of Control class
#property (weak) IBOutlet UIButton *button;
In ViewController.m:
self.control = [[Control alloc] init];
[control createButton:_viewC Button:_button]; //_viewC is the view where the button will be shown
[_button addTarget:self action:#selector(play) forControlEvents:UIControlEventTouchUpInside]; //since play method in ViewController.m
In Control class:
-(void)createButton:(UIView*)view Button:(UIButton*)button
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:#"Play" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal ];
button.backgroundColor = [UIColor whiteColor];
button.titleLabel.font = [UIFont fontWithName:#"Helvetica-Bold" size:10];
button.layer.cornerRadius = 10;
button.frame = CGRectMake(35, 30, 70, 40);
[view addSubview:button];
}
The problem is when I run the game and press the button, no action happens! can someone help me with this. Thanks
Assuming that the code in the 'play' method is correct you can try setting the target in the Control class by changing the declaration to:
-(void)createButton:(UIView*)view Button:(UIButton*)button Selector:(SEL)selector
Then add this within that method:
[button addTarget:self.superview action:selector forControlEvents:UIControlEventTouchUpInside];
Finally in ViewController.m change the line to this:
[control createButton:_viewC Button:_button selector:#selector(play)];
and remove the line underneath.

iOS : Radio Button Programatically Without using any Image

I have been working on creating custom ui controls and want to know how to add radio button to UIView programatically.
I only found one solution but it is for mac osx application control.
Image of required result is given as.
LIMITATION
Not want to use image.
Thanks.
Radio button - Set corner radius and board color as below. Take three button in ViewController.h file
#property(nonatomic,retain) IBOutlet UIButton *btn1;
#property(nonatomic,retain) IBOutlet UIButton *btn2;
#property(nonatomic,retain) IBOutlet UIButton *btn3;
- (IBAction)ClickBtn1:(id)sender;
- (IBAction)ClickBtn2:(id)sender;
- (IBAction)ClickBtn3:(id)sender;
And in ViewDidLoad method.
- (void)viewDidLoad
{
[super viewDidLoad];
self.btn1.layer.cornerRadius = 10;
self.btn1.layer.borderColor = [[UIColor blackColor] CGColor];
self.btn2.layer.cornerRadius = 10;
self.btn2.layer.borderColor = [[UIColor blackColor] CGColor];
self.btn3.layer.cornerRadius = 10;
self.btn3.layer.borderColor = [[UIColor blackColor] CGColor];
// default
[self.btn1 setTitle:#"." forState:UIControlStateNormal];
[self.btn2 setTitle:#"" forState:UIControlStateNormal];
[self.btn3 setTitle:#"" forState:UIControlStateNormal];
}
Button Action on click.
- (IBAction)ClickBtn1:(id)sender
{
[self.btn1 setTitle:#"." forState:UIControlStateNormal];
[self.btn2 setTitle:#"" forState:UIControlStateNormal];
[self.btn3 setTitle:#"" forState:UIControlStateNormal];
}
- (IBAction)ClickBtn2:(id)sender
{
[self.btn2 setTitle:#"." forState:UIControlStateNormal];
[self.btn1 setTitle:#"" forState:UIControlStateNormal];
[self.btn3 setTitle:#"" forState:UIControlStateNormal];
}
- (IBAction)ClickBtn3:(id)sender
{
[self.btn3 setTitle:#"." forState:UIControlStateNormal];
[self.btn1 setTitle:#"" forState:UIControlStateNormal];
[self.btn2 setTitle:#"" forState:UIControlStateNormal];
}
in Your StoryBoard button View set as below height and Width (20,20) of the button. Take title name Dot (.) and its font size System 44.0and set Edge as below image.
Your Radio button is :

Creating a UIButton in a subclass of UIButton

I am trying to create a UIButton in a class that is a subclass of UIButton. The reason for this is that it would be easier for my app to do this. I am wondering if it is possible. I am able to create the button, but the action is not working. My selector exists, but is not getting called.
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, kThumbSide - 350, kThumbSide, 16)];
[button2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button2 setTitle:[NSString stringWithFormat:#"%#", [data objectForKey:#"username"]] forState:UIControlStateNormal];
[button2 sizeToFit];
button2.backgroundColor = [UIColor clearColor];
button2.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button2.font = [UIFont fontWithName:#"Arial" size:15];
[button2 addTarget:delegate action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button2];
Hey you are creating an Object of UIButton class not any Class clear???
and your target method are getting wrong Delegate.
try it
[button2 addTarget:self action:#selector(showUserViews) forControlEvents:UIControlEventTouchUpInside];
replace delegate with self for getting showUserViews method in same class
I haven't see any UIButton subclass here, perhaps you've asked your question wrong.
Any way you should do this:
- (void) viewDidLoad {
[super viewDidLoad]
UIButton *b = [[UIButton alloc]initWithFrame:YOUR_DESIRED_FRAME];
[b setBackgroundColor:[UIColor redColor]]
[b addTarget:self action:#selector(bPressed:) forControlEvents:UIControlEventTouchUpInside]
}
- (void)bPressed:(UIButton *)sender {
NSLog(#"b button pressed")
}

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

Change Existing Set UIColors on UIButton Press

The objective is to change the font color and background color of my UITextView upon UIButton click and then revert them back to the original colors when the UIButton is clicked again; then repeat, ect.
Default Color Settings
Text: blackColor
Background: lightGrayColor
Upon Button Click (change to..)
Text: greenColor
Background: blackColor
(then if clicked again, change back to default color settings)
So far I have this:
- (IBAction) enableSleepMode:(id)sender {
[txtNotes setTextColor:[UIColor greenColor]];
[txtNotes setBackgroundColor:[UIColor blackColor]];
}
I apologize but I'm not exactly sure where to go from here. Thanks in advance.
You said to change the font color and background color of 'UITextView' not 'UIButton' Right? . Then did you add UItextViewDelegate?
I have done this like
EDIT:
- (void)viewDidLoad
{
[super viewDidLoad];
[txtView setBackgroundColor:[UIColor lightGrayColor]];
[txtView setTextColor:[UIColor blackColor]];
str = #"first";
}
-(IBAction)bt:(id)sender
{
if([str isEqualToString:#"first"])
{
[txtView setBackgroundColor:[UIColor blackColor]];
[txtView setTextColor:[UIColor greenColor]];
str = #"second";
}
else
{
[txtView setBackgroundColor:[UIColor lightGrayColor]];
[txtView setTextColor:[UIColor blackColor]];
str = #"first";
}
}
try this
(void)setTextColor:(UIColor *)color forState:(UIControlState)state
[button setTextColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTextColor:[UIColor blackColor] forState:UIControlStateHighlighted];
(void)setBackgroundColor:(UIColor *)color forState:(UIControlState)state
[button setBackgroundColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor lightgrayColor] forState:UIControlStateHighlighted];
something like that is i think what you're looking for

Resources