在 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");
}
通过按下按钮调用 closePopup 后接收以下内容(不打印“HI”)..
-[CBHintPopup performSelector:withObject:withObject:]: message sent to deallocated instance 0x246b2fe0
我尝试在非 ARC 和其他方法中保留按钮,但只是没有运气。可能是一些非常简单的事情,但我无法确定它。我已经删除了标签和图像等的所有设置以节省一些空间,所以忽略 alpha 等。
任何帮助将不胜感激,感谢您的时间。