This issue is baffling me for a few days now.
I have 2 UIButtons, one created using the Storyboard and one created programmatically.
When I press one of the buttons, I am moved to a method that hides both the programatically built and storyboard buttons, but only the storyboard button disappears, whereas the programmatically built button remains. I have tried not only hiding but also changing the frame of the programmatically built button, but to no avail.
Here is the code used:
#property (strong, nonatomic) UIButton *catBut;
#property (weak, nonatomic) IBOutlet UIButton *whenButton;
....
....
-(void)viewDidLoad {
....
[self customizeButton:self.catBut withFrame:CGRectMake(165, 113, 135, 50)
andAction:#selector(selectedCat) andColor:[UIColor belizeHoleColor]
andTag:2 andImage:#"coffee-64.png" andText:#"Cafes" andAlignmentLeft:YES];
....
}
- (void)customizeButton:(UIButton *)but withFrame:(CGRect)rect andAction:(SEL)action
andColor:(UIColor *)col andTag:(NSUInteger)tag
andImage:(NSString *)imageName
andText:(NSString *)buttonText
andAlignmentLeft:(BOOL)alignLeft {
but = [UIButton buttonWithType:UIButtonTypeCustom];
[but setFrame:rect];
[but setTitle:buttonText forState:UIControlStateNormal];
but.tag = tag;
but.titleLabel.numberOfLines = 0;
but.titleLabel.font = [UIFont boldFlatFontOfSize:18];
[but setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
CGFloat spacing = 10; // the amount of spacing to appear between image and title
but.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
but.imageEdgeInsets = UIEdgeInsetsMake(0, spacing, 0, 0);
but.titleEdgeInsets = UIEdgeInsetsMake(0, spacing*2, 0, 0);
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateNormal];
[but setTitleColor:[UIColor cloudsColor] forState:UIControlStateHighlighted];
[but addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[self.buttonsView addSubview:but];
}
....
- (void)selectedCat {
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
}
Why is only the Storyboard button compliant to UI changes? Is there something I am missing? I have tried cancelling the AutoLayout but that didn't change a thing.
The issue is with the passing for argument in your method (customizeButton:(UIButton *)but).
Change your code to this and check
but = [UIButton buttonWithType:UIButtonTypeCustom];
to
self.catBut = [UIButton buttonWithType:UIButtonTypeCustom];
......
[self.buttonsView addSubview:self.catBut];
OR You should pass the address customizeButton:(UIButton **)but in the method
and while calling use &self.catBut
It is BOOL type, not an bool type.
try this
self.catBut.hidden = YES;
self.whenButton.hidden = YES;
I can see this property catBut, But I cann't see creation for this, as well I didn't give Outlet too. Check this. You've tried something but simply fix by following line.
[self.buttonsView addSubview:but];
self.catBut = but;
Related
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.
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];
I have a UIButton inside of a UIView inside of a UIImageView that is not responding to touches. I can't figure out why. I've broken the code down to its simplest form while still doing what my original program does.
#import "TestVideoViewController.h"
#interface TestVideoViewController ()
#property (strong, nonatomic)UIImageView *panel;
#property (strong, nonatomic)UIView *panelSC;
#property (strong, nonatomic)UIButton *panelButtonSC;
#property (strong, nonatomic)UIButton *videoButton;
#end
#implementation TestVideoViewController
-(void) viewDidLoad {
_panelButtonSC = [UIButton buttonWithType:UIButtonTypeCustom];
[_panelButtonSC addTarget:self action:#selector(buttonPressedSC:) forControlEvents:UIControlEventTouchUpInside];
[_panelButtonSC setTitle:#"Open" forState:UIControlStateNormal];
_panelButtonSC.frame = CGRectMake(511, 701, 66, 67);
_panel = [[UIImageView alloc] initWithFrame:CGRectMake(100, 768, 824, 600)];
_panel.backgroundColor = [UIColor whiteColor];
_panelSC = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 784, 560)];
_panelSC.backgroundColor = [UIColor whiteColor];
_videoButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_videoButton addTarget:self action:#selector(playVideo:) forControlEvents:UIControlEventTouchUpInside];
[_videoButton setTitle:#"Play" forState:UIControlStateNormal];
[_videoButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_videoButton.frame = CGRectMake(400, 400, 200, 40);
[_panelSC addSubview:_videoButton];
[_panel addSubview:_panelSC];
[self.view addSubview:_panel];
[self.view addSubview:_panelButtonSC];
}
- (IBAction)buttonPressedSC:(UIButton*)sender {
[self animatePanelUp];
}
- (IBAction)playVideo:(UIButton*)sender {
NSLog(#"play video");
}
- (void) animatePanelUp {
[UIView animateWithDuration: 0.5
delay: 0.0
options: UIViewAnimationOptionCurveEaseIn
animations:^{
_panel.frame = CGRectMake(47, 166, 929, 602);
}
completion:nil];
}
#end
I've looked at every similar answer here. To the best of my knowledge all of my framing is correct. There has got to be something simple and stupid that I'm missing. Why doesn't the "playVideo" method ever fire? The only time it works is if I add the button directly to self.view.
UIImageView keeps userInteractionEnabled as false by default - add following line of code it will work.
_panel.userInteractionEnabled = true;
As you are adding a few subview upon one another, check that your button is clickable and no other subview is eating up the touch event, and overall make sure that your views are user action enabled.
Hint:
There might have some problem with the imageview used, as it will have user interaction disabled by default.
Use UIView instead or do this:
_panelSC.userInteractionEnabled = true;
Make sure user interaction is enabled of the parent view. UIImageView by default has userInteractionEnabled false.
_panel.userInteractionEnabled = true
I have got a problem:
I have set up a property:
#property (strong, nonatomic) UIButton *buttonX;
Also I have a method to create a button:
-(void)createChooseButton : (UIButton *) button
{
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(0, 0, 50, 50);
button.layer.borderWidth = 1;
button.layer.borderColor = [[UIColor greenColor] CGColor];
if ([button isEqual:self.buttonX]) {
NSLog(#"Hello");
[button setTitle:#"X" forState:UIControlStateNormal];
button.center = CGPointMake(self.view.frame.size.width/3, self.view.frame.size.height*2/3);
}
[self.view addSubview:button];
}
When I call this method from viewDidLoad, it does not work, but if I don't use If statement (with isEqual) everything is ok.
Please could you help me, what am I doing wrong?
isEqual will not work here, because you have two pointer here one point to button & second to buttonX. if you need to check the button , give a tag property to buttonX in didload & then check it in createChooseButton method.
When you pass thebuttonX in first place it's nil.
You have toalloc it and then call the method.
you should call
_buttonX = [UIButton buttonWithType:UIButtonTypeRoundedRect];
and then call
[createChooseButton:self.buttonX]
don't call buttonWithType:UIButtonTypeRoundedRect in the method
I have one method that creates a number of UIButton instances programmatically and positions them in the view. Another IBAction needs to change the background colour of one of these UIButton when fired however when I try to action it, it tells me that the Property not found on object of type UIView *
The button is created using:
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[view addSubview:button];
When I try to access it from the other IBAction like:
button.backgroundColor = [UIColor blackColor];
It fails with the error noted above.
Any help is much appreciated. I saw another answer which suggested to create an array as an IBOutlet and access it that way but I'm wondering if there's another way.
Thanks!
declare this button in globally and identify each button in tag, just like or assume this
UIButton * button, *button2; // declare in your view controller.h
- (IBAction)butaction:(id)sender; //this is your color change method;
in your view controller.m wherever u add this button
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=10; //this is important
[self.view addSubview:button];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.tag=20; //this is important
[self.view addSubview:button1];
//here ur color change method
- (IBAction) butaction:(UIButton*)sender {
switch (sender.tag)
{
case 10:
[button setBackgroundColor:[UIColor blackColor]];
break;
case 20:
[button1 setBackgroundColor:[UIColor blackColor]];
break;
default:
break;
}
}
Try this, it may be helpful
for (int index=1; index<=5; index++) {
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(ButtonClickedFunction:) forControlEvents:UIControlEventTouchUpInside];
button.tag= index;
[view addSubview:button];
};
implement this in IBAction
-(IBAction)ButtonClickedFunction:(UIButton *)button{
button.backgroundColor = [UIColor blackColor];
}
Note: In this I have created 5 buttons but not set frame. Please set frame according to your requirements.
Thanks