2016-03-31 17 views
0

ありscrollViewと2つのView Controllerがスクロールビューにアタッチされました これで、View Controllerクラスの1つからscrollViewを含むクラスにメソッドにアクセスします。だから私は方法scrollViewDidEndDecleratingを使用することができます。同じクラスでscrollViewDidEndDecleratingを試しても実行されません。しかし、私はメソッドをオーバーライドしようとする場合。私は致命的なエラーが発生します。さて、これはstoryboard第二viewControllerと同様私はviewDidAppear方法のviewWillAppear使用カントHome_Screenあるクラスから別のクラスへのメソッドのオーバーライド

import UIKit 

public class Main_Screen: UIViewController , UIScrollViewDelegate { 

    @IBOutlet weak var aboutMebtn: UIButton! 
    @IBOutlet weak var studybtn: UIButton! 
    @IBOutlet weak var appsbtn: UIButton! 
    @IBOutlet weak var skillsbtn: UIButton! 

    var scrollView : UIScrollView! 

    @IBOutlet weak var avatarImageView: UIImageView! 

    weak var pageControl: UIPageControl! 

    var mainRead : Bool = false 

    override public func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     // Making the view round 
     for v : UIView in self.view.subviews { 
      if v.tag != 1 { 
       v.layer.cornerRadius = v.frame.size.width/2 
       v.layer.masksToBounds = true 
      } 
     } 
    } 

    public func animate(){ 
     // I get a fatal error here 
     var buttons : Array<UIButton> = [aboutMebtn , studybtn , appsbtn , skillsbtn] 

     avatarImageView.alpha = 0.0 
     avatarImageView.transform = CGAffineTransformMakeScale(1.25, 1.25) 

     UIView.animateWithDuration(1.0, delay: 1.0, usingSpringWithDamping: 1.0, initialSpringVelocity: 0.0, options: .CurveEaseIn, animations: {() -> Void in 
      self.avatarImageView.alpha = 1.0 
      self.avatarImageView.transform = CGAffineTransformMakeScale(0.75, 0.75) 
      }, completion: nil) 
} 

のコードをScrollView

import UIKit 

class ViewController: UIViewController , UIScrollViewDelegate { 

    @IBOutlet weak var scrollVieww: UIScrollView! 

    var mainRead : Bool = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.scrollVieww.pagingEnabled = true 
     self.scrollVieww.delegate = self 

     // Do any additional setup after loading the view, typically from a nib. 
     let V1 = self.storyboard?.instantiateViewControllerWithIdentifier("HomeScreen") as UIViewController! 
     //Add initialized view to main view and its scroll view and also set bounds 
     self.addChildViewController(V1) 
     self.scrollVieww.addSubview(V1.view) 
     V1.didMoveToParentViewController(self) 
     V1.view.frame = scrollVieww.bounds 

     //Initialize using Unique ID for the View 
     let V2 = self.storyboard?.instantiateViewControllerWithIdentifier("MainScreen") as UIViewController! 
     //Add initialized view to main view and its scroll view also set bounds 
     self.addChildViewController(V2) 
     self.scrollVieww.addSubview(V2.view) 
     V2.didMoveToParentViewController(self) 
     V2.view.frame = scrollVieww.bounds 

     //Create frame for the view and define its urigin point with respect to View 1 
     var V2Frame: CGRect = V2.view.frame 
     V2Frame.origin.x = self.view.frame.width 
     V2.view.frame = V2Frame 

     //The width is set here as we are dealing with Horizontal Scroll 
     //The Width is x3 as there are 3 sub views in all 
     self.scrollVieww.contentSize = CGSizeMake((self.view.frame.width) * 2, (self.view.frame.height))  
    } 

    func scrollViewDidEndDecelerating(scrollView: UIScrollView) { 
     if(scrollView.contentOffset.x == self.view.frame.size.width * 1 && !mainRead){ 
     mainRead = true 
     // if i execute this i get a fatal error in the animate method in the class Main_Screen 
     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
      if let vc = storyboard.instantiateViewControllerWithIdentifier("MainScreen") as? Main_Screen { 
       vc.animate() 
     } 
    } 
} 

を含むクラスため

コード。

storyboard.instantiateViewControllerWithIdentifier("MainScreen") as? Main_Screen 

+0

アクセスしようとしている特定のコードは何ですか?どこにアクセスしようとしていますか? – PeejWeej

+0

Home_Screenのアニメーションメソッド見ることができるscrollViewDidEndDecleratingのView Controllerクラスを注意深く見てください。 @PEEJWEEJ –

+0

ああ...私はあなたが探していたものからあなたの質問の中で物事を反転させた。 – PeejWeej

答えて

0

行が既に存在するビューコントローラを見つけていない、ビューコントローラの新しいインスタンスを作成しています。

兄弟のViewController間で通信する必要がある場合は、親のViewControllerから接続を設定する必要があります。

+0

どのように私はそれを行うことができますどのようなアイデア私はかなりswiftに新しいです –

+0

それは速いとはまったく関係がありません、それはちょうどiOSで動作しています。ここにいくつかのリンクがあります:https://developer.apple.com/library/ios/documentation/General/Conceptual/Devpedia-CocoaApp/Storyboard.html、https://www.raywenderlich.com/category/ios、https:/ /www.lynda.com/iOS-training-tutorials/413-0.html – PeejWeej

+0

私はこれを使えません。PrepareForSegueの中にMethidを使用してください。 –

関連する問題