2017-04-12 54 views
0

我有一個MainViewController我在加載滑出菜單。 MainViewController包含一個靜態的TopViewContentView,其中加載了不同的子控制器或視圖,具體取決於所選的菜單條目。UIButton不工作加載在一個孩子ViewController

其中一個孩子的按鈕被加載,但它不工作。 當我用包含按鈕的ViewController啓動應用程序時,設置爲「是初始視圖控制器」,一切工作正常。

當使用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

也容易你的方法的小寫第一個字母,像'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) 
} 

,改變你的代碼將目標添加到

addButton.addTarget(self, action: #selector(FirstViewController.touchUpAddButton(_:)), for: .touchDown) 
addButton.addTarget(self, action: #selector(FirstViewController.touchDownAddButton(_:)), for: .touchDown) 
+0

和發件人將是可以更改的按鈕,而不是「addButton」 – muescha

+0

那是對的,你可以將AddButton更改爲touchUp和touchDown函數內的發件人。 – 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中有兩個視圖(這是我想要添加我的FirstViewController和SecondViewController的地方)。或者我不明白你的解決方案? – Attila

+0

你的ContentView是一種UIContainerView嗎?爲了替換使用UIContainerView的MainViewController中的視圖,它將完美地代替UIView – Neha

相關問題