ios - 将右栏按钮添加到导航栏

标签 ios swift uinavigationbar selector

由于 swift 版本正在进行更新,我无法将右栏按钮添加到我的导航栏。这就是我正在尝试的:

class feedViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let editButton   = UIBarButtonItem(title: "Post",  style: .Plain, target: self, action: #selector(didTapEditButton(_:)))

    self.navigationItem.rightBarButtonItem = editButton

    }

func didTapEditButton(sender: AnyObject){

    print("hjxdbsdhjbv")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

没有编译错误,但是当我运行应用程序时,我在那里看不到任何按钮.. 感谢您的宝贵时间。

最佳答案

看起来您需要将您的方法移出您在其中创建 UIBarButtonItem 的方法的范围。

例如试试这个:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let editButton   = UIBarButtonItem(title: "Post",  style: .Plain, target: self, action: #selector(didTapEditButton))  

        self.navigationItem.rightBarButtonItem = editButton
    }


    func didTapEditButton(sender: AnyObject) {
        print("hjxdbsdhjbv")
    }
}

而不是这个:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let editButton   = UIBarButtonItem(title: "Post",  style: .Plain, target: self, action: #selector(didTapEditButton))  

        self.navigationItem.rightBarButtonItem = editButton

        func didTapEditButton(sender: AnyObject) {
            print("hjxdbsdhjbv")
        }

    }

}

关于ios - 将右栏按钮添加到导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37630395/

相关文章:

ios - 如何安全地删除indexPath处的行

ios - 如何在 TableViewController 中设置多个 segue ViewController?

ios - UINavigationBar 奇怪的标准高度

android - 如何在 android 中正确获取 bottombar 的当前选项卡位置?

ios - UIImagePickerController 作为应用程序的主视图 Controller 和随后显示的导航 Controller

objective-c - UITableviewcell 加载数据问题

ios - 将 Caffe 模型转换为 CoreML

ios - swift Eureka : Adding text value to ButtonRow

SwiftUI 从配置文件选项卡栏项目进行导航的最佳方式

ios - 设置 Navbar hidden = false 不起作用