Proper implementation of the delegate and protocol

I created this test case as a simple example of my problem.

  • AppDelegate initializes the TestViewController and adds it to the window
  • TestViewController initializes TestView and makes it look
  • TestView initializes TestSubView and adds it as a subview

My goal is to provide TestSubView through delegate access to the methods and variables of TestViewController. In this example, TestSubView accesses methods through touchsBegan or touchhesMoved.

Please, help. Thank.

EDITOR: still not working, although no more errors. What I did: I moved the protocol definition to a separate file and imported it, saved it for assignment, and deleted the @protocol TestDelegate declaration at the beginning of the header.

I think my problem is that I do not assign a delegate to TestViewController.m, if this is a problem, how would I do it?

TestViewController.h

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController <TestDelegate> {

    int number;
}

-(void)assignNumber:(int)value;
-(void)displayNumber;

@property int number;

@end

Move to protocol.h and import where required

@protocol TestDelegate

-(void)assignNumber:(int)value;
-(void)displayNumber;

@end

TestViewController.m

#import "TestViewController.h"
#import "TestView.h"

@implementation TestViewController

@synthesize number;

- (void)loadView {
    TestView *myView = [[TestView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    self.view = myView;
    [myView release];
}

-(void)assignNumber:(int)value {
    NSLog(@"Number Assigned");
    number = value;
}

-(void)displayNumber {
    NSLog(@"%i",number);
}

TestSubView.h

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


@interface TestSubView : UIView {

    id<TestDelegate> delegate;
}

@property (nonatomic, retain) id<TestDelegate> delegate;

@end

TestSubView.m

#import "TestSubView.h"
#import "TestViewController.h"

@implementation TestSubView

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        // Initialization code
        [self setBackgroundColor:[UIColor redColor]];
        [self setUserInteractionEnabled:YES];
    }
    return self;
}


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

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.delegate assignNumber:5];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    [self.delegate displayNumber];  
}
+3
source share
2 answers

First of all, you declare a protocol and then assign the same object as the implementation of this protocol. What you're probably looking for is to declare the TestDelegate protocol on your TestSubView. Also, when setting the property for the delegate, be sure to use the keyword "assign" rather than "save."

, UIGestureRecognizer. testSubView. , , , , , , , .

0

TestView, TestSubView, .

TestView *myView = [[TestView alloc] initWithFrame:CGRectMake(0,0,320,480)];

, .
:

myView.delegate = self;

:

- (void)loadView {
    TestSubView *myView = [[TestSubView alloc] initWithFrame:CGRectMake(0,0,320,480)];
    myView.delegate = self; // Set Delegate Here
    self.view = myView;
    [myView release];
}
0

Source: https://habr.com/ru/post/1776116/


All Articles