UIButton not showing up in UIViewController - ios

I have set up my button as so:
CGRect button1Frame = CGRectMake(200, 200, 100, 100);
self.button1 = [[UIButton alloc] initWithFrame:button1Frame];
[self.button1 addTarget:self action:#selector(goToController1:)
forControlEvents:UIControlEventTouchDown];
self.button1.userInteractionEnabled = YES;
self.button1.opaque = YES;
self.button1.backgroundColor = [UIColor redColor];
[self.button1 setTitle:#"B1" forState:UIControlStateNormal];
[self.view addSubview:self.button1];
with my sender:
- (void) goToController1:(id)sender {
NSLog(#"Pressed");
}
and my view controller has been initialized properly.
What would cause my button not to show up? I have checked to make sure everything is being called properly.

You could try this:
button = [UIButton buttonWithType: UIButtonTypeSystem];
And then set the frame like this:
button.frame = CGRectMake(0,0,20,20);
The UIButton buttonWithType: method fixed my problem at one point. Not sure if it will make a difference in this case though. Just something to try.

Related

Adding multiple UIButtons to an UIView with a selector action

I've added some buttons to an UIView (via addSubview) programmatically. I’ve added to this button an #selector with a function. However, they appear in the view but when I do click, the last button works only.
Into my .h:
#property (nonatomic, strong) UIButton * myButton;
Into my .m
for(int i=0;i<5;i++){
myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:myButton];
}
-(void)myaction:(UIButton *)sender{
if(sender.tag == 0){
NSLog(#“uibutton clicked %ld", (long)sender.tag);
}
}
How do I add the action to all buttons? not for the last only...
This works fine:
- (void)viewDidLoad {
[super viewDidLoad];
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, 55*i, 30, 30);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}
}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
The code you posted in your question appears to be "this is what my code looks like" rather than your actual code. That can cause problems when people try to help.
In the future, post your actual code.
Let's make it more dynamic by calculating the height of the button and adding padding based on the number of buttons.
:
-(void)viewDidLoad {
[super viewDidLoad];
NSInteger height = self.frame.size.height - 5*20; // 5 is the button count
and 20 is the padding
NSInteger buttonHeight = height/5;
for(int i=0;i<5;i++){
UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(55, buttonHeight*i+padding*(i+1), 150,
buttonHeight);
myButton.tag = i;
myButton.backgroundColor = [UIColor redColor];
[myButton setTitle:[NSString stringWithFormat:#"%ld", (long)i]
forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myaction:)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
}}
-(void)myaction:(UIButton *)sender{
NSLog(#"uibutton clicked %ld", (long)sender.tag);
}
You could make it more dynamic by calculating the height of the button like this:
NSInteger height = self.frame.size.height - 5*20;
NSInteger buttonHeight = height/5;

How to make 3 clickable area on a button in Objective-C

I'm new in objective-C and I've create a project that use a button to change some elements from my view. I would like that depending on what part of the button I click, it displays different elements than previous.
For exemple when I click on the left area from button a red circle appears and when I click in the middle, it's the blue circle that appears and in the right area it's a green circle.
I tried searching on google and stackoverflow but I have trouble understanding how the selector used in this case. Could someone help me?
PS: Sorry for the mistakes, my English is not very good
I help you.
First you need the create the small custom view programmatically or through the xib or storyboard
UIView *viewButton = [[UIView alloc] initWithFrame: CGRectMake ( 100, 100, 300, 300)];
[self.view addSubView:viewButton];
Then Create the 3 buttons inside the viewButton
//First Button
UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonLeftRed = [UIButton buttonWithType:UIButtonTypeSystem];
buttonLeftRed.frame = CGRectMake(0, 0, 100, 100);
buttonLeftRed.tag = 1;
[buttonLeftRed setTitle:#"Red" forState:UIControlStateNormal];
[buttonLeftRed addTarget:self
action:#selector(actionPressMeOne:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonLeftRed];
//Second Button
UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonMiddleBlue = [UIButton buttonWithType:UIButtonTypeSystem];
buttonMiddleBlue.frame = CGRectMake(100, 0, 100, 100);
buttonMiddleBlue.tag = 2;
[buttonMiddleBlue setTitle:#"Blue" forState:UIControlStateNormal];
[buttonMiddleBlue addTarget:self
action:#selector(actionPressMeSecond:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonMiddleBlue];
//Third Button
UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeCustom];
OR
UIButton *buttonRightGreen = [UIButton buttonWithType:UIButtonTypeSystem];
buttonRightGreen.frame = CGRectMake(200, 0, 100, 100);
buttonRightGreen.tag = 3;
[buttonRightGreen setTitle:#"Blue" forState:UIControlStateNormal];
[buttonRightGreen addTarget:self
action:#selector(actionPressMeThird:) forControlEvents:UIControlEventTouchUpInside];
[viewButton addSubview:buttonRightGreen];
If you want to change circle button, you have to import the Quartzcore Framework
#import <QuartzCore/QuartzCore.h>
#pragma mark - UIButton Action Methods
//First
- (void)actionPressMeOne:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *button = sender;
//Then do whatever you want to do here
//half of the width
button.layer.cornerRadius = button.frame.size.width/2.0f;
button.layer.borderColor=[UIColor redColor].CGColor;
button.layer.borderWidth=2.0f;
button.clipsToBounds = YES;
[button setBackgroundColor:[UIColor redColor]];
}
//Second
- (void)actionPressMeSecond:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *buttonTwo = sender;
//Then do whatever you want to do here
//half of the width
buttonTwo.layer.cornerRadius = button.frame.size.width/2.0f;
buttonTwo.layer.borderColor=[UIColor blueColor].CGColor;
buttonTwo.layer.borderWidth=2.0f;
buttonTwo.clipsToBounds = YES;
[buttonTwo setBackgroundColor:[UIColor blueColor]];
}
//Third
- (void)actionPressMeThird:(UIButton *)sender
{
NSLog(#"Pressed Button Tag is - %#",sender.tag);
UIButton *buttonThree = sender;
//Then do whatever you want to do here
//half of the width
buttonThree.layer.cornerRadius = button.frame.size.width/2.0f;
buttonThree.layer.borderColor=[UIColor greenColor].CGColor;
buttonThree.layer.borderWidth=2.0f;
buttonThree.clipsToBounds = YES;
[buttonThree setBackgroundColor:[UIColor greenColor]];
}
Thank You:-)

Setting selectors for global UIButton

I want to return a global UIButton from a global class.
+(UIBarButtonItem *) globalButton {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
[myButton addTarget:self action:#selector(???)
forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
So if I call [Global globalButton] from my viewController, I can get this button. The problem I'm having is how to set the selector, if I'm calling if from my view controller?
You should change your code as follow, assuming you would be calling it from UIViewController
+(UIButton *) globalButton:(UIViewController*)delegate {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 40, 40);
if([delegate respondsToSelector:#selector(gloBalButtonTapped:)])
[myButton addTarget:delegate action:#selector(gloBalButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
return myButton;
}
//Below method should be present in your view controller than only it will be added to myButton click event.
-(IBAction)gloBalButtonTapped:(id)sender{
}
You should return UIButton only if you are creating UIButton, if you need to return UIBarButtonItem than you should create UIBarButtonItem or Xcode will give warning, just a suggestion though.
Just dont add target in Global Class wherever you want to use it just use it like this.
UIBarButtonItem *btn = [Global globalButton];
btn addTarget:self action:#selector(yourSelector)
forControlEvents:UIControlEventTouchUpInside];
Or you can make globalButton a property in GlobalClass and write this method in setGlobalButton and then you can use it as a property in any viewController.
Or if you want to control it in Global only then you can mention your selector mentioned in this class only. And in that method you can control the behaviour.
Try this in your controller:
UIBarButtonItem* button = [UIBarButtonItem globalButton];
[button addTarget:self action:#selector(yourDidTapAction:) forControlEvents:UIControlEventTouchUpInside];
or this in globalButton:
+(UIBarButtonItem*)globalButtonWithTarget:(id)target andAction:(SEL)action
{
// ...
[myButton addTarget:target action:action
forControlEvents:UIControlEventTouchUpInside];
// ...
}
I found the easiest way to do this is below:
+(UIBarButtonItem *) buttonWithSelector:(SEL)selector andSender:(id)sender {
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(-40.0, 10.0, 40, 40);
[myButton addTarget:sender action:selector forControlEvents:UIControlEventTouchUpInside];
return myButton;
}

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).

Update UIButton title after receiving reply from SKProductsRequest for showing in-app purchases price

I'm trying to create two buttons whose label is initially set as a default value. The two buttons are managed by a popOverViewController, in which the viewDidLoad method initialise all the controls. I also have another method called addButtons that gets executed by the AppDelegate after receiving a valid response from Apple's servers containing products descriptions and prices.
My problem is that I can't get the label updated with that method.
My viewDidLoad method in the popOverViewController is:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
NSString *priceWeekly = [NSString stringWithFormat:#"weekly subscription"];
[button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
[button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
[self.view addSubview:button];
[self.view addSubview:button2];
}
and my update label method, of which I'm sure it gets called at the right time, is:
-(void)addButtons {
[self.button setTitle:#"updated label" forState:UIControlStateNormal];
[self.button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
}
can you please check my code and suggest the right way to update the labels? Basically I want to initialize the controls with a default label because of the waiting time between controls creation and server response.
UPDATE:
I have updated the code as follows, but still everything that's inside the addButtons method gets executed as I have an NSLog to check, but the button title still remains as created in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Add a label to the popover's view controller.
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
ilManifestoAppDelegate *delegate = (ilManifestoAppDelegate*)[[UIApplication sharedApplication] delegate];
[button addTarget:self
action:#selector(button1Week:)
forControlEvents:UIControlEventTouchDown];
// [button setTitle:priceWeekly forState:UIControlStateNormal];
button.frame = CGRectMake(0.0, 0.0, 200.0, 50.0);
[button2 addTarget:self
action:#selector(button1Month:)
forControlEvents:UIControlEventTouchDown];
// [button2 setTitle:#"monthly subscription" forState:UIControlStateNormal];
button2.frame = CGRectMake(0.0, 55.0, 200.0, 50.0);
// [self addButtons];
NSString *weekPrice = [NSString stringWithFormat:#"settimanale a %#",delegate.priceWeek];
[button setTitle:weekPrice forState:UIControlStateNormal];
[button2 setTitle:#"updated label 2" forState:UIControlStateNormal];
NSLog(#"week price %#", weekPrice);
[self.view addSubview:button];
[self.view addSubview:button2];
}
-(void)addButtons {
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setTitle:#"modificato" forState:UIControlStateNormal];
[self.view addSubview:button];
NSLog(#"addButtons executed");
}
In viewDidLoad you are using button and button2. In addButtons you are using self.button and self.button2. Did you ensure that these are the same buttons?
Also place a breakpoint in addButtons to see if it really is called, if you haven't done this yet.
Have you also created the outlet to the buttons in the xib, if you haven't placed them only with code.

Resources