Removing subView from SuperView [duplicate] - iphone

I have added a few buttons to self.view. When the user clicks on one button, i will load another view (subView). My code where i am loading the subView is shown below
subView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.contentSize.width, self.scrollView.contentSize.height)];
subView.tag=1;
// and then i add various other components to it
[self.view addSubview:subView]; // and finally add it to the view
When the user touches this view, i detect it and remove the view (i have already done the detection part).
This is how i am removing the view
UIView *v = [self.subView viewWithTag:1];
v.hidden = YES;
[v endEditing:YES];
[v removeFromSuperview];
The view dissapears, but i can't click on the buttons or anything that was on the self.view. When i hold the mouse cursor on a button for awhile i could click it otherwise i can't. How can i solve this ?

Try removing the subview like this:
for (UIView *subView in self.view.subviews) {
if (subView.tag == (int)yourSubViewTag) {
[subView removeFromSuperview];
}
}

Should this line:
UIView *v = [self.subView viewWithTag:1];
really be:
UIView *v = [self.view viewWithTag:1];
?

I think your problem is that you do
[self.subView viewWithTag:1];
but the view you want to remove is "owned" by self.view so it should be
[self.view viewWithTag:1];
But if you have a reference to subView, why search it again? Why not immediately:
self.subView.hidden = YES;
[self.subView endEditing:YES];
[self.subView removeFromSuperview];
Btw you might want to look into your memory management, I see you alloc your subView but I don't see it being released, when the view is removed again. Maybe you do and you just don't show the code, in that case forget my comment.

Related

objective c How to set UserInteractionEnabled:NO for the entire view except on UIButton in that view

In my application i have a view
in that i have many UIElements, buttons, etc..
I need to set
UserInteractionEnabled:NO for all the ui elements in that view except one button.
I tried with
[self.view setUserInteractionEnabled:NO];
The Require button also the subview that self.view that button also apply same behavior.
I can able to apply individually but it is not a good way.
How should i set UserInteractionEnabled:NO for all the other ui elements except one button
for (UIView *view in [self.view subviews])
{
if (view.tag==101)
[ view setUserInteractionEnabled:YES];
else
[ view setUserInteractionEnabled:NO];
}
Check this out:
for (UIView *view in self.view.subviews) {
if (!([view class]==[UIButton class]) )
{
view.userInteractionEnabled = NO;
}
}
You can just add transparent subview in the front of your view and place button on this transparent view:
UIView* maskView = [[UIView alloc] initWithFrame:self.view.bounds];
maskView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:maskView];
[maskView addSubview:buttonView];
And be sure that this transparent view is the last added subview, or just push it to the front of view in viewWillAppear:
[self.view bringSubviewToFront:maskView];

Deleting controls programmatically

I want to remove a UITextField/UIButton/UILabel added to a view on the click of button. How can this be achieved in iOS?
To remove you can simply use:
[myTextField removeFromSuperview];
try this one...
Blockquote
UILabel *t1;
NSArray *arr1=[YourView subviews];
for (UIView *v in arr1)
{
if([v isKindOfClass:[UILabel class]])
{
t1 = (UILabel *)v;
if (t1.tag==your_tag)
[t1 removeFromSuperview];
}
}
Blockquote
You can use [myTextField removeFromSuperview]; to get rid of a view.
[myTextField removeFromSuperview]
See UIView documentation.
All controls inherited from UIView.
So you can remove all the controls which added on UIView using like this. By clicking the button action write like this
for(UIView *control in [self.view subviews])
{
[control removeFromSuperview];
}

Removing view from its superview

I have added a few buttons to self.view. When the user clicks on one button, i will load another view (subView). My code where i am loading the subView is shown below
subView = [[UIView alloc] initWithFrame:CGRectMake(0,0,self.scrollView.contentSize.width, self.scrollView.contentSize.height)];
subView.tag=1;
// and then i add various other components to it
[self.view addSubview:subView]; // and finally add it to the view
When the user touches this view, i detect it and remove the view (i have already done the detection part).
This is how i am removing the view
UIView *v = [self.subView viewWithTag:1];
v.hidden = YES;
[v endEditing:YES];
[v removeFromSuperview];
The view dissapears, but i can't click on the buttons or anything that was on the self.view. When i hold the mouse cursor on a button for awhile i could click it otherwise i can't. How can i solve this ?
Try removing the subview like this:
for (UIView *subView in self.view.subviews) {
if (subView.tag == (int)yourSubViewTag) {
[subView removeFromSuperview];
}
}
Should this line:
UIView *v = [self.subView viewWithTag:1];
really be:
UIView *v = [self.view viewWithTag:1];
?
I think your problem is that you do
[self.subView viewWithTag:1];
but the view you want to remove is "owned" by self.view so it should be
[self.view viewWithTag:1];
But if you have a reference to subView, why search it again? Why not immediately:
self.subView.hidden = YES;
[self.subView endEditing:YES];
[self.subView removeFromSuperview];
Btw you might want to look into your memory management, I see you alloc your subView but I don't see it being released, when the view is removed again. Maybe you do and you just don't show the code, in that case forget my comment.

