2016-07-31 73 views
0

我可以成功地將第一個UIView嵌入到容器中,只需將容器拖到新的UIView中,但是要添加轉換視圖,我無法用鍵入事件的新視圖替換當前的嵌入視圖。Swift嵌入UIView在主UIViewController容器 - 如何從1嵌入UIView轉換到下一個嵌入UIView

這裏是我的代碼如下:

import Foundation 
import UIKit 

class MainViewController: UIViewController { 

@IBOutlet var overlayView: UIView! 

@IBOutlet var logoImage: UIImageView! 

@IBOutlet var overlayUIView: UIView! 

private var embeddedViewController: ViewController! 
private var birthdayPickerController: BirthdayPickerController! 

required init(coder aDecoder: NSCoder) { 
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 

    self.birthdayPickerController = storyBoard.instantiateViewControllerWithIdentifier("birthdayPickerController") as! BirthdayPickerController 

    super.init(coder: aDecoder)! 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    view.backgroundColor = UIColor(patternImage: UIImage(named: "lasvegas.jpg")!) 

    overlayView.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) 

    self.modalPresentationStyle = .OverCurrentContext 

    logoImage.image = UIImage(named: "philia"); 

    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 

    self.embeddedViewController = storyBoard.instantiateViewControllerWithIdentifier("viewController") as! ViewController 


    self.embeddedViewController.setOverlayUIView(overlayUIView) 
} 



let viewControllerIdentifiers = ["viewController", "birthdayPickerController"] // storyboard identifiers for the child view controllers 

/* 
@IBAction func didChangeValue(sender: UISegmentedControl) { 

    let newController = (storyboard?.instantiateViewControllerWithIdentifier(viewControllerIdentifiers[sender.selectedSegmentIndex]))! as UIViewController 

    let oldController = childViewControllers.last! as UIViewController 

    oldController.willMoveToParentViewController(nil) 
    addChildViewController(newController) 
    newController.view.frame = oldController.view.frame 

    transitionFromViewController(oldController, toViewController: newController, duration: 0.25, options: .TransitionCrossDissolve, animations:{() -> Void in 
     // nothing needed here 
     }, completion: { (finished) -> Void in 
      oldController.removeFromParentViewController() 
      newController.didMoveToParentViewController(self) 
    }) 
} 
*/ 
} 


import UIKit 
import Foundation 

class ViewController: UIViewController, UITextFieldDelegate{ 

/* 
* Reference to the container in MainViewController 
* This is where all views are rendered 
*/ 
private var overlayUIView: UIView! 

@IBOutlet var backgroundImageView: UIView! 

@IBOutlet var anotherTextLabel: UITextView! 

@IBOutlet var textField: UITextField! 

func setOverlayUIView(overlayView: UIView) { 
    overlayUIView = overlayView 
} 

func textFieldShouldReturn(textField: UITextField) -> Bool { 
    self.view.endEditing(true) 
    onEnter(textField) 
    return false 
} 

@IBAction func onEnter(sender: UITextField) { 
    let msgEntered = textField.text 

    anotherTextLabel.text = "Hi \(msgEntered!)!" 


    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil) 

    let birthdayPickerController = storyBoard.instantiateViewControllerWithIdentifier("birthdayPickerController") as! BirthdayPickerController 

    birthdayPickerController.name = textField.text! 

    overlayUIView.willRemoveSubview(self.view) 
    overlayUIView.addSubview(birthdayPickerController.view) 

    /* 
    self.willMoveToParentViewController(nil) 



self.parentViewController?.addChildViewController(birthdayPickerController) 

    birthdayPickerController.view.frame = self.view.frame 

    transitionFromViewController(self, toViewController: birthdayPickerController, duration: 0.25, options: .TransitionCrossDissolve, animations: {() -> Void in}, completion: {(finished) -> Void in 
      self.removeFromParentViewController() 
      birthdayPickerController.didMoveToParentViewController(self.parentViewController) 
    }) 
*/ 

     //self.navigationController?.pushViewController(thirdViewController, animated: true) 

     //self.parentViewController!.performSegueWithIdentifier("birthdayPickerController", sender: self) 

} 

override func viewDidLoad() { 
    super.viewDidLoad() 

    UIColor.blackColor().colorWithAlphaComponent(0).CGColor 

    view.backgroundColor = UIColor.blackColor().colorWithAlphaComponent(0.3) 

    self.textField.delegate = self; 

} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
} 

/* 
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) { 
    if segue.identifier == "birthdayPickerController" { 
     let birthdayPickerController = segue.destinationViewController as! BirthdayPickerController 

     birthdayPickerController.name = textField.text! 
    } 
} 
*/ 

} 

回答

0

我解決了這個問題。我使用委託來將父代傳遞給嵌入式控制器,並讓子控制器使用新的控制器類對父代進行回調,其中父類將隨後將新控制器的舊控制器換出。

相關問題