迅速

2016-03-31 13 views
1

別のクラスからIBOutletを取得、私はScrollViewを持っていますが、私は取得しようとするならば、それはビューで迅速

コードnilを返し、メインのViewControllerにフックアップクラスにavaliableであるクラスMain_ScreenでIBOutletを持っていますコントローラ

import UIKit 

class ViewController: UIViewController , UIScrollViewDelegate { 

    @IBOutlet weak var scrollVieww: UIScrollView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.scrollVieww.pagingEnabled = true 
     self.scrollVieww.delegate = self 

     let storyboard = UIStoryboard(name: "Main", bundle: nil) 
     if let vc = storyboard.instantiateViewControllerWithIdentifier("MainScreen") as? Main_Screen { 
      // imageview returns nil :(
      let imageView = vc.avatarImageView 
     } 

     // 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)) 

} 
+0

私はあなたが 'vc'で何かをしているところは見ません。スクロールビューに追加することを意味しましたか? 2番目のMainScreenを後で割り当てると(v2)、別のインスタンスになるので、imageViewは設定されず、どこにも 'main_screen'にアクセスしません。 – Paulw11

+0

UiScrollViewデリゲートメソッドを呼び出して、できないimageViewをアニメーションしたいクラスMain_Screen @ Paulw11で –

答えて

4

短い回答:しないでください。別のView Controllerのビューをプライベートとして扱う必要があります。

別のView ControllerのUIを操作する必要がある場合は、UIの変更を要求してVC内のコードに変更を加えるために使用するpublicメソッドを追加します。

これはどちらも優れたデザインで、他のView Controllerのビューがまだ作成されていないため、それらはゼロであり、失敗するか、「オプションのアンラップを試みたときに遭遇しました。