简体   繁体   English

呼叫代表方法(iOS)

[英]Call delegate method (iOS)

Fist of all I want to show you my code/storyboard: 我想向您展示我的代码/故事板的拳头: 故事板

This is my storyboard: I have a little UISlider and a UIView. 这是我的故事板:我有一个小UISlider和一个UIView。 The UIView is a custom view and his class is "SquareView". UIView是一个自定义视图,其类为“ SquareView”。 This is the code: 这是代码:

SquareView.h SquareView.h

#import <UIKit/UIKit.h>

    @class SquareView;

    @protocol SquareViewDelegate <NSObject>

    -(int)giveMeTheNumbersOfSquare:(SquareView *)squareView;

    @end

    @interface SquareView : UIView

    @property (nonatomic , weak) IBOutlet id<SquareViewDelegate> delegate;

    @end

and SquareView.m SquareView.m

#import "SquareView.h"

@implementation SquareView
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{

    int numberOfSquare = [delegate giveMeTheNumbersOfSquare:self];
    NSLog(@"Value of numberOfSquare in drawRect---> %d" ,numberOfSquare);
    //numberOfSquare = 20;
    for (int i=0; i<numberOfSquare; i++) {
        float x = arc4random() % (int)self.frame.size.width - 20;
        float y = arc4random() % (int)self.frame.size.width - 20;

        UIRectFill(CGRectMake(x, y, 20.0, 20.0));
       // NSLog(@"X---> %f" ,x);
        //NSLog(@"Y---> %f" ,y);
        [self setNeedsDisplay];
    }

    // Drawing code
}
@end

Now the ViewController... The ViewController is the SquareView's Delegate. 现在,ViewController ... ViewController是SquareView的委托。

ViewController.h ViewController.h

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

@interface TestViewController : UIViewController <SquareViewDelegate>

@property (weak) IBOutlet SquareView *squareView;
@property(weak) IBOutlet UISlider *slider;

-(IBAction)changeSquareNumbers:(id)sender;


@end

and ViewController.m ViewController.m

#import "TestViewController.h"

@interface TestViewController ()
{
    int _squareCount;
}

@end

@implementation TestViewController

@synthesize squareView = _squareView;
@synthesize slider = _slider;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

-(void)awakeFromNib
{
    [_squareView setDelegate:self];
    _squareCount = 50;
    [self.squareView setNeedsDisplay];
}

/********* Delegate Method *********/
-(int)giveMeTheNumbersOfSquare:(SquareView *)squareView
{
    //return _squareCount;
    NSLog(@"Value of _squareCount in giveMeTheNumberOfSquare ---> %d" ,_squareCount);
    return _squareCount;
}

-(IBAction)changeSquareNumbers:(UISlider *)sender
{
    NSLog(@"Value of slider in changeSquareNumbers --> %f" ,[sender value]]);
    _squareCount = (int)[sender value];
    NSLog(@"Value of _sqareCount ---> %d" ,_squareCount);
    [self.squareView setNeedsDisplay];
}

Now... in the SquareView.m file, in the drawRect function I don't know why the program NEVER call the "giveMetheNumbersOfSquare function. The "numberOfSquare" in drawRect stay at 0!!! I don't understand why the program NEVER enter in the "giveMeTheNumberOfSquare" Function!! 现在...在SquareView.m文件的drawRect函数中,我不知道为什么该程序从不调用“ giveMetheNumbersOfSquare函数。drawRect中的” numberOfSquare“保持为0 !!!我不明白为什么该程序切勿输入“ giveMeTheNumberOfSquare”函数!

Thanks!! 谢谢!!

Make sure that the delegate is being set correctly. 确保正确设置了委托。 You might need to alloc or initialize your squareView. 您可能需要分配或初始化squareView。 Also if you're using a storyboard you might not be calling awakeFromNib . 另外,如果您使用情节提要,则可能不会调用awakeFromNib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM