- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 View 上添加了一个按钮,并使用 addTarget 绑定(bind)事件来调用 self.testp,但是当我运行它时,发生了一个错误:
2015-06-19 23:08:29.237 UI[16978:1700826] -[UI.ViewController testp:]: unrecognized selector sent to instance 0x7864d4a0
2015-06-19 23:08:29.240 UI[16978:1700826] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UI.ViewController testp:]: unrecognized selector sent to instance 0x7864d4a0'
代码是:
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var btn:UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
btn.frame = CGRectMake(10, 150, 100, 30)
btn.setTitle("button", forState: UIControlState.Normal)
//!!!!!!DID NOT WORK
btn.addTarget(self, action: Selector("testp:"), forControlEvents: UIControlEvents.TouchUpInside);
self.view.addSubview(btn)
func testp(){
println("tttt")
}
}
}
为什么?!!
最佳答案
只需从您的选择器中删除 :
,您的代码将是:
btn.addTarget(self, action: Selector("testp"), forControlEvents: UIControlEvents.TouchUpInside)
并将您的函数放在 viewDidLoad
方法之外,但放在 ViewController
类中。
如果您的函数有如下所示的参数,您可以使用 "testp:"
:
func testp(yourArgument: String){
println("tttt")
}
关于TouchUpInside 上的 Swift addTarget 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30941631/
nextButton 仅响应第一次单击。 我需要实现其他措施来识别更多点击吗? nextButton.TouchUpInside += (s, e) => { }; 最佳答案 设置 nextButto
我有以下代码: override func viewDidLoad() { super.viewDidLoad() ... signInButton =
我有一个带有图像的简单按钮,它的宽度和高度固定为 110。 所以问题是,只有当我点击按钮的前 20% 时,touchUpInside 事件才会触发,如果点击下方,则不会触发任何内容,甚至连按钮上的点击
我是 IOS 编程的新手,但奇怪的是,相同的代码在不同的类中工作,所以我不知道为什么它不起作用它在 Swift 3 中 class BottomMenu : NSObject{ //Conta
我在 Interface Builder 中设置了三个按钮,每个按钮都通过 touchUpInside 绑定(bind)到 btnSelection: - (IBAction)btnSelection
所以我有一个很大的UIButton,它是一个UIButtonTypeCustom,并且为UIControlEventTouchUpInside调用按钮目标。我的问题是如何确定 UIButton 中发生
我正在尝试使用 Swift 在 iOS 应用程序中按下按钮时自定义按钮的外观和感觉。我目前正在向其添加目标,包括 TouchDown 和 TouchUpInside,但按钮标签的标准“变暗”仍然保留。
UIButton TouchUpInSide 不工作,但在长按选择器对应的 TouchUpinside 被调用,TouchDown 工作正常。我谷歌了很多来找到答案,但没有运气。希望这个奇怪的问题得到
我有一个 UIScrollView,里面装满了我以编程方式创建的按钮。分页已启用,每当用户快速滚动页面时,他们总是会意外点击他们不是故意的按钮...我应该使用 TouchUpInside 还是这是我出
我正在以编程方式创建 UIButton 并设置它们的目标。大多数时候,TouchDown-TouchDragInside-TouchUpInside 链似乎正常工作,但如果我快速执行此事件链(每秒约
这是我的 Xib。我只是拖动了一个 UIButton 并更改了它的背景颜色。 我使用这段代码创建了一个半圆图层 let circlePath = UIBezierPath.init(arcCe
我的 ViewController 就像 public partial class TestView : MvxViewController { ...code here... } 然
有时,当您的项目包含非常不受管理的代码时,您无法弄清楚如何解决一个简单的问题,就会发生这种情况。在大多数情况下,调试器帮助我们到达根点开始,我只是想知道 xcode 调试器有多强大。当我使用一些像 O
我有一堆通过代码添加的 UILabel,如果用户的手指触摸内部(很像 IB 中的 UIButton 的 touchUpInside),我想对每个标签执行特定的操作。最好的方法是什么? 最佳答案 最简单
我可能在这里遗漏了一些东西,但是...... 我有一个 UIView 和几个 child (几个 UILabel 和 UIImageView)。当用户单击(点击)父 UIView 中的任何位置(UIV
我像往常一样在 IB 中连接了 touchUpInside,使用新的 Xcode,build设置为 iOS 5.0,但我的 iPhone 4(iOS6 发布之前的最新 iOS5)似乎没有响应。有人有同
我在最新的 Xcode 11.0 中遇到了奇怪的错误 我的代码: let button = ASAuthorizationAppleIDButton(type: .default, style: .b
我正在努力在运行时向 UICollectionViewCell 添加按钮,并在运行时根据其类型将按钮注册到事件。按钮的类型可以是例如话语按钮和操作按钮,并且两者都应该注册到单独的事件。 UI 元素的层
我将一些自定义类型的 UIButtons 实用地放置在自定义 View 中。 View 基本上是一个菜单。我在 View 的 .m 文件中使用以下代码生成按钮: -(void) generateBut
我有一个 UISwipeGestureRecognizer,它根据手势时突出显示的 UIButton 进行动画处理,它在 UIButton 时完成动画调用 TouchUpInside/TouchUpO
我是一名优秀的程序员,十分优秀!