2017-08-12 67 views
-2

我試圖添加一個disclaimerViewController作爲最初的viewController我的應用程序,如果接受將導致用戶爲entryViewController,如果沒有,將不允許用戶使用該應用程序。但是,我希望如果用戶接受免責聲明,則在他開啓申請之後的任何時間,他將不會被呈現disclaimerViewControleler,而是entryViewController添加免責聲明Screen

我知道它對編輯AppDelegate.swift文件有一些幫助,但我不確定是否要開始。

+0

這已經被問過很多次。請在提問前進行搜索。這裏是一個示例解決方案:https://github.com/mattneub/RegistrationExample – matt

+0

你介意解釋爲什麼這被標記爲重複?您的答案和iOS項目都不支持免責聲明。 –

+0

獅子狗和西班牙狗都是狗。你需要製造一隻狗;這只是一個偶然的事實,它是什麼品種。正如我所說過的,一次性顯示視圖控制器(或直到用戶滿足某些要求)的問題在這裏已經得到了很大的處理。 – matt

回答

1

您需要保存在UserDefaults用戶選擇

下面的代碼是用斯威夫特3

如果你不希望加載entryViewController然後在AppDelegate中:

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

     //retrieve values from UserDefaults 
     //for the first time it will be false, because it was not set earlier 
     let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted") 

     if isAccepted == false { 
      //present your disclaimer here 
     }else{ 
      //show entryViewController 
     } 

     return true 
} 

也可以加載entryViewController和現在的免責聲明瞬間,然後在您的entryViewController:

override func viewDidLoad() { 
     super.viewDidLoad() 

    //retrieve values from UserDefaults 
    //for the first time it will be false, because it was not set earlier 
    let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted") 

    if isAccepted == false { 
     //present your disclaimer here  
    } 
} 

在DisclaimerVC:

@IBAction func accept(_ sender: UIButton){ 
    //it can be another action in your controller 
    //but anyway, you should save user choice after that 
    UserDefaults.standard.set(true, forKey: "isAccepted") 

    //add code here to dismiss disclaimer 
} 
0

我會這樣做的方式是將您的disclaimerViewController設置爲故事板文件中的初始視圖控制器。

在swift文件中,檢查用戶以前是否接受過viewDidLoad方法中的免責聲明,如果他們沒有,則允許代碼正常運行並顯示屏幕,如果他們已將用戶推到了您的entryViewController

如果你想設置自己的初始視圖控制器在AppDelgate而不是在故事板,這裏是一個有用的鏈接:set initial viewcontroller in appdelegate - swift

過渡到SWIFT新的視圖控制器,這裏是一個有用的鏈接:Switching view controllers in swift

0

實現此目的的最平滑方法是以編程方式切換視圖。

確保在StoryBoard中設置ViewControllers的恢復ID。

class AppDelegate: UIResponder, UIApplicationDelegate { 

    var window: UIWindow? 

    var mainViewController: ViewController? 
    var acceptViewController: AcceptViewController? 


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
     // Override point for customization after application launch. 
     let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil) 
     mainViewController = mainStoryboard.instantiateViewController(withIdentifier: "MainView") as? ViewController 
     acceptViewController = mainStoryboard.instantiateViewController(withIdentifier: "AcceptView") as? AcceptViewController 
     if mainViewController == nil { 
      print("mainViewController is nil") 
     } 
     if acceptViewController == nil { 
      print("acceptViewController is nil") 
     } 

     let isAccepted = UserDefaults.standard.bool(forKey: "isAccepted") 

     if isAccepted == false { 
      switchToAcceptViewController() 
     } else { 
      switchToMainViewController() 
     } 
     return true 
    } 

    func switchToMainViewController() { 
     //mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view 
     self.window?.rootViewController = mainViewController 
     self.window?.makeKeyAndVisible() 
    } 
    func switchToAcceptViewController() { 
     //mainViewController?.selectedIndex = 0 // only needed if the main view controller is a tab view 
     self.window?.rootViewController = acceptViewController 
     self.window?.makeKeyAndVisible() 
    } 

} 

AcceptViewController

class AcceptViewController: UIViewController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
    } 

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

    @IBAction func acceptAction(_ sender: Any) { 
     UserDefaults.standard.set(true, forKey: "isAccepted") 
     let appDelegate = UIApplication.shared.delegate as! AppDelegate 
     appDelegate.switchToMainViewController() 
    } 

} 

你可以得到完整的項目在這裏:https://github.com/ryantxr/legendary-fiesta