You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

点击按钮切换背景图片报错:unrecognized selector问题原因排查请求

Fixing "unrecognized selector sent to instance" for Your Button Tap Action

Hey there, let's break down why you're seeing this error and how to fix it quickly!

What's Causing the Error?

The error message ViewController oButton: unrecognized selector sent to instance ... tells us exactly what's wrong: your app is trying to call a method named oButton on your ViewController instance, but there's no such method in your code.

Looking at your code, oButton is actually an IBOutlet (the button itself), not an action method. This means you accidentally linked your button's tap event to the wrong thing in Storyboard/XIB.

Step-by-Step Fix

  1. Open your Storyboard or XIB file where the oButton is placed.
  2. Right-click on the oButton to bring up the connections menu, or switch to the Connections Inspector (the arrow icon in the right-side panel).
  3. Look under the Sent Events section (usually Touch Up Inside is the default tap event). You'll see it's currently linked to oButton (the IBOutlet) instead of your oButtonAction method.
  4. Delete that wrong connection (click the small "x" next to the connection line), then re-link the tap event to the oButtonAction method. You can do this by dragging from the event to your ViewController class in the code editor, or selecting oButtonAction from the dropdown in the Connections Inspector.

A Small Code Cleanup (Not the Error Cause, But Good Practice)

Your setBackgroundImage line has an extra pair of parentheses that aren't needed. It won't break anything, but cleaning it up makes the code cleaner:

oButton.setBackgroundImage(UIImage(named: "plus"), for: .normal)

Quick Double-Check for Future Functionality

Once you fix the selector error, make sure:

  • The image named plus is properly added to your project's Assets.xcassets folder.
  • The image name matches exactly (iOS is case-sensitive, so Plus won't work if your file is named plus).

内容的提问来源于stack exchange,提问作者Artem Derut

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:45:00