简体   繁体   English

如何从Swift中的另一个视图调用contentOffset?

[英]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. 你需要有指向你的存在的情况下oneViewControlleranthorView实例。 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.

相关问题 使用Swift,如何从另一个视图控制器中的按钮调用在一个视图控制器中编码的函数? - Using Swift, how do I call a function coded in one view controller from a button in another view controller? 如何使用swift从视图控制器调用SetRootViewController? - how do i call SetRootViewController from a view controller using swift? 如何使用Swift从另一个视图调用按钮 - How to call a Button from another view with Swift 如何在Swift中从另一个类调用方法? - How do I call a method from another class in Swift? 使用swift将y contentOffset从scrollViewDidScroll(scrollView传递到另一个视图控制器 - Passing y contentOffset from scrollViewDidScroll(scrollView to another view controller using swift 如何从另一个视图控制器调用方法 - How do I call a method from another view controller iOS如何从另一个View Controller Swift调用func - iOS How to call func from another View Controller Swift 如何从另一个视图控制器重命名按钮标题集(Swift 3) - How do I rename a button title set from another view controller (Swift 3) 如何在Swift中向另一个内部呈现视图控制器? - How do I present a view controller inside another in Swift? 如何判断哪个按钮在 swift 4 中调用了另一个视图? - How do I tell which button called another view in swift 4?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM