How to hide my tableView which is declared in another class..
Here is my code snippet,
CRStoreView.h
#interface CRStoreView : UIView <UITableViewDelegate, UITableViewDataSource>{
....
}
#property (strong, nonatomic) IBOutlet UITableView *tblStore;
and i want to hide this tblStore in my new class(CRNextView.m)..
I tried this but table is not getting hide,
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
NSLog(#"touchesBegan");
CRStoreView *Obj = [[CRStoreView alloc] init];
[Obj.tblStore setHidden:YES];
}
How to Solve it ?
One method is to use delegates. Make CRStoreView a delegate of the CRNextView and call the setHidden method from the CRNextView on the delegate. Or you could pass the current instance of the CRStoreView to CRNextView and access the tableView object.
I have a UIControl which implements the touches began method like so:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
//More code goes here
This subclass of UIControl is instantiated in a view controller, it is then added as a subview to that view controller. I have a breakpoint at the touches began method of the UIControl, and the method never gets called. I've been doing some reading and it seems that the View Controller has some logic that decides whether to pass on touch events to its subviews. The strange thing is that I have a different subclass of UIControl in the same view controller, and the touch events get passed down to it when the user touches it!
Here is the full code:
.h
#import <UIKit/UIKit.h>
#interface CustomSegment : UIView
#property (nonatomic, strong) UIImageView *bgImageView;
#property (nonatomic, assign) NSInteger segments;
#property (nonatomic, strong) NSArray *touchDownImages;
#property (nonatomic, readonly, assign) NSInteger selectedIndex;
#property (nonatomic, weak) id delegate;
- (id)initWithPoint:(CGPoint)point numberOfSegments:(NSInteger)_segments andTouchDownImages:(NSArray *)_touchDownImages;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
#end
.m
#import "CustomSegment.h"
#implementation CustomSegment
#synthesize bgImageView, segments, touchDownImages, selectedIndex, delegate;
- (id)initWithPoint:(CGPoint)point
numberOfSegments:(NSInteger)_segments
andTouchDownImages:(NSArray *)_touchDownImages
{
self = [super initWithFrame:CGRectMake(point.x, point.y, [[_touchDownImages objectAtIndex:0] size].width, [[touchDownImages objectAtIndex:0] size].height)];
if (self)
{
touchDownImages = _touchDownImages;
segments = _segments;
bgImageView = [[UIImageView alloc] initWithImage:[touchDownImages objectAtIndex:0]];
[self addSubview:bgImageView];
}
return self;
}
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//[super touchesBegan:touches withEvent:event];
UITouch *touch = [touches anyObject];
float widthOfSegment = [self frame].size.width / segments;
float bottomPoint = 0;
float topPoint = widthOfSegment;
for (int i = 0; i < segments; i++)
{
if ([touch locationInView:self].x > bottomPoint && [touch locationInView:self].x < topPoint)
{
[bgImageView setImage:[touchDownImages objectAtIndex:i]];
selectedIndex = i;
return;
}
else
{
bottomPoint = topPoint;
topPoint += topPoint;
}
}
}
#end
tl;dr Set all subviews of the UIControl to setUserInteractionEnabled:NO. UIImageViews have it set to NO by default.
Original Post
One thing I found recently is that it helps if the top-most subview of the UIControl has setUserInteractionEnabled:NO. I arrived at this because I had a UIControl subclass with a UIImageView as it's only subview and it worked fine. UIImageView has userInteractionEnabled set to NO by default.
I also had another UIControl with a UIView as it's top most subview (technically the same UIControl in a different state). I believe UIView defaults to userInteractionEnabled == YES, which precluded the events being handled by the UIControl. Settings the UIView's userInteractionEnabled to NO solved my issue.
I don't know if it's the same issue here, but maybe that will help?
--
Edit: When I say topmost view... probably set all subviews of the UIControl to setUserInteractionEnabled:NO
Check frames of all parent views. The rule is that if sub-view (or its part) of the view is outside the view bounds, it doesn't receive touch events.
Xcode 12 and Latter.
As mention in the accepted answer, make sure all the subviews of the
UIControl view get "User Interaction Enabled" unchecked.
Select your
UIControl view and switch to the "Connection Inspector" and make
sure it has been connected to "Touch Up Inside" event. Sometimes
Xcode uses "Value Changed" event so make sure to change to "Touch
Up Inside" event
It is possible that another view is covering your UIControl, and preventing it from receiving the touch events. Another possibility is that userInteractionEnabled is set to NO somewhere by mistake.
EDIT: I see that you added more code above. Did you verify that your view's frame has a width and height greater than zero? It looks to me like you are calling "size" directly on an object from NSArray (which is 'id'). I don't know how you are doing this without a cast (perhaps the parenthesis didn't come through above?) but if you are somehow pulling it off I wouldn't be surprised if it was an invalid value.
First, why do you call [super touchesBegan:touches withEvent:event];? This call is forwarding the event to the next responder. Usually you do it only when you don't want to handle the event. Are you sure you know what are you doing there?
An idea why it doesn't work - can it be that you have a gesture recognizer which handles the event first?
Anyway, do you really need to override touchesBegan? UIControl is made to track the touch events by itself and call your handlers in response. And the UIControl docs say HOW to subclass it.
Subclassing Notes
You may want to extend a UIControl subclass for either of two reasons:
To observe or modify the dispatch of action messages to targets for particular events
To do this, override sendAction:to:forEvent:, evaluate the passed-in selector, target object, or UIControlEvents bit mask, and proceed as required.
To provide custom tracking behavior (for example, to change the highlight appearance)
To do this, override one or all of the following methods: beginTrackingWithTouch:withEvent:, continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:.
Possibilities:
You might have forgot to set the delegate for the UIControl.
The other UIControl which receives the touch is obscuring/covering over the UIControl.
Fair enough. Recently re-investigated this (UIControl is very poorly documented) and realised that tracking touches is a replacement for touchesBegan/Ended, not an additional, so sorry about that. I'd reverse the vote but it wouldn't let me :(
You might need to override intrinsicContentSize inside your UIControl subclass.
override var intrinsicContentSize: CGSize {
return CGSize(width: 150, height: 100)
}
I don't quite understand how it fixes it or why, but it works. It doesn't even need to be the exact size as your control.
UIView instances are unable to respond to events so there is no way, up to your current configuration, to trigger your action method ?
Try to change the class of the view to be an instance of the UIControl class !
I try to subclass the UScrollview but it ends up losing the default UIScrollview delegate method.
#import <UIKit/UIKit.h>
#protocol myscrollviewDelegate <NSObject>
-(void) myscrollview_return;
#end
#interface myscrollview : UIScrollView <UIScrollViewDelegate> {
id<myscrollviewDelegate> delegate;
}
#property(nonatomic, assign) id<myscrollviewDelegate> delegate;
#end
(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
never get called when scroll.
what's wrong? Can I subclass the UIScrollview and add additonal delegate and at the same times keeping the original delegates??
You are not adding a property, but overriding it, as UIScrollView already has a delegate property. When you set a delegate using the new property, the reference will be stored in the instance variable you added, not in the private instance variable of the original UIScrollView.
My theory is that the implementation of UIScrollView accesses the instance variable without using the property. I haven't verified it, but try not adding a new ivar and overriding the delegate property.
You can do this without creating a second delegate property.
First, make your delegate protocol inherit from UIScrollViewDelegate:
#protocol myscrollviewDelegate <NSObject, UIScrollViewDelegate>
Then, declare the delegate property in your header for your class:
#interface myscrollview : UIScrollView <UIScrollViewDelegate>
#property(nonatomic, assign) id<myscrollviewDelegate> delegate;
And the key is to not synthesize the property, but rather make it dynamic in your implementation file.
#implementation myscrollview
#dynamic delegate;
...
This is because You implement the delegate methods with id id delegate; I hope so
so change the name of delegate. instead using delegate use other name like "delegateSomeClass" etc
Now the delegates method of UIscrollView calls
hope it will clear :)
I would like to be able to paint on top of my subviews, or in other words: have an overlay that does not block the events. So far I discovered:
- any instructions in drawRect are painted below subviews,
- putting a transparent UIView on top blocks events.
Is there another trick I can try?
Use a transparent UIView on top, and in IB uncheck "User Interaction Enabled" for that view, then input events will go down to your controls beneath it.
Or, in code do:
UIView *overlayView = [[UIView alloc] init...];
overlayView.userInteractionEnabled = NO;
To solve this you want to forward the hitTest events. Add the code below to your project, add a UIImageView to your interface, set its Class to ClickThroughImageView and connect the "onTopOf" outlet to whatever UIView is below the image.
The ClickThroughImageView.h file:
#import <Foundation/Foundation.h>
#interface ClickThroughImageView : UIImageView
{
IBOutlet UIView *onTopOf;
}
#property (nonatomic, retain) UIView *onTopOf;
#end
The ClickThroughImageView.m file
#import "ClickThroughImageView.h"
#implementation ClickThroughImageView : UIImageView
#synthesize onTopOf;
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
return [onTopOf hitTest:point withEvent:event];
}
#end
I'm having trouble getting my SubViews properly initialized using Interface Builder. I have the following View hierarchy (UIWindow -> BlankCanvasView -> StalkerView). BlankCanvasView is a subclass of UIView and StalkerView is a IBOutlet of BlankCanvasView
#interface BlankCanvasView : UIView {
IBOutlet UIView *stalker;
}
#end
I've established a connection between the stalker outlet of BlankCanvasView and the subview. However, in my touchesBegin method of BlankCanvasView the stalker outlet is nil. See below for touchesBegin.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Touch Begin detected!!!");
NSLog(#"Stalker instance %#", stalker);
[UIView beginAnimations:#"StalkerAnimation" context:nil];
UITouch *touch = [touches anyObject];
//stalker is nil here!!!
[stalker setCenter:[touch previousLocationInView:self]];
[UIView commitAnimations];
}
What am I missing? It looks like none of my demo apps are properly loading any subviews when I try and follow examples on iTunesU.
If you are creating your 'stalker' view in IB, it must be part of the view hierarchy (that is, added as a subview of another view), or it must be retained by your code in order for it to not be released after loading. If you use a retain property for your stalker variable, this will be taken care of for you, automatically:
#interface BlankCanvasView : UIView
{
UIView *stalker;
}
#property (nonatomic, retain) IBOutlet UIView *stalker;
#end
Make sure you #synthesize stalker; in your BlankCanvasView's implementation, and set the property to nil when you're deallocating your view:
#implementation BlankCanvasView
#synthesize stalker;
- (void)dealloc
{
self.stalker = nil;
[super dealloc];
}
#end