UICategory - UIButtons and UITextfield - ios

I want to create five buttons with same formats, so that i have created UICategory class for UIButton, But I don't get that category class calling, please help me on this,
My category class is as follows,
#implementation UIButton (headerBtn)
+(UIButton *)headerButtons{
UIButton *header = [[UIButton alloc]init];
[header setTitleColor:[UIColor headerBtnColor] forState:UIControlStateNormal];
header.titleLabel.font = [UIFont fontWithName:Fonts_ProximaNovaMedium size:13.0f];
return header;}
Am calling button like as below,
btn_pulse = [UIButton headerButtons];
is that correct way?

Change the line:
UIButton *header = [[UIButton alloc]init];
for
UIButton *header = [UIButton buttonWithType:UIButtonTypeCustom];
Anyway, I don't think you need a class method. I would do it this way:
#implementation UIButton (headerBtn)
-(void)headerStyle{
[self setTitleColor:[UIColor headerBtnColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont fontWithName:Fonts_ProximaNovaMedium size:13.0f];
}
And the you just need to import your category and:
UIButton *myButton = [[UIButton buttonWithType:UIButtonTypeCustom] headerStyle];

You have to import your category wherever you need it:
#import "UIButton+headerBtn.h"

Related

How to reuse properties

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.

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.

UIButton set as UIBarButtonItem is not disabled when set title on it (Works fine if I use UIImage in UIButton)

I have a UIBarButtonItem
#property (nonatomic, retain) UIBarButtonItem *editBarButton;
I simply insert a UIButton in UIBarButtonItem, with title, font & etc in viewDidLoad
editButton = [UIButton buttonWithType:UIButtonTypeCustom];
editButton.titleLabel.font = [UIFont fontWithName:#"Helvetica-light" size:18];
editButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
[editButton setTitle:#"Post" forState:UIControlStateDisabled];
//[editButton setImage:[UIImage imageNamed:#"status_post"] forState:UIControlStateNormal];
[editButton addTarget:self action:#selector(editBarButtonPress:)forControlEvents:UIControlEventTouchUpInside];
[editButton setFrame:CGRectMake(0, 0, 57, 30)];
self.editBarButton = [[UIBarButtonItem alloc] initWithCustomView:editButton];
self.navigationItem.leftBarButtonItem = self.editBarButton;
Latter I use it in many condition, like in viewWillAppear :
if (self.chatLogsArray.count)
{
self.editBarButton.enabled = YES;
}
else
{
self.editBarButton.enabled = NO;
}
But it is not disable while I use title in Button. But works fine if I set an UIImage in UIButton. Why is that? And what I am missing?
Thanks a lot in advance.
Maybe you should make property and disable UIButton instead UIBarButtonItem.
If you want to have both text and image on UIButton, you need to set an image as background:
[editButton setBackgroundImage:[UIImage imageNamed:#"status_post"] forState:UIControlStateNormal];

Create a function that is able to check the title of the button pressed

I have a loop for creating buttons dynamically.
for (int b = 0; b < _currentPlayerTeams.count; b++)
{
HNTeamObject *team = _currentPlayerTeams[b];
CGFloat buttonY = 168 + ((b + 1) * distanceBetweenButtons) - 23;
NSString *buttonTitle = [NSString stringWithFormat:#"%# %#",team.teamName, team.seriesName];
UIButton *button = [[UIButton alloc] init];
button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"images.png"] forState:UIControlStateNormal];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self
action:#selector(chooseTeamOne:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle: buttonTitle forState:UIControlStateNormal];
button.titleLabel.text = (#"%#", buttonTitle);
button.frame = CGRectMake(labelAndButtonX, buttonY, 268.0, 46.0);
[self.view addSubview:button];
}
I need to make a function that can act as the selector that for each button that is created and look at the button's title. Since I am making the buttons in the loop they are local and do not appear in another function that I create. Any help would be appreciated. I apologize in advance because I am very new to coding and am not quite up to speed with what is going on. Thanks.
Each button will call the chooseTeamOne: function when it is pressed so I assume you want to get the buttons title in that method.
To do so, use the UIButton's title property:
NSLog(#"%#", button.currentTitle);
This will log the button's current title. What is important to note is that I am referencing "button" which is the instance of the UIButton which called the chooseTeamOne method. I am assuming chooseTeamOne takes a UIButton button as a parameter.
If your method takes an 'id' as a parameter you would need to cast the sent object to a UIButton like this:
- (IBAction)chooseTeamOne:(id)sender {
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.currentTitle;
}
Do you need a separate selector to return the title? If not then..
I imagine that your chooseTeamOne function is something along the lines of:
-(IBAction)chooseTeamOne:(id)sender {
...do things...
}
in which case, just add
-(IBAction)chooseTeamOne:(id)sender {
UIButton *button = sender;
NSString *stringThatYouWant = button.titleLabel.text; //get the text on the button
...do things...
}

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")
}

Resources