简体   繁体   English

ios5 - 单击按钮时出现“EXC_BAD_ACCESS”

[英]ios5 - “EXC_BAD_ACCESS” when clicking button

I am a total beginner to Objective C. 我是Objective C的初学者。

Currently I try to display a view in my main window. 目前我尝试在主窗口中显示视图。 This view contains a button. 该视图包含一个按钮。 For some reason by clicking this button xcode throws an error. 出于某种原因,单击此按钮xcode会引发错误。

Hopefully you can help me to understand what I am doing wrong. 希望你能帮助我理解我做错了什么。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];

    BtnView *btn = [[BtnView alloc] init];
    [self.window addSubview:btn.view];        
    [self.window makeKeyAndVisible];

    return YES;
}

The viewController of my button view has an action for recognizing a touch up event h file: 我的按钮视图的viewController有一个用于识别触摸事件h文件的动作:

- (IBAction)touched:(id)sender;

and in the m file 并在m文件中

- (IBAction)touched:(id)sender {
    //actions ...
}

By touching the button I get the following error: 通过触摸按钮我收到以下错误:

在此输入图像描述 Whats wrong? 怎么了?

m file: m文件:

在此输入图像描述

h file: h文件: 在此输入图像描述

I assume you have ARC enabled for this project. 我假设你为这个项目启用了ARC。

When you return from didFinishLaunchingWithOptions: the object assigned to btn will be released since you assign it to a local variable that is only defined within that method. didFinishLaunchingWithOptions:返回时didFinishLaunchingWithOptions:分配给btn的对象将被释放,因为您将其分配给仅在该方法中定义的局部变量。 Make BtnView *btn a strong property of your class and it should stay around to handle the button presses. 使BtnView *btn成为你班级的strong属性,它应该留在处理按钮按下。 (Remember to use self.btn in references to it.) (记得在引用中使用self.btn 。)

(But, I'm curious, why not just put the button on your main view in the first place and then not worry about an extra controller and subviews?) (但是,我很好奇,为什么不首先将按钮放在主视图上,然后再担心额外的控制器和子视图?)

- (IBAction)touched

尝试不使用发件人

In .xib file, you can hook up the button to file's owner. 在.xib文件中,您可以将按钮连接到文件的所有者。

1) Right Click on button. 1)右键单击按钮。 Then one window will be appeared. 然后会出现一个窗口。 You can see Touch Up Inside in that window and click on small circle and drag to file's owner and connect to touched . 您可以在该窗口中看到Touch Up Inside并单击小圆圈并拖动到文件的所有者并连接到touched I think it will be helpful to you. 我认为这对你有所帮助。

Is there any specific reason why do you need the whole UIViewController to be a button? 有什么特定的理由为什么你需要整个UIViewController作为一个按钮? The conventional way to do this would be to have the UIViewController as a controller and than put a UIButton as a subview to its view in the viewDidLoad method, hook it up to the desired methods and even have it across the whole screen if you need. 执行此操作的传统方法是将UIViewController作为控制器,而不是将UIButton作为子视图放在viewDidLoad方法的视图中,将其连接到所需的方法,如果需要,甚至可以将其放在整个屏幕上。 In case you need to customize the appearance, you can do that with UIButton too. 如果您需要自定义外观,也可以使用UIButton进行自定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM