UIView custom delegate does not detect touch

I did my research but didn't find an answer to the following problem: I have a custom delegate - a UIView class - and for some reason the touchBegan doesn't work in the delegate implementation.

TestView.h

#import <UIKit/UIKit.h>

@class TestView;

@protocol TestViewDelegate <NSObject>
@end

@interface TestView : UIView
@property (assign) id <TestViewDelegate> delegate;
@end

      

TestView.m

#import "TestView.h"

@implementation TestView

@synthesize delegate = _delegate;

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSLog(@"Touch detected on TestViewDelegate");
}

@end

      

ViewController.h

#import <UIKit/UIKit.h>
#import "TestView.h"

@interface ViewController : UIViewController<TestViewDelegate>
@end

      

ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

UILabel* title = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 280, 40)];
[title setFont:[UIFont fontWithName:@"Helvetica-Bold" size:30]];
[title setTextColor:[UIColor blackColor]];
[title setTextAlignment:UITextAlignmentCenter];
[title setBackgroundColor:[UIColor clearColor]];
[tile setText:@"Test"];
[self.view addSubview:title];    
}

- (void)viewDidUnload
{
[super viewDidUnload];
}

@end

      

What am I missing to make sure that touchesBegan

from TestView.m is called when calls appear in ViewController.m?

+3


source to share


1 answer


Your last line indicates a fundamental misunderstanding of views and controllers view. Touches do not occur as controllers; touches occur in the eyes. After the gaze has touched, it tells the controller that it was touched, and the controller does something with this information. The way it does it is through a pattern called delegation.

So let it go piece by piece. To get what you want, you will need to do the following:

First: create an instance TestView

and add it as a subview of the view controller.

The view now exists and when you click it, you will see yours is "Touch detected on TestViewDelegate"

logged into the console. But it won't do anything with the delegate (there is no delegate yet!).

Second: set the newly created property TestView

delegate

to the view controller. Do this after creating the instance TestView

, but before adding it to the view hierarchy.

Now they are slightly connected, but the view never talks to its delegate (this does not happen automatically, when you create the delegate protocol, you must specify which messages will be displayed in the view).



Third: add a method to the protocol TestViewDelegate

and implement that method in the view controller. It could be something like, touchesBeganOnTestView:(TestView *)sender

or something else that you want the view to pass to the delegate when it touched. It looks like this:

@class TestView;
@protocol TestViewDelegate <NSObject>
- (void)touchesBeganOnTestView:(TestView *)sender;
@end

      

You must add a line @class

because the protocol declaration precedes the declaration TestView

- at this point in the file, the compiler doesn't know what " TestView

" means , so to avoid the warning you say "don't worry, I'm going to declare that later."

Fourth: call this method from TestView

touchesBegan

. It's as easy as adding a line [self.delegate touchesBeganOnTestView:self];

.

This will give you what you want. From your question I am going that you are very new to iOS / Objective-C and it will be difficult if you don't have a clear understanding of the basics. Apple's description of delegation might be a good place to start .

+8


source







All Articles