show and remove same button in iOS - 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.

Related

How to set subview as a first responder?

I am trying to close the image View while clicking on close button.but button does not responds to touch event.
here is my code:
-(void)imageTouchUP:(id)sender
{
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.image=self.testimonialImage;
imageView.tag=1;
UIButton *closeButton=[UIButton buttonWithType:UIButtonTypeCustom];
closeButton.frame=CGRectMake(0, 0, 20, 20);
[closeButton setImage:[UIImage imageNamed:#"close.jpg"] forState:UIControlStateNormal];
[closeButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[imageView addSubview:closeButton];
[self.view addSubview:imageView];
}
-(void)buttonClicked:(id)sender
{
UIView *subView=[self.view viewWithTag:1];
[subView removeFromSuperview];
}
For the UIImageView to handle touches you need to set userInteractionEnabled to YES, it defaults to NO.
Try:
[imageView setUserInteractionEnabled:YES];
Reference
There is 1 property of UIImageView userInteractionEnabled and its value is by default NO. So in your code add
UIImageView *imageView=[[UIImageView alloc]initWithFrame:CGRectInset(self.view.bounds, 30 , 30)];
imageView.userInteractionEnabled = YES;
Maybe there's another view with tag=1 in your self.view. Please try to edit button clicked like this:
- (void) buttonClicked:(UIButton *)btn
{
UIView *view = btn.superview;
if ([view isKindOfClass:[UIImageView class]]) {
[view removeFromSuperview];
}
}

Why the sender not called when I click the image of the button in Objective-C?

I am new to IOS and Objective-C , and I try to set Image for a UIButton.
Wen I click the button , the button will change image1. If I click the button one again , it will change to image2
First, I set the image in Viewdidload like the following code.
#interface AITPreviewViewController ()
{
VLCMediaPlayer *mediaPlayer ;
BOOL recording ;
BOOL recordMode;
}
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
When I click the button , the button will change the image. The code is like the following:
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
if (recordMode == YES) {
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateNormal];
recordMode = NO;
}else if (recordMode == NO){
NSLog(#"modeButtonClick..%hhd = " , recordMode);
[self.modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
recordMode = YES;
}
}
When I click the image on the button , the image of the button become dark in color. It seems the button has been press. But the - (IBAction)modeButtonClick:(id)sender didn't called.
When I click the blank space (the side of image) , the button has been press. The - (IBAction)modeButtonClick:(id)sender has been called. And the image also change to other image.
I am sure I have set action in header file and .m file.
The above describe is like the following picture.
It seems the button is cover by image , and the image and the button is not the same.
Why this happened ??
How to solve this problem ?
Thanks in advance.
Couple of changes that you could implement:
First:
Change the setBackgroundImage to setImage.
For example: [self.modeChangeButton setImage: [UIImage imageNamed: #"recordmode.png" forControlState: UIControlStateNormal];
That way, if you have set an image to the button by mistake, this will overwrite it.
Second:
Make sure you have connected your reference outlets (IBOutlet) and actions (IBActions) in Interface builder - whether XIB files, or storyboard. If you don't link the the interface objects there to those in your code, they won't work.
Third:
If it still does not work, try adding this line to your viewDidLoad
[self.modeChangeButton addTarget:self #selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
Fourth:
Change this line in your viewDidLoad:
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
to:
self.modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
This should solve your problem.
This code is adding a new button on top of your self.modeChangeButton which I'm assuming is coming from a xib or storyboard.
- (void)viewDidLoad
{
recordMode = YES;
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[self.view addSubview:modeChangeButton];
}
Do this instead...
- (void)viewDidLoad {
self.recordMode = YES;
}
- (void)setRecordMode:(BOOL)recordMode {
_recordMode = recordMode;
UIImage *imageForMode = recordMode ? [UIImage imageNamed:#"photomode.png"] : [UIImage imageNamed:#"recordmode.png"];
[self.modeChangeButton setBackgroundImage:imageForMode forState:UIControlStateNormal];
}
- (IBAction)modeButtonClick:(id)sender {
self.recordMode = !self.recordMode;
}
Check your button size and make the frame size closer to the image size. And try to maintain the two images as like below.
- (void)viewDidLoad
{
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 450, 60, 60)];
[modeChangeButton setImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
[modeChangeButton setImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender
{
sender.selected = !sender.selected;
}
Try modifying you viewDidLoad method as follows.
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.PNG"] forState:UIControlStateNormal];
// You might need to add following two lines into your code.
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
Above solution should work. As I see, you have referencing property named modeChangeButton in your modeButtonClick: method. But you are not assigning the created button into your property. Also you have to set the action selector to your button.
As a improvement, you can do this to improve performance of your application. Without setting the background image on click event, you can select/deselect the button. With this you don't need to maintain boolean flags, which may introduce new bugs to your application.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *modeChangeButton = [[UIButton alloc] initWithFrame:CGRectMake(200, 300, 60, 60)];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"recordmode.png"] forState:UIControlStateNormal];
[modeChangeButton setBackgroundImage:[UIImage imageNamed:#"photomode.png"] forState:UIControlStateSelected];
[modeChangeButton addTarget:self action:#selector(modeButtonClick:) forControlEvents:UIControlEventTouchUpInside];
self.modeChangeButton = modeChangeButton;
[self.view addSubview:modeChangeButton];
}
- (IBAction)modeButtonClick:(id)sender {
NSLog(#"modeButtonClick..!!!!!!");
[self.modeChangeButton setSelected:![self.modeChangeButton isSelected]];
}
Hope this helps.

adding multiple subviews in ios

I'm trying to add multiple subview programmatically, but my buttons inside those subviews are not working. The buttons were also added as subviews programmatically.
Here's the code, hopefully will explain more about what I mean.
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
[self setBackgroundColor:[UIColor clearColor]];
// UIView container of selected image with comment button.
UIView *containerOfSelectedImage = [[UIView alloc] initWithFrame:CGRectMake(0, 20, 0, 350)];
NSLog(#"%f", self.frame.size.width);
// image view of selected photo by user.
UIImageView *userImage = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"nature_01.jpg"]];
[userImage setFrame :CGRectMake(0, 0, 340, 300)];
// comment button with action of recodering user's comment.
UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
//[commentButton setBackgroundColor : [UIColor clearColor]];
float y = userImage.frame.size.height + 5.0f;
[commentButton setFrame : CGRectMake(32.0f, y, 24.0f, 24.0f)];
[commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown];
[commentButton setImage:[UIImage imageNamed:#"comments-24.png"] forState:UIControlStateNormal];
[containerOfSelectedImage addSubview:userImage];
[containerOfSelectedImage addSubview:commentButton];
[self addSubview:containerOfSelectedImage];
[self addSubView:self.commentContainerView];
return self;
You need to increase the frame of the containerOfSelectedImage. Currently the width is 0.
The containerOfSelectedImage's frame has to be big enough so the button fits inside it. If the button is outside that frame it will show up but you can't touch him.
To debug this issues you can use a tool like Reveal App. If any of your buttons is outside the frame of the parent then the touch will not be detected.
Change [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchDown]; to [commentButton addTarget:self action:#selector(audioRecordAction) forControlEvents:UIControlEventTouchUpInside];

Adding a close button to the modal view that is partially off the view

I am trying to add a "X" button to the top of the modal view so if the user presses that it closes the modal view. I looked at the solution proposed here how to add close button to modal view corner which is presented in UIModalPresentationPageSheet?
But I have two follow up questions here as i am fairly new to ios programming
The solution proposed in the above page does not seem to work for me. I tried it with a normal button first in the viewDidLoad of the modal VC and I am seeing it appear only within the bounds of modal view and not outside as I want.
UIView *buttonView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
buttonView.backgroundColor = [UIColor brownColor];
CGRect buttonFrame = CGRectMake( 0, 0, 100, 50 );
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
[button setTitle: #"Close" forState: UIControlStateNormal];
[button setTitleColor: [UIColor redColor] forState: UIControlStateNormal];
[button addTarget:self
action:#selector(closeButtonPressed)
forControlEvents:UIControlEventTouchUpInside];
[buttonView addSubview: button];
CGRect parentView = self.view.superview.frame;
CGRect currentView = self.view.frame;
CGRect buttonViewFrame = buttonView.frame;
buttonViewFrame.origin = CGPointMake(parentView.origin.x - buttonViewFrame.size.width/2.0f, parentView.origin.y - buttonViewFrame.size.height/2.0f);
[buttonView setFrame:buttonViewFrame];
[self.view addSubview:buttonView];
what I am seeing is
How do I draw the "X" button do I use CGRect to draw it our or instead of adding a button in the above example should I just add a image with the "X" in it as the subview?
thanks in advance for the help
do like this, hope helps u :)
//adding button to your view
UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(100, 200, 300, 350)];
aView.backgroundColor = [UIColor blueColor];
aView.tag = 100;
[self.view addSubview:aView];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
[aButton setTitle:#"CLOSE" forState:UIControlStateNormal];
[aButton addTarget:self action:#selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
aButton.frame = CGRectMake(aView.bounds.origin.x, aView.bounds.origin.y, 80, 30);// adjust with respect to bounds
aButton.backgroundColor = [UIColor redColor];
[aView addSubview:aButton];
[aView release]; //i am doing without ARC
The other option is to create a larger view and put your UITableView within this, along with your close button. The close button can then be at position (0,0) and the UITableView can start at (25,25).
This prevents complications - I have seen clipped views fail to recognise the touch outside of the main view, so your 50x50 button only has a touch area of 25x25.

Adding Button from loop into UIScrollView

i am writing this code to create buttons from loop and adding them into UIScrollView but somehow its not working , please can someone help.
the code is
scroll = [[UIScrollView alloc]init];
[self createurdulistfordisplay];
for(int i = 1;i<[urdulist count];i++)
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage:[UIImage imageNamed:#"2.png"] forState:UIControlStateNormal];
[button addTarget:self
action:#selector(playurduaudio:)
forControlEvents:UIControlEventTouchDown];
button.frame = CGRectMake(80.0, 210.0 + (30*i), 213.0, 46.0);
[scroll addSubview:button];
}
I think you have forgotten to add scroll to view. add like his at last line
[self.view addSubView:scroll];
Here you have created scroll but not added to your view that means until you adds this scrollView to main view i.e. to self.view ,you will not get it display on screen

Resources