2017-05-18 24 views
-1

我正在用swift 3製作一個應用程序,並且讓應用程序有多個變量。我希望我的應用程序能夠測試某個變量何時處於某個值,無論您在哪個視圖中移動到新的視圖控制器。我知道我可以使用segues,但有很多觀點會變得混亂。有什麼我可以使用,只是使用代碼或簡單得多。 謝謝強制移動到一個新的視圖控制器(沒有segue)

+1

您的變量的值將在其條件和變量的值保持相同的所有viewControllers? –

+0

是的變量將是相同的,並在所有視圖更新 –

回答

0

您可以使用屬性的didSet並呈現視圖控制器。

class TestVC : UIViewController { 
    var variable: Int = 10 { 
     didSet { 
      if variable == 20 { 
       present(someVC, animated: true) 
      } 
     } 
    } 
} 

class SomeVC : UIViewController { 
    func didFinishWhatever() { 
     dismiss(animated: true) 
    } 
} 
+0

當我嘗試代碼,我把視圖控制器名稱在目前位說它「不能將類型'TestViewController.type'的值轉換爲期望的參數類型'UIViewController' 「 –

0

可以使UIViewControllerextension,並在那裏,只要您的變量的值變化簡單的推/出示您的需要ViewController

0

如果您使用的是故事板嘗試的幾行代碼:

self.window = UIWindow(frame: UIScreen.mainScreen().bounds) 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let initialViewController = storyboard.instantiateViewControllerWithIdentifier("SomeVCIdentifier") 
self.window?.rootViewController = initialViewController 
self.window?.makeKeyAndVisible() 
相關問題