Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在swift 3中以编程方式设置barButtonItem的操作?_Ios_Swift_Selector_Swift3_Uibarbuttonitem - Fatal编程技术网

Ios 如何在swift 3中以编程方式设置barButtonItem的操作?

Ios 如何在swift 3中以编程方式设置barButtonItem的操作?,ios,swift,selector,swift3,uibarbuttonitem,Ios,Swift,Selector,Swift3,Uibarbuttonitem,这是我以前用过的 var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector("menuButtonTapped:")) 但是Swift 3的语法有一些变化。例如: navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .pla

这是我以前用过的

var barButtonItem = UIBarButtonItem(image: backImgs, style: UIBarButtonItemStyle.plain, target: self, action: Selector("menuButtonTapped:"))
但是Swift 3的语法有一些变化。

例如:

navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))

从Swift 3开始,您只需更改
选择器
语法,您需要在函数调用中指定方法的第一个参数名,因此请按如下方式更改选择器

#selector(menuButtonTapped(sender:))
func menuButtonTapped(sender: UIBarButtonItem) {

}
你的方法应该是这样的

#selector(menuButtonTapped(sender:))
func menuButtonTapped(sender: UIBarButtonItem) {

}

iOS 10.1的Swift 3上有一行代码:

navigationController?.navigationBar.topItem?.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: nil)

总结Swift 3中为按钮添加操作的常用方法

  • 带文本的按钮

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addTapped))
    
  • 巴巴顿有你自己的形象

    navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"add"), style: .plain, target: self, action: #selector(addTapped))
    
  • 带系统映像的按钮

    navigationItem.rightBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addTapped))
    

  • 制作一个uibarbuttonite:

    let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(LocationViewController.doneButtonClicked(_:)))
    
    添加到NavigationItem:

    self.navigationItem.rightBarButtonItem = rightButton
    
    相关功能:

    func doneButtonClicked(_ button:UIBarButtonItem!){    
        print("Done clicked")    
    }
    

    如果有人正在使用
    customView

    barButtonItem.customView?.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(onBarButtonItemClicked)))
    

    为barbutton项创建扩展

     extension UINavigationItem {
        func addSettingButtonOnRight(){
           let button = UIButton(type: .Custom)
           button.setTitle("setting", forState: .Normal)
           button.titleLabel?.font = UIFont.systemFontOfSize(15.0)
           button.layer.cornerRadius = 5
           button.backgroundColor = UIColor.grayColor()
           button.frame = CGRect(x: 0, y: 0, width: 100, height: 25)
           button.addTarget(self, action: #selector(gotSettingPage), forControlEvents: UIControlEvents.TouchUpInside)
           let barButton = UIBarButtonItem(customView: button)
    
           self.rightBarButtonItem = barButton
       }
    
       func gotSettingPage(){
    
       }
     }
    
    并从viewDidLoad()调用它


    在Swift 3中,你可以像这样添加uibarbuttonite

    let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
    addButton.tintColor = UIColor.white
    self.navigationItem.rightBarButtonItem = addButton
    
    func buttonAction(_ sender: UIBarButtonItem) {
    
    }
    
    像这样处理按钮动作

    let addButton = UIBarButtonItem(image:UIImage(named:"your_icon_name"), style:.plain, target:self, action:#selector(YourControllerName.buttonAction(_:)))
    addButton.tintColor = UIColor.white
    self.navigationItem.rightBarButtonItem = addButton
    
    func buttonAction(_ sender: UIBarButtonItem) {
    
    }
    

    希望有帮助。

    对于Swift 4加载项
    viewDidLoad

    navigationItem.rightBarButtonItem = UIBarButtonItem(
        barButtonSystemItem: UIBarButtonSystemItem.add, 
        target: self, 
        action: #selector(addTransaction)
    )
    

    如果有人正在寻找在现有按钮(Swift 5)上设置操作的方法:

    斯威夫特5

    //For righter button item
    let rightBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod))//Change your function name and image name here
    self.navigationItem.rightBarButtonItem = rightBtn
    //self.navigationItem.rightBarButtonItem = [rightBtn, anotherBtn] //If you want to add more buttons add like this
    
    //For left bar button item
    let leftBtn = UIBarButtonItem(image: UIImage(named: "rightmenu"), style: .plain, target: self, action: #selector(onClickMethod)) //Change your function name and image name here
    self.navigationItem.leftItemsSupplementBackButton = true
    self.navigationItem.leftBarButtonItem = leftBtn
    //self.navigationItem.leftBarButtonItems = [leftBtn, anotherBtn] //If you want to add more buttons add like this
    
    //This is your function name
    @objc func onClickMethod() {
        print("Left bar button item")
    }
    

    在swift 3中,我必须指定第一个参数,但我不需要这样做。。无论如何thanks@Bhanupriya是的,您可以,但为此,您需要使用带有选择器的
    \code>\selector(menubuttonapped(:)
    ,并且还需要将
    \uu
    作为第一个参数添加到方法中,如
    func menubuttonapped(u.发送方:uibarbuttoneim){
    您必须以其他方式使用的选项中的任何一个都不起作用。@Bhanupriya您是否得到了我在上述注释中告诉您的有关选择器的信息?不,我不需要。我需要的是:var barButtonItem=UIBarButtonItem(图像:backImgs,样式:。plain,目标:self,操作:#选择器(menubuttonapped))@Bhanupriya那么,为什么您以前尝试使用
    选择器(“menuButtonTapped:”)
    此处
    表示方法包含一个参数,这就是我以这种方式给出答案的原因。谢谢!这节省了我的时间。我会单击向上箭头,但它位于69,所以我们暂时不谈。@JackDerbis如果在使用
    自定义视图时需要将手势识别器添加到条形按钮项中,这将起作用。
    初始值设定项。据我所知,只有此方法签名有效(将手势添加到视图本身/添加到工具栏按钮/任何其他操作都不适用于我)。太好了。它也适用于Swift 4和更低版本。