2017-04-12 5 views
0

MainViewController私はスライドアウトメニューをロードしています。 MainViewControllerには、選択されたメニューエントリに応じて、異なる子コントローラまたはビューがロードされるスタティックTopViewContentViewが含まれています。UIButtonが子に読み込まれないViewController

子供の1人の中にボタンがありますが、それは動作していません。 ボタンを含むViewControllerでアプリケーションを起動すると、「初期View Controllerです」に設定されていると、すべて正常に動作します。

ロードされた子を持つ最初のView ControllerとしてMainViewControllerのアプリを起動すると、ボタンが機能しません(ビューに読み込まれても反応しません)。 どのように私はこの作品を作ることができますか?子ビューの

MainViewControllerロード

class MainViewController: UIViewController, SideBarDelegate { 
var contentView = UIView() 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    .... 
    contentView.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1) 
    contentView.translatesAutoresizingMaskIntoConstraints = false 
    self.view.addSubview(contentView) 
    contentView.topAnchor.constraint(equalTo: statusBarView.bottomAnchor).isActive = true 
    contentView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor).isActive = true 
    contentView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor).isActive = true 
    contentView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor).isActive = true 
    contentView.layoutIfNeeded() 

} 

func sideBarDidSelectButtonAtIndex(_ index: Int) { 
    ...  
    if index == 0{ 
     statusBarLabel.text = "First" 
     let controller:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController 
     controller.view.frame = ContentView.bounds 
     controller.willMove(toParentViewController: self) 
     contentView.addSubview(controller.view) 
     controller.didMove(toParentViewController: self) 
     print("touched index 0") 
    } else if index == 1{ 
     // index is 1 
    } 
} 

FirstViewController

class FirstViewController: UIViewController, UIScrollViewDelegate { 

    let addButton = UIButton() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.view.backgroundColor = UIColor(red: 230/255, green: 230/255, blue: 230/255, alpha: 1) 

     addButton.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 60) 
     addButton.clipsToBounds = true 
     addButton.setTitleColor(UIColor.white, for: .normal) 
     addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1) 
     addButton.setTitle("add feed", for: .normal) 
     addButton.titleLabel?.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightRegular) 
     self.view.addSubview(AddButton) 
     addButton.layoutIfNeeded() 
     addButton.addTarget(self, action: #selector(touchUpAddButton), for: UIControlEvents.touchUpInside) 
     addButton.addTarget(self, action: #selector(touchDownAddButton), for: UIControlEvents.touchDown) 

    func touchUpAddButton() { 
     addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1) 
    } 

    func touchDownAddButton() { 
     addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1) 
    } 
} 
+0

のためにしてください使用:また、クラス定義 – muescha

+0

は異なる、それは簡単に最初の文字小文字のVARの名前を使ってインスタンスに名前を付けます'TouchUpAddButton'から' touchUpAddButton'のようなあなたのメソッドの小文字の最初の文字 – muescha

+0

それ以外はあなたのコードを読みにくいです(クラスのメソッドや静的関数を呼び出すと分かりません....) – muescha

答えて

0

これら

func TouchUpAddButton(sender: UIButton){ 
    addButton.backgroundColor = UIColor(red: 102/255, green: 204/255, blue: 255/255, alpha: 1) 

} 

func TouchDownAddButton(sender: UIButton) { 
    addButton.backgroundColor = UIColor(red: 70/255, green: 174/255, blue: 253/255, alpha: 1) 
} 

にあなたの機能を更新し、あなたのコードを変更します

+0

と送信者は 'addButton'の代わりに変更できるボタンになります – muescha

+0

TouchUpおよびtouchDown関数内で送信側にAddButtonを変更できます。 – Devster101

+0

まだ動作していません。コンパイル中にエラーが表示されます。未解決の識別子 'TouchUpAddButton'の使用 – Attila

0

コンテナにビューを追加するときに、addSubview関数を使用する必要はなく、FirstViewControllerの機能は動作していません。

コードの下

用途:あなたが時間を変更することができます

let newViewController:FirstViewController = storyboard!.instantiateViewController(withIdentifier: "First") as! FirstViewController 
let oldViewController = childViewControllers.last! as UIViewController 
    oldViewController.willMove(toParentViewController: nil) 
    addChildViewController(newViewController) 
    transition(from: oldViewController, to: newViewController, duration: 0.25, options: .transitionCrossDissolve, animations:{() -> Void in 
     // nothing needed here 
    }, completion: { (finished) -> Void in 
     oldViewController.removeFromParentViewController() 
     newViewController.didMove(toParentViewController: self) 
    }) 

は、それがアニメーション

+0

私は、私のMainViewController、TopViewとContentView(これは私がFirst-とSecondViewControllerを追加したい場所です)に2つのビューがあるので、サブビューとして追加する必要があります。それとも、私はあなたの解決策を得られないのでしょうか? – Attila

+0

ContentViewはUIContainerViewのようなものですか? MainViewControllerのビューをUIContainerViewで置き換えると、UIViewの代わりに完全に機能します – Neha

関連する問題