2016-04-02 109 views
0

我想以編程方式彈出視圖,但遇到問題。以編程方式彈出視圖

import UIKit 

class CreatePostViewController: UIViewController { 

    var isAnimating: Bool = false 
    var dropDownViewIsDisplayed: Bool = false 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     createDescriptionField() 
     createLabels() 
     createInputFields() 
     createBackButton() 

    } 
    //More code omitted 

    func createBackButton(){ 
     let button = UIButton() 
     button.setTitle("back", forState: .Normal) 
     button.setTitleColor(UIColor.blueColor(), forState: .Normal) 
     button.frame = CGRectMake(200,200,100,50) 
     button.backgroundColor = UIColor.redColor() 
     button.addTarget(self, action: "goBack:", forControlEvents: .TouchUpInside) 
     self.view.addSubview(button) 

    } 

    func goBack(sender: UIButton){ 
     navigationController?.popToRootViewControllerAnimated(true) 

    } 

} 

我補充這個類從另一個類是這樣的:

import UIKit 

class HomeViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     needCashButton() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func needCashButton(){ 

     let image = UIImage(named: "GlossyRoundedButton.png") as UIImage? 
     let button = UIButton() 
     button.setTitle("Do Job", forState: .Normal) 
     button.setTitleColor(UIColor.blueColor(), forState: .Normal) 
     button.setBackgroundImage(image, forState: .Normal) 
     button.frame = CGRectMake(225,200,100,50) 

     button.addTarget(self, action: "cashButtonPressed:", forControlEvents: .TouchUpInside) 
     self.view.addSubview(button) 

    } 


    func cashButtonPressed(sender: UIButton){ 
     let findJob = CreatePostViewController() 
     self.presentViewController(findJob, animated: false, completion: nil) 
    } 

} 

我讀了建議我剛剛流行與線CreatePostViewController許多其他職位:navigationController?.popToRootViewControllerAnimated(true)但是,這並不做任何事情的時候我點擊按鈕。我在這裏忽略了什麼?

回答

1

如果你的UIViewController是在UINavigationViewController(或它模態呈現),使用此予以駁回它在一個UINavigationViewController裏面,使用這個來解僱它:

self.navigationController.popViewControllerAnimated(true) 
+0

這樣做謝謝你 – Anderology

0

僅當您實際使用導航控制器時,navigationController才起作用:)。如果你有

self.dismissViewControllerAnimated(true, completion: nil) 

:所以我會建議使用這樣的

let vc = UINavigationController(rootViewController: CreatePostViewController()) 
self.presentViewController(vc, animated: false, completion: nil)