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.
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). 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
. You need to have pointer to your existed instance of oneViewController
in your anthorView
instance. 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
. Here is changes that you need add to your classes
class anthorView:
var offsetClosure: (CGFloat -> ())?
func btnForget_Click(sender: AnyObject) {
// delete previous implementation
offsetClosure?(0)
}
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. For do this declare button as property in 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:
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)
}