How to set touch events in subviews?

i have a main view controller with two subview in it,one is note-view and other one is share-view . When i click the cell of the tableview that is in the main-view controller ,it popups note-view,there is button in the note-view ,if the user tap the button it pop sup a another subview ,the share-view .But i need to close the share view when the user tap the note-view.Is that possible to implement a touch event in a subview ?
I have done this coding but result is error
in viewdidload
NotesView *label =[[NotesView alloc]init];
UITapGestureRecognizer *tapGesture =
[[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(NoteTap)] autorelease];
[label addGestureRecognizer:tapGesture];
[tapGesture release];
then
-(void)NoteTap
{
UIView *langview = (UIView *)[self.view viewWithTag:120];
//ViewWithTag Number should be same as used while allocating
[langview removeFromSuperview];
}
Inside touchesBegan: of the view controller, you can check to see if a touch event takes places within the frame of the subview like so.
CGPoint touchPoint = [touch locationInView:self];
if (CGRectContainsPoint(label.frame, touchPoint)) {
UIView *langview = (UIView *)[self.view viewWithTag:120];
[langview removeFromSuperview];
}

scrollview's subview does not get touch events when scroll view scrolls

I have a scrollView onto which some imageViews are added.
I want that when scrollView scrolls then also the imageViews should get touch events.
But it is not happening in my case.
Only one of the two things happen at a time.
Either the scrollView scrolls but the imageViews do not get touch events
or the imageViews get touch events and the scrollView does not get scrolled.
Can't both of the things happen simultaneously.
Please suggest.
You can use "Tap Gesture Recognizer" to get the events on your images while it's being added to the scroll view.
EDIT
You can refer to this code:
-(void)yourGestureRecognizer
{
UITapGestureRecognizer *yourTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTap:)];
[yourImageView addGestureRecognizer:yourGestureRecognizer];
[yourGestureRecognizer release];
}
You can handle tap here:
-(void)handleTap:(UIGestureRecognizer *)sender{
UIImageView *imageView = (UIImageView *)sender.view;
switch (imageView.tag)
{
case 1:
m_pYourInstance=[[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self.navigationController pushViewController:XXXXX animated:YES];
break;
}
Hope this helps !!
I know this was posted a while ago, but...
By default, UIImageView objects have their property userInteractionEnabled set to NO. This could be why touches aren't registering. This has tripped me up on more than one occasion and it's one of those little things that only experience will help.
Set it to YES and see if that fixes it.
Use UIGestureRecognizer. Add the recognizer to your image views.
And if you are targeting some old iOS version, have a look at this link.
yes you can do both. you need to customize your uiimageview by sub classing it and have to add Tap Gesture Recognizer like :
in .h file
#interface CustomView : UIImageView
{
// your variables
}
in .m file
//when you create an instants of your image view UITapGestureRecognizer will added in all your instants.
- (id)initWithFrame:(CGRect)frame
{
if ((self = [super initWithFrame:frame]))
{
// add Gesture for tapping
UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:#selector(tap:)];
tap.numberOfTapsRequired = 1;
[self addGestureRecognizer:tap];
}
return self;
}
then add delegate methods in .m file too
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UITouch *touch = [touches anyObject];
}
now in your custom cell, add your custom image view with Tap Gesture Recognizer
hope it will help you...
Use UIButtons with background images set on them. Here's the loop you use to layout UIScrollView subviews:
for (int i = 0; i < [images count]; i++) {
CGRect aFrame = ...;
UIButton *button = [[[UIButton alloc] initWithFrame:aFrame] autorelease];
[button setImage:[images objectAtIndex:i] forState:UIControlStateNormal];
button.tag = i;
[button addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
}
And here you process the touch event:
- (void)buttonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
[self performActionWithImageAtIndex:[images objectAtIndex:button.tag]];
}
That will omit the need to create additional gesture recognizers and will lower the amount of boilerplate work.
-- edit
The other reason your UIImageViews are not detecting the touches is because you create them in code and forget to set userInteractionEnabled to YES.