2015-06-20 62 views
18

我重建而不故事板的應用程序,並且我具有最麻煩被編程導航視圖到視圖中的一部分。很少有東西寫在那裏不使用故事板,所以找到答案是困難的。夫特 - 實例化而不故事板導航控制器中應用代表

我的問題很簡單。我有我的ViewController和我的SecondViewController,我想從前者推到後者。

AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
    // Override point for customization after application launch. 

    window = UIWindow(frame: UIScreen.mainScreen().bounds) 
    window?.backgroundColor = UIColor.whiteColor() 
    window?.rootViewController = ViewController() 
    window?.makeKeyAndVisible() 

    return true 
} 

然後在ViewController.swift

class ViewController: UIViewController, AVAudioPlayerDelegate, UITextFieldDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     startFinishButton.setTitle("Begin", forState: .Normal) 
     startFinishButton.addTarget(self, action: "moveToSecondViewController", forControlEvents: .TouchUpInside) 
     view.addSubview <*> startFinishButton 
    } 

    func moveToSecondViewController(sender: UIButton) { 
     let vc = SecondViewController() 
     println(self.navigationController) // returns nil 
     self.navigationController?.pushViewController(vc, animated: true) 
    } 
} 

印刷self.navigationController返回nil。我試着這樣做:

var navController = UINavigationController()ViewController類創建(但viewDidLoad中之外,對下類聲明),並使用navController VAR進行推送,但是這並沒有奏效。

我想也許該解決方案是在應用程序委託,以創建導航控制器,我想整個應用程序將使用一個全局變量?

我希望這篇文章可以成爲許多人誰是新的雨燕,並希望從他們的應用程序中刪除故事板。

感謝您的關注和幫助。

回答

20

在夫特3

將此代碼放入AppDelegate類中的didFinishLaunchingWithOptions方法內。

window = UIWindow(frame: UIScreen.main.bounds) 
let mainController = MainViewController() as UIViewController 
let navigationController = UINavigationController(rootViewController: mainController) 
navigationController.navigationBar.isTranslucent = false 
self.window?.rootViewController = navigationController 
self.window?.makeKeyAndVisible() 
+0

你有什麼自定義的navigationController? – user7097242

16

在AppDelegate中

var window: UIWindow? 
var navController: UINavigationController? 

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 

    navController = UINavigationController() 
    var viewController: ViewController = ViewController() 
    self.navController!.pushViewController(viewController, animated: false) 

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 

    self.window!.rootViewController = navController 

    self.window!.backgroundColor = UIColor.whiteColor() 

    self.window!.makeKeyAndVisible() 

    return true 
} 

在的ViewController

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.title = "FirstVC" 

    var startFinishButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton 
    startFinishButton.frame = CGRectMake(100, 100, 100, 50) 
    startFinishButton.backgroundColor = UIColor.greenColor() 
    startFinishButton.setTitle("Test Button", forState: UIControlState.Normal) 
    startFinishButton.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside) 

    self.view.addSubview(startFinishButton) 
} 

func buttonAction(sender:UIButton!) 
{ 
    println("Button tapped") 
    let vc = SecondViewController() 
    self.navigationController?.pushViewController(vc, animated: true) 
} 

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


} 
+0

爲什麼你將viewcontroller設置爲uiviewcontroller而不是我的第一個視圖控制器ViewController.swift? –

+0

@ZackShapiro,我編輯了我的答案。我用這段代碼在Xcode中創建了一個例子,它可以工作。 – agy

+0

謝謝,當我回到我的機器時會試一試 –

相關問題