我有像这样的代码
func showSearchBar(searchBar: UISearchBar, navigationItem: UINavigationItem) {
searchBar.alpha = 0
navigationItem.titleView = searchBar
searchBar.showsCancelButton = true
navigationItem.setRightBarButtonItem(nil, animated: true)
for subView in searchBar.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
textField.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Search", comment:""),
attributes:[NSForegroundColorAttributeName: UIColor(red: 25/255, green: 128/255, blue: 214/255, alpha: 1)])
textField.font = UIFont(name:"AvenirNext-Medium", size: 15)
textField.textColor = UIColor(red: 65/255, green: 65/255, blue: 65/255, alpha: 1)
}
}
}
UIView.animateWithDuration(0.5, animations: {
searchBar.alpha = 1
}, completion: { finished in
searchBar.becomeFirstResponder()
})
}
func hideSearchBar(searchBar: UISearchBar, navigationItem: UINavigationItem, navigationController: UINavigationController, leftButton: UIBarButtonItem) {
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "showSearchBar:")
searchBar.alpha = 0
navigationController.navigationBar.topItem?.rightBarButtonItem = button
navigationItem.setLeftBarButtonItem(leftButton, animated: true)
searchBar.clipsToBounds = true
UIView.animateWithDuration(0.3, animations: {
navigationController.navigationBar.alpha = 1
}, completion: { finished in
})
}
我在调用func hideSearchBar后出错了 我不知道,但我认为错误,因为我在hideSearchBar中调用showSearchBar但在showSearchBar上有2个参数,但我没有发送它...我怎么能在动作上发送2个参数
我试过
showSearchBar:
showSearchBar::
但它仍然是错误,我该怎么办? 或者我的代码有其他错误?
答案 0 :(得分:3)
要使名称正确,您需要将Swift方法声明转换为该方法的Objective-C名称。这种翻译很简单,并且遵循完全机械化的规则,但是您将输入名称作为文字字符串,并且输入错误很容易,所以要小心:
注意这意味着如果方法接受任何参数,其Objective-C名称将以冒号结束。大写计数,名称不应包含空格或除冒号外的其他标点符号。
为了说明,这里有三个Swift方法声明,其Objective-C名称在注释中以字符串形式给出:
func sayHello() -> String // "sayHello"
func say(s:String) // "say:"
func say(s:String, times n:Int) // "say:times:"
即使您的选择器名称与声明的方法正确对应,也可能会崩溃。例如,这是一个小测试class
创建NSTimer
并告诉它每秒调用一次特定方法:
class MyClass {
var timer : NSTimer? = nil
func startTimer() {
self.timer = NSTimer.scheduledTimerWithTimeInterval(1,
target: self, selector: "timerFired:",
userInfo: nil, repeats: true)
}
func timerFired(t:NSTimer) {
println("timer fired")
}
}
class
结构上没有错;它编译,并可以在应用程序运行时实例化。但是当我们打电话给startTimer
时,我们会崩溃。问题不在于timerFired
不存在,或者"timerFired:"
不是它的名字;问题是Cocoa找不到timerFired
。反过来,这是因为我们的类MyClass
是一个纯粹的Swift类;因此,它缺乏允许Cocoa查看和调用timerFired
的Objective-C内省和消息发送机制。以下任何一种解决方案都可以解决问题:
MyClass
声明为NSObject
。MyClass
属性声明@objc
。timerFired
属性声明@obc
。timerFired
关键字声明dynamic
。 (但这可能有点过分;你应该保留在需要它的情况下使用dynamic
,即Objective-C需要能够改变类成员的实现。)在你的情况下这样做:
"showSearchBar:navigationItem:"
<强>更新强>
从项目中删除General.swift,并使用以下代码替换Home.swift中的代码:
@IBAction func searchBarAction(sender: AnyObject) {
showSearchBar(self.searchBar, navItem: self.navigationItem)
}
func showSearchBar(searchBar: UISearchBar, navItem navigationItem: UINavigationItem) {
searchBar.alpha = 0
navigationItem.titleView = searchBar
searchBar.showsCancelButton = true
navigationItem.setRightBarButtonItem(nil, animated: true)
for subView in searchBar.subviews {
for subsubView in subView.subviews {
if let textField = subsubView as? UITextField {
textField.attributedPlaceholder = NSAttributedString(string:NSLocalizedString("Search", comment:""),
attributes:[NSForegroundColorAttributeName: UIColor(red: 25/255, green: 128/255, blue: 214/255, alpha: 1)])
textField.font = UIFont(name:"AvenirNext-Medium", size: 15)
textField.textColor = UIColor(red: 65/255, green: 65/255, blue: 65/255, alpha: 1)
}
}
}
UIView.animateWithDuration(0.5, animations: {
searchBar.alpha = 1
}, completion: { finished in
searchBar.becomeFirstResponder()
})
}
//MARK: UISearchBarDelegate
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
hideSearchBar(self.searchBar, navigationItem: self.navigationItem, navigationController: self.navigationController!, leftButton: btnMenu)
}
func hideSearchBar(searchBar: UISearchBar, navigationItem: UINavigationItem, navigationController: UINavigationController, leftButton: UIBarButtonItem) {
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Search, target: self, action: "searchBarAction:") //add your button action here.
searchBar.alpha = 0
navigationItem.rightBarButtonItem = button
navigationItem.setLeftBarButtonItem(leftButton, animated: true)
searchBar.clipsToBounds = true
UIView.animateWithDuration(0.3, animations: {
navigationController.navigationBar.alpha = 1
}, completion: { finished in
})
}