Problems in uiview hide and show - ios

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

Related

UIButton Click Not working when its triggered from static library

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!

Creating a UIButton in a subclass of UIButton

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

show and remove same button in iOS

I have one UIButton. Now I add button on Subview :-
UIButton* button4 = [[UIButton alloc] initWithFrame:frame4];
[button4 setBackgroundImage:image3 forState:UIControlStateNormal];
[button4 setShowsTouchWhenHighlighted:YES];
[button4 addTarget:self action:#selector(Nofication:) forControlEvents:UIControlEventTouchDown];
- (void)Nofication:(id)sender
{
share=[[UIView alloc]init];
share.frame=CGRectMake(300, 60, 130, 100);
[self.view addSubview:share];
[UIView animateWithDuration:2.0
animations:^{
share.frame =CGRectMake(180, 50, 130, 100); // its final location
}];
share.backgroundColor=[UIColor yellowColor];
login=[[UIButton alloc]init];
login.frame=CGRectMake(0, 0, 130, 50);
[login setBackgroundImage:[UIImage imageNamed:#"login.png"] forState:UIControlStateNormal];
[login addTarget:self action:#selector(asubmit:) forControlEvents:UIControlEventTouchDown];
[share addSubview:login];
login.layer.borderColor = [[UIColor whiteColor]CGColor];
policies=[[UIButton alloc]init];
policies.frame=CGRectMake(0, 50, 130, 50);
[policies setBackgroundImage:[UIImage imageNamed:#"Policies.png"] forState:UIControlStateNormal];
[policies addTarget:self action:#selector(apolicies:) forControlEvents:UIControlEventTouchDown];
[share addSubview:policies];
}
When I click button it's open subview working nice. But now I need if subview is showing in that time click button it's need to hidden subview. If subview is not showing in that time when I click button it's need to show subview. So Please give me any idea about it.
How to Show and Hidden same UIButton
thanks in Advance .
There is a hidden property on UIView. https://developer.apple.com/library/ios/documentation/uikit/reference/uiview_class/UIView/UIView.html#//apple_ref/occ/instp/UIView/hidden
if (button.isHidden) {
button.hidden = NO;
}
Is it what asked?
Where subview is the view (or UIButton) you are trying to hide if shown, and show if hidden.
[subview setHidden:![subview isHidden]];
Edit :
In your Notification function :
// Be sure that the view is loaded, maybe load it at a different place or use a boolean
if([subview isHidden]){
[subview setHidden:false];
}else{
[subview setHidden:true];
}
You can set button4.selected = !button4.selected in Nofication: method.
Then you can check button4.isSelected and hide/show subview with hidden property
Hope it helps.

Custom UIButton in UIscrollview

I am using xcode 4.6 to develop an app. Here i want to add UIButton programmatically to UIscrollview. This is the code i follow.
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Now the problem is that Button gets disappeared (technically its textcolor becomes white) on click. I checked keeping UIscrollview color to red that th button was still in the view but i cant get the reason why its text color changed and how do i undo dis.
Basically I wan to create a clickable link using UIbutton.
I know uitextview approach (datadetectortype) but its of no use as i want to show different text in the label for the link and the actual link.
Note: The textcolor doesnt change back to blue and remains white only.
Thanks in advance.
Try the below code
UIButton *bt =[UIButton buttonWithType:UIButtonTypeCustom];
bt.frame = CGRectMake(50.0, 50.0, 100.0, 50.0);
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
bt.backgroundColor = [UIColor grayColor];
bt.titleLabel.textColor=[UIColor blueColor];
[self.scrollTest addSubview:bt];
-(void)userTappedOnLink:(UIButton*)sender
{
NSLog(#"Test ..");
[self performSelector:#selector(changeBtnTextColor:) withObject:sender afterDelay:1.0];
}
-(void)changeBtnTextColor:(UIButton*)btn
{
btn.titleLabel.textColor=[UIColor blueColor];
}
hope it will work for you.
use below code you will get your solution
UIButton *bt =[[UIButton alloc]initWithFrame:frame];
bt=[UIButton buttonWithType:UIButtonTypeCustom];
[bt setTitle:#"Custom Button" forState:UIControlStateNormal];
[bt addTarget:self action:#selector(userTappedOnLink:) forControlEvents:UIControlEventTouchUpInside];
[bt setBackgroundColor:[UIColor grayColor]];
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[self.mainscrollview addSubview:bt];
[self.mainscrollview bringSubviewToFront:bt];
Use the below code:
[bt setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
Problem is you are set Title to titleLabel and you are changing textColor of buttonTitle color.
All the best !!!
check Button's Fram .. is it valid or not ??
and remove bt=[UIButton buttonWithType:UIButtonTypeCustom]; line from your code.

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

Resources