gpt4 book ai didi

iOS - UIButton 事件崩溃

转载 作者:行者123 更新时间:2023-11-29 04:32:06 26 4
gpt4 key购买 nike

我在这个link中尝试了Moshe的代码,除了“for (UIButton *button in ...”的部分之外,它都有效,并且每次我单击按钮时它都会崩溃。

所以我在 viewDidLoad 方法中尝试了这段代码:

UIButton *testButton = [[UIButton alloc]initWithFrame:CGRectMake(20,50,30,30)];
testButton.backgroundColor = [UIColor orangeColor];
[testButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[testButton setTitle:@"A" forState:UIControlStateNormal];
[testButton addTarget:self action:@selector(commonMethodForButtons:) forControlEvents:UIControlEventTouchDown];

[self.view addSubview:testButton];
[testButton release];

我的项目只包含这个和 Moshe 的示例代码。知道应用程序崩溃的原因吗?我没有收到崩溃日志。

编辑:

在开放范围内我有这个方法:

-(void)commonMethodForButtons:(id)sender
{
NSLog (@"you touched me!");
}

编辑2:

我找到了这个问题的原因:

我在 AppDelegate 中注释掉了 [mvc release];,所以它现在工作得很好:)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

// Override point for customization after application launch.

MVC *mcv = [[MVC alloc]initWithNibName:nil bundle:nil];

[self.window addSubview: mcv.view];

//[mcv release];

[self.window makeKeyAndVisible];

return YES;
}

感谢您指出这一点! :)

最佳答案

使用 mcv 作为属性

在AppDelegate的头文件中:

@class MVC;
@interface AppDelegate : UIResponder {
MVC *mcv;
}

@property (nonatomic, retain) MVC *mcv;

在实现文件中

@implementation AppDelegate

@synthesize mcv;

- (void)dealloc
{
[mcv release];
[super dealloc];
}

关于iOS - UIButton 事件崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11595720/

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