I want to hide button Objective-C ios? - ios

When I define the button on .h, it works but when I code the button does not work.
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(clickActionItem) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[self.button Sethidden:YES]:
self.button.hidden=YES;
_button.hidden=YES;
}
But not work

Issue
The problem is that you are creating UIButton on viewDidLoad Method and you don't keep the instance stored so you cannot access it. Another issue is that you are giving another method to selector of the button.
Solution
Change your target at selector and add the UIButton as parameter just like the code below
[button addTarget:self action:#selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
And the method btnClicked take the instance of the button which is passed at button
-(void)btnClicked:(UIButton *)button
{
[button sethidden:YES];
}
This should do the trick.

#implementation YourView{
UIButton *button;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"nl.png"];
//create the button and assign the image
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(230,302,32,32);
[button setImage:buttonImage forState:UIControlStateNormal];
//create a UIBarButtonItem with the button as a custom view
//UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[button addTarget:self action:#selector(btnClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)btnClicked
{
[button setHidden:YES];
}
Your code did not give sufficient information for to see what you intend to do. Can you also provide the header file?

Related

UIButton placed as a UIBarButttonItem at the the Navigation bar won't trigger method when tapped

I have created a custom UIButton and placed it at the right hand side of the navigation controller. But when I tapped on it, it doesn't trigger the buttonTapped method and printout Testing 123, what have I done wrong?
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UIImage *btnImage = [[UIImage alloc] init];
BOOL unlike = NO;
if (unlike){
btnImage = [UIImage imageNamed:#"ic_favorite_border_48pt.png"];
}else{
btnImage = [UIImage imageNamed:#"ic_favorite_48pt.png"];
}
UIButton *heart = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect listButtonFrame = heart.frame;
listButtonFrame.size = btnImage.size;
heart.frame = listButtonFrame;
[heart setImage:btnImage forState:UIControlStateNormal];
[heart addTarget:self.navigationController.parentViewController
action:#selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *jobsButton = [[UIBarButtonItem alloc] initWithCustomView:heart];
self.navigationItem.rightBarButtonItem = jobsButton;
}
-(void)buttonTapped:(UIButton*)heart{
NSLog(#"Testing 123");
}
#end
[heart addTarget:self.navigationController.parentViewController
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];
Probably needs to be
[heart addTarget:self
action:#selector(buttonTapped:)
forControlEvents:UIControlEventTouchUpInside];

Is there a way of doing an action locally on a UIButtton?

I am setting up a table with an I have this code:
UIButton *done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:done];
return cell;
This doesn't work beacuse this button is only declared in this scope and I want to simply change its image on this click
Change the image in the done: method.
- (void)done:(id)sender
{
UIButton *button = (UIButton *)sender;
[button setImage:[UIImage imageNamed:#"yourImage.png"]
forState:UIControlStateNormal];
}
What does you done method look like?
I think that if you add a sender argument to the method, you can access the button.
- (void)done:(id)sender
{
UIButton *button = (UIButton*)sender;
[button doStuff];
}
You have to declare an ivar or a private/public property. So you can change its image from everywhere, for example:
#interface ViewController ()
#property UIButton *done;
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.done = [[UIButton alloc]initWithFrame:CGRectMake(20, 25, 40, 40)];
[self.done setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[self.done addTarget:self action:#selector(done:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.done];
}
- (void)done:(UIButton *)sender
{
//Do something.. like change image
[self.done setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
}
#end
Or you declare the sender as the UIButton, like this:
- (void)done:(id)sender
{
UIButton *done = (UIButton *)sender;
//Do something with the button
}

Navigation Bar custom button

backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:#selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];
The problem i am facing is that though the button dimensions are44*44, wherever i tap anywhere around it, the the button action is fired.
Please try the bellow code : its working properly
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *buttonImage = [UIImage imageNamed:#"back.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
[customBarItem release];
}
-(void)back {
[self.navigationController popViewControllerAnimated:YES];
}
Its not a bug. It is a default behaviour. In iPhone, for navigation bar buttons the touch detection is little more expanded rather than its frame. Just have a look on any other application. Everywhere the button get fired if we tap nearer but outside its frame.
It's the intended behaviour, if you really want to limit the area of the touch, you can wrap the button inside a UIView:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];

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

Toggle Button Image

I have multiple of buttons.
They all load in the viewDidLoad. I want it so once one button has been pressed, it changes to the other image.
So Img1 loads via the viewDidload, then once it's been pressed, it goes to Img2
This is my current code which i have tried:
In the viewdidload:
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(playSound:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
In the ibaction (which i tried, but im guessing)
-(IBAction)ButtonAction
{
if (btn1==0)
{
btn1=1;
[btn1 setImage:[UIImage imageNamed:#"img1.png"] forState:UIControlStateNormal];
}
else
{
[btn1 setImage:[UIImage imageNamed:#"img2.png"] forState:UIControlStateNormal];
btn1=0;
}
}
Thanks for the help
You need to make sure you are hooking up your UIButton to the proper selector. Also, I would use an #property or a global / instance variable to track which image is currently in the UIButton. Here's some code that should do the trick:
//... in #interface
#property (nonatomic, strong) NSString *btn1ImageName;
- (void)viewDidLoad {
[super viewDidLoad];
// ...
self.btn1ImageName = #"img1.png";
btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
[btn1 setFrame:CGRectMake(367, 117, 97, 25)];
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
[btn1 addTarget:self action:#selector(toggleImage:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)toggleImage:(UIButton *)btn1 {
self.btn1ImageName = ([self.btn1ImageName isEqualToString:#"img1.png"]) ? #"img2.png" : #"img1.png";
[btn1 setImage:[UIImage imageNamed:self.btn1ImageName] forState:UIControlStateNormal];
}
if (btn1==0)
I don't get that part.
One easy solution would be to have two different images for a selected and un-selected state.
btn1.selected = YES, will automatically choose the selected button image.
btn1.selected = NO, will automaticall choose the un-selected button image.

Resources