SubViews not properly initialized using IB - iphone

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

Related

Hide TableView declared in other class

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.

UserInteraction enable for subview only

I have a view and view.UserInteractionenabled = no and a button is added to the view. i need to click the button only . is it possible to enable interaction for button only.
A view cannot receive touches unless userInteractionEnabled is YES for the view and all of its superviews up to the UIWindow object.
You can make a subclass of UIView to contain the button, and make it ignore touches outside the button by overriding hitTest:withEvent:. Example:
#interface MyView : UIView
#property (nonatomic, strong) IBOutlet UIButton *button;
#end
#implementation MyView
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
UIView *subview = [super hitTest:point withEvent:event];
return subview == self.button ? subview : nil;
}
#end

UIControl Not Receiving Touches

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 !

iphone paint on top / overlay with event pass through

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

Using UITouch To Drag A UITextField Around The Screen on iPhone

I am developing a sample application, where I have a situation for moving a UITextFields, UILabels etc., across the screen. I am not able to find any resource for implementing this.
I want to implement like this for a UITextField
Since you have an excellent tutorial on dragging already, I will assume your problem is the keyboard that comes up when you try to drag a UITextField instead of a UIView. The solution should be pretty simple: myTextField.userInteractionEnabled = NO; - that should disable user interaction and make it "read only". Perhaps have an edit mode where all text fields get this flag set. If it causes trouble, then add the textField to a UIView and then set the userInteractionEnabled to false. Then you can drag the UIView and it will drag the text field with it.
I followed Michael's suggestion and got the solution.I have given the code snippets below which will be useful for those who need to implement the same.
Steps:
Choose windows based application,then create a UIViewController subclass and add it to the window in the appdelegate file.
In the XIB of the viewcontroller class you created add UIViews and add the controls like textfield etc.,to the UIViews that you have created.We are going to move these views only,so add IBOutlets in the .h file of the view controller subclass and map them to the IB accordingly.
Sample Code
appdelegate.h
#import <UIKit/UIKit.h>
#import "MyView.h"
#interface MyAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MyView *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#end
appdelegate.m
#import "MyAppDelegate.h"
#implementation MyAppDelegate
#synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch.
viewController=[[MyView alloc]init];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
viewcontroller.h
#import <UIKit/UIKit.h>
#interface MyView : UIViewController {
IBOutlet UIView *textFieldView;
IBOutlet UIView *labelView;
}
#end
viewcontroller.m
#import "MyView.h"
#implementation MyView
- (void)viewDidLoad {
[self.view addSubview:textFieldView];
[self.view addSubview:labelView];
[super viewDidLoad];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// get touch event
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if ([touch view] == textFieldView) {
// move the image view
textFieldView.center = touchLocation;
}
if ([touch view] == labelView) {
// move the image view
labelView.center = touchLocation;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Thank u all.Have a niece time.