gpt4 book ai didi

iphone - 将 NSObject 添加到 View - NSObject 中的 UIButton 选择器导致崩溃

转载 作者:行者123 更新时间:2023-12-03 19:54:39 27 4
gpt4 key购买 nike

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

在 View Controller 中它被实例化..

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 等。

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

最佳答案

你实现了 CBHintPopup 的构造函数吗? ,因为您已经调用了构造函数

[[CBHintPopup alloc]init];

你必须像这样实现构造函数

在CBHintPopup的.m文件中

-(id)init{

if(self == [super init]){

// do some initialization here

}
return self;

}

关于iphone - 将 NSObject 添加到 View - NSObject 中的 UIButton 选择器导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12894706/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com