我有一个UIBarButtonItem,我想在另一个类中推送ViewController。所以我在viewDidLoad中有这行代码:
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<div id="image" onmousedown="mouseDown();" onmouseup="mouseUp();">
<img src="schloss.jpg" draggable="false" />
</div>
<script>
var oldY,oldX;
function getCoordinates(elem) {
var LeftPos = elem.offsetLeft;
var TopPos = elem.offsetTop;
return {X:LeftPos,Y:TopPos};
}
function mouseUp() {
document.removeEventListener('mousemove', mouseMove, false);
document.removeEventListener('mouseup', mouseUp, false);
}
function mouseDown() {
document.addEventListener('mousemove', mouseMove, false);
document.addEventListener('mouseup', mouseUp, false);
oldY=-1; oldX=-1;
}
function mouseMove(e){
var iwidth,iheight,newX, newY, diffX, diffY;
var div=document.getElementById('image');
div.style.position = 'absolute';
if (oldX==-1) {
diffX=0;diffY=0;}
else {
diffY=e.clientY-oldY;diffX=e.clientX-oldX;}
var p=getCoordinates(div);
newY=p.Y+diffY;newX=p.X+diffX;
div.style.top = newY + 'px';div.style.left = newX + 'px';
oldY=e.clientY;oldX=e.clientX;
}
</script>
</body>
</html>
这是我的GoToPages课程:
self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(named:"settings"), style: .plain, target: self, action: #selector(GoToPages.goToSettings(_:)))
但是我收到一个错误:无法识别的选择器被发送到实例。
答案 0 :(得分:1)
参数target
的值必须是实现action
的类的实例。
self
表示action
已在当前类中实现。
答案 1 :(得分:0)
我的代码:
导入UIKit
扩展名UIViewController {
@objc func goToPage(_ sender: Any) {
guard let button = sender as? UIBarButtonItem else {
return
}
switch button.tag {
case 0:
let settingsPageView = self.storyboard?.instantiateViewController(withIdentifier: "Notifications")
self.navigationController?.pushViewController(settingsPageView!, animated: true)
case 1:
let settingsPageView = self.storyboard?.instantiateViewController(withIdentifier: "Settings")
self.navigationController?.pushViewController(settingsPageView!, animated: true)
default:
return
}
}
答案 2 :(得分:0)
您应该删除“ GoToPages”。来自#selector(GoToPages.goToSettings(_:))
的选择器定义:
#selector(goToSettings(_:))
因为GoToPages.goToSettings是类函数,并且由于您的参数“ target:self”而应该是可以工作的实例方法。这里self指定实例,因此您的选择器必须是实例方法。
如果这不能解决您的问题,请使用self.navigationItem.setRightBarButton(...)
而不是直接分配给self.navigationItem.rightBarButtonItem = ...
以下是对我有用的代码:
override func viewDidLoad() {
super.viewDidLoad()
let barButtonItem = UIBarButtonItem(image: UIImage(named: "image"),
style: .plain,
target: self,
action: #selector(barButtonItemTapped(sender:))
navigationItem.setRightBarButtonItems([barButtonItem], animated: true)
}
@objc private func barButtonItemTapped(sender: UIBarButtonItem) {
}