Creating a UIButton in a subclass of UIButton - ios

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

Related

Make navigation bar title clickable Objective C iOS 10.2

Is there any way to make the navigation bar title clickable? I don't have any specific class for navigation bar. I control everything from ViewController. If there's a way, I would love to know. I have been searching it for quite a white but couldn't find a suitable answer.
Make button on Navigation bar
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self
action:#selector(btnclick:)
forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, self.view.frame.size.width, 64.0);
self.navigationItem.titleView =button;
-(void)btnclick:(id)sender
{ NSLog(#"button click");
}
You can try like this way
UIView *topView = [[UIView alloc]initWithFrame:CGRectZero];
UIButton * button = [[UIButton alloc]initWithFrame:CGRectZero];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title here" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button sizeToFit];
[topView addSubview:button];
[topView sizeToFit];
self.navigationItem.titleView = topView;
-(void)buttonAction:(Id) sender
{
NSLog(#"button clicked");
}

Call methods on UIButton objects in NSMutableArray Objective-C

I have an array called buttons which consists of UIButton objects. I populate it as follows:
for (int i=0; i<self.numButtons; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Title" forState:UIControlStateNormal];
button.frame = CGRectMake(xCoord-40, y, 80, 20);
[button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[button setTag:i];
[self.buttons addObject:button];
[self.view addSubview:button];
y+= 45;
}
The action for these buttons is buttonAction. This action is supposed to change the color of the selected button. Here is how I have it implemented:
-(void) buttonAction:(id)sender{
int num = (int)[sender tag];
[[self.buttons objectAtIndex:num] setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
NSLog(#"%#", [self.buttons objectAtIndex:buttonClicked].titleLabel.text);
}
The result is that there is no color change. Even when I try to NSLog the title of the button it returns (null). So my question is how can I fix this but also how do I call methods on objects inside arrays? Any help would be appreciated. Thanks!
All evidence points to self.buttons being nil. Did you ever initialize it?
Somewhere you need a line like:
self.buttons = [NSMutableArray array];
Of course you could use the sender parameter to your buttonAction: method:
- (void)buttonAction:(UIButton *)button {
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}
Note the change in the parameter.
I like to do this. Don't forget to use IBAction. It will help if you decide to use a nib or storyboard.
-(IBAction) buttonAction:(id)sender{
[((UIButton*)sender) setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]
NSLog(#"%#", ((UIButton*)sender).titleLabel.text)
}

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.

Problems in uiview hide and show

Actually i have to make a view appear and disappear once the button is clicked.please tell me the specific code to make it done
happyView=[[UIView alloc]init];
happyView.frame=CGRectMake(MainView.frame.size.width*0.26,CGRectGetHeight(happyBtn.frame)*2.2, CGRectGetWidth(MainView.frame)/1.6, CGRectGetHeight(MainView.frame)/3);
happyView.layer.cornerRadius=8.0;
happyView.layer.borderWidth=0.5;
happyView.layer.borderColor=[[UIColor grayColor]CGColor];
[MainView addSubview:happyView];
LettingGo=[[UIButton alloc]init];
LettingGo.frame=CGRectMake(happyView.frame.size.width*0.01,happyView.frame.size.height*0.1, CGRectGetWidth(happyView.frame)/1.05, 50);
[LettingGo setTitle:#"Letting go of negativity" forState:UIControlStateNormal];
LettingGo.titleLabel.numberOfLines=2;
LettingGo.titleLabel.adjustsFontSizeToFitWidth=YES;
[LettingGo setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LettingGo];
LivingPresent=[[UIButton alloc]init];
LivingPresent.frame=CGRectMake(happyView.frame.size.width*0.01,LettingGo.frame.size.height*1.4, CGRectGetWidth(happyView.frame)/1.05, 40);
[LivingPresent setTitle:#"Living in the present" forState:UIControlStateNormal];
LivingPresent.titleLabel.adjustsFontSizeToFitWidth=YES;
[LivingPresent setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[happyView addSubview:LivingPresent];
happyView.hidden=YES;
...
- (void)tapHappy:(id)selector {
happyView.hidden=NO;
}
UIButton has a method addTarget to call a method when an action has happened on the button
[yourButton addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
This will search the class it's in "self" for a method "tapHappy:" when the user lifts their finger from the button "UIControlEventTouchUpInside"
Obviously you can change these values to suit your needs
But that should be what you're looking for I think..
And then maybe change the tapHappy method so it isn't ALWAYS setting YES to the hidden property... maybe something like
happyView.hidden=!happyView.hidden;
That will toggle it on and off
When you call tapHappy method check weather your happyView hidden or not.
UIButton * yourButton=[[UIButton alloc]init];
yourButton.frame=CGRectMake(YOUR_FRAME);
[yourButton setTitle:#"Title" forState:UIControlStateNormal];
[yourButton addTarget:self action:#selector(tapHappy:) forControlEvents:UIControlEventTouchUpInside];
[yourButton setBackgroundColor:[UIColor colorWithRed:0.400f green:0.737f blue:0.761f alpha:1.00f]];
[YourView addSubview:yourButton];
-(void)tapHappy:(id)selector{
if(happyView.hidden)
happyView.hidden = NO;
else
happyView.hidden = YES;
}

ACTION for UIBUTTON in iOS

I have one custom class with subclass of UIView, inside this I created one button dynamically.
Now I want to set Action for button method. I set normal like UIviewController.
But its not working.
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
UIButton *but=[UIButton buttonWithType:UIButtonTypeDetailDisclosure];
but.frame= CGRectMake(200, 15, 15, 15);
[but setTitle:#"Ok" forState:UIControlStateNormal];
[but addTarget:self action:#selector(aMethod)
forControlEvents:UIControlEventTouchUpInside];
[self addSubview:but];
// Initialization code
}
return self;
}
-(void)aMethod
{
NextEyeViewController *nexteye=[[NextEyeViewController alloc]initWithNibName:#"NextEyeViewController" bundle:nil];
[self presentViewController:nexteye animated:YES completion:nil];
}
-(void)aMethod is my method name for button
If you want to set an action to your button created programmatically you need to do like this :
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 160.0, 40.0);
[view addSubview:button];
Create UIButton:
UIButton *yourButton = [[UIButton alloc] initWithFrame:frameButton];
// Here code to set button properties: color, label...
[yourButton addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:yourButton];
Your method has to receive the sender of the action:
-(void)aMethod:(id)sender {
// your code
}
Update:
You also have to call your method suitably with #selector(aMethod:) instead of #selector(aMethod).

Resources