我想将代码从“播放”更改为“暂停”的UIBarButtonItem的标识符。我怎么能这样做?
由于
答案 0 :(得分:17)
1)初始化一个按钮
//change to play
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "someAction")
navigationBar.topItem.leftBarButtonItem = button
//change to pause
let button = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "someOtherAction")
navigationBar.topItem.leftBarButtonItem = button
2)只需更改文字:
navigationBar.topItem?.leftBarButtonItem?.title = "AnyText"
如果您在访问导航栏时遇到问题,最好只设置一些标签(我喜欢使用特定视图的负标签*来确保2个视图*没有获得相同的标签) 。 然后你可以这样做:
let navigationBar = (self.view.viewWithTag(-1) as UINavigationBar)
navigationBar.topItem?.leftBarButtonItem?.title = "AnyText"
答案 1 :(得分:8)
我使用此代码切换编辑模式
@IBAction func editMode(sender: AnyObject) {
self.setEditing(!self.editing, animated: true)
let newButton = UIBarButtonItem(barButtonSystemItem: (self.editing) ? .Done : .Edit, target: self, action: #selector(editMode(_:)))
self.navigationItem.setLeftBarButtonItem(newButton, animated: true)
}
答案 2 :(得分:4)
对于工具栏项,以下是示例代码。
var player:AVAudioPlayer = AVAudioPlayer()
@IBOutlet var toolbar: UIToolbar!
@IBOutlet var playPauseButton: UIBarButtonItem!
@IBAction func playPause(sender: AnyObject) {
if player.playing {
player.pause()
playPauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
toolbar.items![0] = playPauseButton //apply for first toolbar item
} else {
player.play()
playPauseButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause:")
toolbar.items![0] = playPauseButton //apply for first toolbar item
}
}
答案 3 :(得分:1)
此代码经过测试并与Swift 2一起使用
select user_userID, count(*) as orders_count from orders
group by user_userID having orders_count > 1
答案 4 :(得分:0)
我看到了几个线程,这是实际做到我正在寻找的唯一答案。我唯一的问题是它将我的工具栏项移动到最左边,我把它放在中心位置。如果您的按钮以灵活的空格键为中心,或者它不是第一个按钮,只需更改索引,如下所示:
toolbar.items![1] = playPauseButton //apply for second toolbar item