Делегат не будет запускаться в init-функции, но после нажатия кнопки UIButton? - PullRequest
0 голосов
/ 23 февраля 2012

Я только что создал свою собственную функцию делегата, используя ARC с последней версией Xcode 4.2.

У меня было несколько сообщений об ошибках, но после нахождения этого поста я смог запустить свой код без проблем.

К сожалению, мой делегат не будет уволен.

Вот мой код:

CVC.h

#import "HotSpotContentView.h"

@interface ContentViewController : UIViewController <UIScrollViewDelegate, HotSpotContentViewDelegate>{
// some stuff....
}

CVC.m

if (!contentView) {
        contentView = [[HotSpotContentView alloc] initWithHotSpot:spot withSize:CGSizeMake(700, 550)];
        contentView.delegate = self;
        [self addSubview:contentView];
}

HotspotView.h

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

@protocol HotSpotContentViewDelegate <NSObject>

- (void)traceMyFunction:(NSString*)txt;

@end

@interface HotSpotContentView : UIView{
    __unsafe_unretained id <HotSpotContentViewDelegate> delegate;
}
-(id) initWithHotSpot:(HotSpotView*)hotspot withSize:(CGSize)contentSize;
@property(unsafe_unretained) id<HotSpotContentViewDelegate>  delegate;
@end

HotSpotView.m

@implementation HotSpotContentView
@synthesize delegate = _delegate;
-(id) initWithHotSpot:(HotSpotView*)hotspot withSize:(CGSize)contentSize
{    
   // some stuff
 self = [super initWithFrame:frame];
    if (self) {
      //some stuff

    [self.delegate traceMyFunction:@"_______HotspotContentView"];
}
    return self;
}

Теперь я изменил свою функцию initWithHotSpot и добавил кнопку UIB.

HotSpotView.m

    @implementation HotSpotContentView
    @synthesize delegate = _delegate;
    -(id) initWithHotSpot:(HotSpotView*)hotspot withSize:(CGSize)contentSize
    {    
       // some stuff
     self = [super initWithFrame:frame];
        if (self) {
          //some stuff
    UIButton* back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [back setTitle:@"Test" forState:UIControlStateNormal];
        back.frame = CGRectMake(100, 100, 50, 100);
        [back addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:back];
    }
        return self;
    }

-(void)buttonClicked:(UIButton*)sender{
    [self.delegate traceMyFunction:@"_______HotspotContentView"];
}

После тестирования и нажатияКнопка, мой делегат будет уволен.Есть идеи, почему делегат может быть уволен после этого или после этого?

    [NSTimer scheduledTimerWithTimeInterval:0.5f target:self 
                                           selector:@selector(sendDelegate) userInfo:nil repeats:NO];

-(void)sendDelegate{
    [self.delegate traceMyFunction:@"_______HotspotContentView"];
}

Спасибо за помощь, я учусь .... :)

...