[英]How do I call an contentOffset from another view in Swift?
class oneViewController: UIViewController{
let sv = UIScrollView();
override func viewDidLoad() {
super.viewDidLoad();
sv.frame = CGRectMake(0, 100, screenWidth, screenHeight);
sv.backgroundColor = UIColor.clearColor();
sv.contentSize = CGSize(width: sv.frame.size.width*3 ,height: sv.frame.size.height);
sv.delegate = self;
scrollToContentOffsetX(screenWidth);
self.view.addSubview(sv);
}
func scrollToContentOffsetX(offsetX:CGFloat) {
sv.contentOffset.x = offsetX;
}
}
I can scroll UIScrollView,it working.But I call from another View,its not working. 我可以滚动UIScrollView,它工作。但我从另一个视图调用,它不工作。
class anthorView: UIView{
override init (frame : CGRect)
{
super.init(frame : frame)
let btnForget = UIButton(frame: CGRectMake(screenWidth-155, txtPWD.frame.origin.y+txtPWD.frame.size.height+5, 70, 30));
btnForget.setTitle("Forget", forState: UIControlState.Normal);
btnForget.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal);
btnForget.addTarget(self, action: #selector(btnForget_Click), forControlEvents: .TouchUpInside);
btnForget.titleLabel!.font = UIFont.systemFontOfSize(14);
btnForget.backgroundColor = UIColor.clearColor();
self.addSubview(btnForget);
}
func btnForget_Click(sender: AnyObject) {
oneViewController().scrollToContentOffsetX(0);
}
}
I have breakpoint,both have go func scrollToContentOffsetX(offsetX:CGFloat). 我有断点,都有func scrollToContentOffsetX(offsetX:CGFloat)。 who can help me and sry for my English
谁可以帮助我,为我的英语而烦恼
Problem in your code pointed by @AminNegm-Awad: in btnForget_Click
you create completely new instance of oneViewController
, which will never appear on screen and have no connection to existed oneViewController
. @ AminNegm-Awad指向您的代码中的问题:在
btnForget_Click
您创建了oneViewController
全新实例,该实例将永远不会出现在屏幕上并且与现有的oneViewController
没有任何关联。 You need to have pointer to your existed instance of oneViewController
in your anthorView
instance. 你需要有指向你的存在的情况下
oneViewController
在anthorView
实例。 Usual way for view to "talk" with controller is protocol-delegate. 通常与控制器“对话”的方式是协议委托。 Read about it by yourself;
亲自阅读; here I will offer another solution with closure.
在这里,我将提供另一种封闭解决方案。 Anyway, you will need to have pointer to your
anthorView
instance (lets declare it as anthorViewInstance
property) in oneViewController
. 无论如何,你将需要有指向您的
anthorView
实例(让声明为anthorViewInstance
财产) oneViewController
。 Here is changes that you need add to your classes 以下是您需要添加到课程中的更改
class anthorView: class anthorView:
var offsetClosure: (CGFloat -> ())?
func btnForget_Click(sender: AnyObject) {
// delete previous implementation
offsetClosure?(0)
}
class oneViewController: class oneViewController:
var anthorViewInstance: anthorView!
override func viewDidLoad() {
super.viewDidLoad()
// some your code...
// next line is initialization of anthorView, do it in other place if you wish
anthorViewInstance = anthorView(frame: CGRectZero)
anthorViewInstance.offsetClosure =
{
[weak self]
offsetX in
self?.scrollToContentOffsetX(offsetX)
}
}
Also, there is local case for this solution can be performed in your case. 此外,在您的情况下可以执行此解决方案的本地案例。 You may set button action of
anthorView
directly from view controller. 您可以直接从视图控制器设置
anthorView
按钮操作。 For do this declare button as property in anthorView
为此,请在
anthorView
按钮声明为属性
class anthorView: class anthorView:
var btnForget: UIButton!
super.init(frame : frame) {
btnForget = UIButton(frame: CGRectMake(screenWidth-155, txtPWD.frame.origin.y+txtPWD.frame.size.height+5, 70, 30))
// ... other init code
// remove btnForget.addTarget line
}
// move btnForget_Click method to view controller
class oneViewController: class oneViewController:
var anthorViewInstance: anthorView!
override func viewDidLoad() {
super.viewDidLoad()
// some your code...
// next line is initialization of anthorView, do it in other place if you wish
anthorViewInstance = anthorView(frame: CGRectZero)
anthorViewInstance.btnForget.addTarget(self, action: #selector(btnForget_Click), forControlEvents: .TouchUpInside)
}
func btnForget_Click() {
scrollToContentOffsetX(0)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.