gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 12:47:18 29 4
gpt4 key购买 nike

由于 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/

29 4 0