在ARC环境中有一点问题。创建一个将视图添加到父视图的NSObject-它基本上是一个“弹出类”,可以处理一些文本并显示它。

在视图控制器中被实例化。

CBHintPopup *popup = [[CBHintPopup alloc]init];
[popup showPopupWithText:@"test text" inView:self.view];


和实际的类文件

CBHintPopup.h

@interface CBHintPopup : NSObject {

}

-(void)showPopupWithText:(NSString *)text inView:(UIView *)view;
-(IBAction)closePopup;

@property (nonatomic, strong) UIView *popupView;
@property (nonatomic, strong) UIImageView *blackImageView;
@property (nonatomic, strong) UIButton *closeButton;

@end


CBHintPopup.m

@implementation CBHintPopup
@synthesize  popupView,blackImageView, closeButton;

-(void)showPopupWithText:(NSString *)text inView:(UIView *)view {

//CREATE CONTAINER VIEW
self.popupView = [[UIView alloc]initWithFrame:CGRectMake((view.frame.size.width/2)-(225/2),-146,225,146)];
self.popupView.alpha = 0;
self.popupView.backgroundColor = [UIColor clearColor];

//CREATE AND ADD BACKGROUND
UIImageView *popupBackground = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,225,146)];
popupBackground.image = [UIImage imageNamed:@"hintbackground.png"];
[self.popupView addSubview:popupBackground];

//CREATE AND ADD BUTTON
self.closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[self.closeButton addTarget:self action:@selector(closePopup) forControlEvents:UIControlEventTouchUpInside];
[self.popupView addSubview:self.closeButton];

//CREATE AND ADD LABEL
UILabel *popupTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(22,25,176,93)];
popupTextLabel.text = text;
[self.popupView addSubview:popupTextLabel];


[view addSubview:self.popupView];
}

-(void)closePopup {
NSLog(@"HI");
}


通过按下按钮(不打印“ HI”),调用closePopup即可接收以下内容。

-[CBHintPopup performSelector:withObject:withObject:]: message sent to deallocated instance 0x246b2fe0


我尝试过将按钮保留在非ARC和其他方法中,但是没有运气。可能是真正简单的事情,但我不能钉牢。我删除了所有标签和图像等设置,以节省一些空间,因此请忽略Alpha等。

任何帮助将不胜感激,谢谢您的时间。

最佳答案

您是否已经为CBHintPopup实现了构造函数,因为您已经调用了构造函数

[[CBHintPopup alloc]init];


你必须实现这样的构造方法

在CBHintPopup的.m文件中

-(id)init{

if(self == [super init]){

// do some initialization here

   }
  return self;

}

09-20 19:26