2016-03-30 18 views
0

Ich habe mit UIScrollView beschäftigt Ich möchte es für eine andere Klasse zugänglich sein, so dass ich Änderungen daran durch.Machen Sie einen UIScrollView accesable anderen Klassen

Der Code für viewController wo scrollView

import UIKit 

public class ViewController: UIViewController , UIScrollViewDelegate { 
    @IBOutlet public weak var scrollView: UIScrollView! 

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

    self.scrollView.pagingEnabled = true 
    self.scrollView.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.scrollView.addSubview(V1.view) 
    V1.didMoveToParentViewController(self) 
    V1.view.frame = scrollView.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.scrollView.addSubview(V2.view) 
    V2.didMoveToParentViewController(self) 
    V2.view.frame = scrollView.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.scrollView.contentSize = CGSizeMake((self.view.frame.width) * 2, (self.view.frame.height))  
    } 
} 

Nun ist die scrollView in dieser Klasse möchte ich zugänglich in der Klasse Home_Screen

der Code für Home_Screen

import UIKit 

class Home_Screen: UIViewController , UIScrollViewDelegate { 

    @IBOutlet weak var nameLabel: UILabel! 
    @IBOutlet weak var slideToUnlockLabel: UILabel! 
    @IBOutlet weak var imageView: UIImageView! 
    @IBOutlet weak var shimmeringView : FBShimmeringView! 

    override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 

    let viewController = ViewController() 
    let scrollView = viewController.scrollView 

    // Add The ShimmeringView 
    shimmeringView.contentView = slideToUnlockLabel 
    shimmeringView.shimmering = true 
    shimmeringView.shimmeringBeginFadeDuration = 0.5 
    shimmeringView.shimmeringOpacity = 0.45 
    shimmeringView.shimmeringAnimationOpacity = 1.5 
    } 
} 

Antwort

0

Das definiert ist Problem ist, wie Sie das ViewC initialisieren Kinderwagen mit dem Scrollview. Du hast es aus einem Storyboard oder Xib erstellt, aber ohne es zu initialisieren. Versuchen Sie etwas wie das

let storyboard = UIStoryboard(name: "ViewController", bundle: nil) 
if let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerId") as? ViewController{ 
     let scrollview = vc.scrollview 
    } 
Verwandte Themen