2015-10-05 40 views
0

我剛剛開始在Swift 2中編寫我的應用程序,並避免使用XIB和故事板。但是,我無法複製following feature。這正是我想要的。以編程方式創建UIContainerView

我試着創建一個UIView來執行以下使用.backgroundColor,它的工作原理,但是,我無法將其鏈接到我的UIViewControllers。只是想知道它是如何完成的?我如何鏈接我的UIView到我的UIViewController?

代碼:

let subFrame : CGRect = CGRectMake(0,screenHeight*1/2.75,screenWidth,screenHeight) 
var loginView = SignUpViewController() 

let signUpView: UIView = UIView(frame: subFrame) 
signUpView.backgroundColor = UIColor.redColor() 

//Controls what each segment does 
switch segmentView.indexOfSelectedSegment { 
case 0: 
     self.view.addSubview(signUpView) 
case 1: 
     self.view.addSubview(loginView) 
default: 
     break; 
} 

我甚至不知道,如果.view.addSubview(xxx)覆蓋/替換原來子視圖如果不是這樣。這是做到這一點的正確方法嗎?

回答

1

如果您不熟悉面向對象編程(OOP)語言(如Swift)的簡單操作,那麼不要只編寫一個應用程序。這不是如何學習編程語言的方式。當然,你可以在試驗的時候學習,但最好先從更復雜的東西開始理解本書。請閱讀Apple的Swift書中的幾頁。大多數用於iOS開發的類仍然是Objective-C包裝類(reference type,因爲頂級superClass可能是NSObject;請牢記這一點)。

這裏是你想要的代碼示例:

class ViewController: UIViewController { 

    let firstView = UIView() 
    let secondView = UIView() 
    let segmentedControlView = UISegmentedControl(items: ["firstView", "secondView"]) 

    override func viewDidLoad() { 

     super.viewDidLoad() 

     self.view.backgroundColor = UIColor.whiteColor() // we need this for the playground 

     /* setup your view here */ 
     /* add your sigment logic somewhere */ 

     self.view.addSubview(self.segmentedControlView) 
     self.view.addSubview(self.firstView) 
     self.view.addSubview(self.secondView) 

     self.segmentedControlView.frame = CGRect(x: 0, y: 20, width: self.view.frame.width, height: 44) 
     self.segmentedControlView.selectedSegmentIndex = 0 // enable the first segment 
     self.segmentedControlView.addTarget(self, action: "segmentIndexChanged:", forControlEvents: UIControlEvents.ValueChanged) 

     /* add your own frame calculation here */ 
     /* I prefer AutoLayout, but for the example static frames will be fine */ 

     self.firstView.frame.origin = CGPoint(x: 0, y: self.segmentedControlView.frame.origin.y + self.segmentedControlView.frame.height) 
     self.firstView.frame.size = CGSize(width: self.view.frame.width, height: self.view.frame.height - self.segmentedControlView.frame.origin.y) 
     // to prevent same code, we just copy the same frame from the firstView 
     // both will sit in the same place 
     self.secondView.frame = self.firstView.frame 

     /* lets add some colors so we'll see our views */ 
     self.firstView.backgroundColor = UIColor.blueColor() 
     self.secondView.backgroundColor = UIColor.redColor() 

     self.secondView.hidden = true // when intializer the secondView is not visible 
    } 

    func segmentIndexChanged(sender: UISegmentedControl) { 

     switch sender.selectedSegmentIndex { 

     case 0: 
      self.firstView.hidden = false 
      self.secondView.hidden = true 

     case 1: 
      self.firstView.hidden = true 
      self.secondView.hidden = false 

     default: 
      break; 
     } 
    } 
} 

如果你不明白的功能,應該要查找它的定義在開發者文檔。 (Like:addSubview