UIButton Click Not working when its triggered from static library - ios

We are trying to trigger display button from static library project using webview. while integrating static library with single view application we have got the output design. but when we tried to attempt action by clicking on Button its not working.
Below is the code,which we have used inside cocoa touch static library
UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom];
imageButton.frame = CGRectMake(self.view.frame.size.width-50, 10, 50, 50);
[imageButton setImage:[UIImage imageNamed:#"close.png"] forState:UIControlStateNormal];
imageButton.adjustsImageWhenHighlighted = NO;
// [imageButton addTarget:self action:#selector(close:) forControlEvents:UIControlEventTouchUpInside];
[imageButton addTarget:self action:#selector(hideWebViewBtn:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)hideWebViewBtn:(id)sender {
NSLog(#"Hide Button clicked");
[self.adView removeFromSuperview];
self.adView = nil;
return;
}
Below is the screenshot which displayed in single view application after integration and running.
Once i click on the button its not getting clicked.

try this,whether self button is triggering or not..
[self.myButton sendActionsForControlEvents:UIControlEventTouchUpInside];
It worked for me

You should add your button to a super view.And also,the super view should enable user interfaces.
[yourSuperView addSubView:button];yourSuperView.userInteractionEnabled=YES;

How about you change your return type of your 'hideWebViewBtn' method from 'IBAction' to 'void'?

"Once i click on the button its not getting clicked." You mean that u click the close button,but it doesn't call the function hideWebViewBtn:() ?

You can try this.
UIButton *imageButton = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width-100, 10, 50, 50)];
[imageButton setImage:[UIImage imageNamed:#"close.png"] forState:UIControlStateNormal];
[self.view addSubview:imageButton];
[self.view bringSubviewToFront:imageButton];
[imageButton removeTarget:self action:NULL forControlEvents:UIControlEventAllEvents];
[imageButton addTarget:self action:#selector(hideWebViewBtn:) forControlEvents:UIControlEventTouchUpInside];
Hope it works for you. Thanks!

Related

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;
}

UIButton programatically is not working in iOS

When I click on the UIButton nothing happened
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create button like below.
UIButton *checkbox = [UIButton buttonWithType:UIButtonTypeCustom]; // this is important
checkbox.frame = CGRectMake(73, 324, 15, 15)];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
You have to create the button first using button property and then give its frame.
If still its not working then self.background addSubview should be self.view addSubview.
What is self.background?
Your code is perfectly fine. You need to check the following .
check whether self.background is placed properly in your view heirarchy, I tried your code with self.view instead of self.background like below and it works perfectly.
self.checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 50, 50)];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox1.png"] forState:UIControlStateNormal];
[self.checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox2.png"] forState:UIControlStateSelected];
[self.checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.checkbox];
From the error message that you have given
"
Warning: Attempt to present <enter_the_den: 0x7fbf1bd4c4e0> on <ViewController: 0x7fbf1bc771c0> whose view is not in the window hierarchy!
I suspect the issue is in your code inside the method autologin, just comment all the code in your method and just try a log , it should work.
refer this : whose view is not in the window hierarchy
If you read the Apple Documentation for UIButton and look under the heading marked as Creating Button you'll find that the way to create a UIButton is to use buttonWithType: and not initWithFrame:.
Return Value
A newly created button.
Discussion
This method is a convenience constructor for creating button objects with specific configurations. If you subclass UIButton, this method does not return an instance of your subclass. If you want to create an instance of a specific subclass, you must alloc/init the button directly.
When creating a custom button—that is a button with the type UIButtonTypeCustom—the frame of the button is set to (0, 0, 0, 0) initially. Before adding the button to your interface, you should update the frame to a more appropriate value.
The only time when you should use alloc/init with a UIButton is when you have subclassed UIButton however your init method should then implement the buttonWithType: and pass in UIButtonTypeCustom.
So change:
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(73, 324, 15, 15)];
To
// Create a button of type custom
checkbox = [UIButton buttoWithType:UIButtonTypeCustom];
// Now set the frame for your button
[checkbox setFrame:CGRectMake(73, 324, 15, 15)];
// Continue with the rest of your button code
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox.png"] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:#"checkbox-checked.png"] forState:UIControlStateSelected];
[checkbox addTarget:self action:#selector(autologin) forControlEvents:UIControlEventTouchUpInside];
[self.background addSubview:checkbox];
Check userInteractionEnabled property of your background property.
But i see why you have no feedback on button if rest is fine - you use UIControlStateSelected for setting image when button has touch, but you must use UIControlStateHighlighted

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.

IBAction & Buttons programmatically

I'm creating buttons programmatically and I then want to add functionality whereby when they are tapped on/pressed, they stay highlighted unless tapped on again. What I'm doing now is creating the buttons and then trying to add an IBAction. However, the issue is I have my button creation in a method and then I'm not sure how to reference the button in my IBAction. Here's my code:
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender{
//Not sure what to do here, since this method doesn't recognize testButn, How do I reference testButn
The sender is testButn. You should change stayPressed's argument type from (id) to (UIButton *)
Unless an action method is connected to several different classes of objects, it's best to replace the id with whatever object class you're using. This can be useful if you're hooking things up in IB, since it won't let you hook it to the wrong kind of object.
The fact that it's not working isn't because it doesn't recognize your button. Your approach is wrong. I think you need to have an action connected to touch down, and maybe set the selected state to YES. In your button definition, you need to set an imageForState: for the selected state. The way you're doing it now, that method isn't called until touch up.
Something like this:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"New_PICT0019.jpg"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"New_PICT0002.jpg"] forState:UIControlStateSelected];
[testButn addTarget:self action:#selector(stayPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:testButn];
}
-(void)stayPressed:(UIButton *) sender {
if (sender.selected == YES) {
sender.selected = NO;
}else{
sender.selected = YES;
}
}
You need to cast sender to UIButton.
- (IBAction)staypressed:(id)sender
{
UIButton *theButton = (UIButton*)sender;
//do something to theButton
}
UIButton* testButn = [UIButton buttonWithType:UIButtonTypeCustom];
[testButn setFrame:CGRectMake(0, 135, 40, 38)];
[testButn setImage:[UIImage imageNamed:#"test_butn_un.png"] forState:UIControlStateNormal];
[testButn setImage:[UIImage imageNamed:#"test_butn_pressed.png"] forState:UIControlStateHighlighted];
[testButn addTarget:self action:#selector(staypressed:) forControlEvents:UIControlEventTouchUpInside];
testButn.tag = 1;
[self.contentview addSubview:testButn
-(IBAction)staypressed:(id)sender
{
if ([sender tag]==1)
{
somecodes...
}
}

Why I can't see my UIBUTTON on my iPad but I see my UIBUTTON on the simulator?

Introduction:
I'm creating my UIBUTTON at runtime. And my UIButton is in a TableView. The whole function of this UIButton is: If I click on "delete OFF" or "delete ON" (normal void Method) I can delete each cell in my TableView. I took the position of my UIButton on the right side like the real delete-method.
First of all, I know that Objective-C has got a delete-method! But I cant use this method, because I have Sliders in my TableView and sliders with Objective-C delete method doesn't work together.
Because of this, I have written this Code:
- (UITableViewCell *) getCellContentView:(NSString *)cellIdentifier {
if (allowchange==YES) //<- Button to activate the delete mode
{
UIButton *playButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
playButton.frame = CGRectMake(700, 23, 45, 25);
[playButton setBackgroundImage:[UIImage imageNamed:#"button_delete.png"]
forState:UIControlStateNormal];
[self.view addSubview:playButton];
[playButton addTarget:self action:#selector(deleteRow:) forControlEvents:UIControlEventTouchDown];
playButton.tag=count;
[cell.contentView addSubview:playButton];
[playButton release];}}

Resources