0

I work with Xcode Swift 7 and 2. I want to know how to execute an action before viewDidLoad my initial view. In fact I would like to change the initial view based on a parameter (if I am logged in or not) ... Should we do it in the AppDelegate? Thank you

7
  • 1
    you can add your code into didFinishLaunchingWithOptions method in AppDelegate Commented Sep 25, 2015 at 5:02
  • And to change the initial view? I can not seem to change with the id of the controller ... It is in the StorageId need to identifier ? Commented Sep 25, 2015 at 5:05
  • So you want to initiate specific view from appdelegate? Commented Sep 25, 2015 at 5:06
  • Yes you need identifier then.and correct your question title. Commented Sep 25, 2015 at 5:07
  • but I can not ... He does not recognize my id ... Commented Sep 25, 2015 at 5:08

2 Answers 2

1

This way you can initiate specific viewController from appdelegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { self.window = UIWindow(frame: UIScreen.mainScreen().bounds) let storyboard = UIStoryboard(name: "Main", bundle: nil) let initialViewController = storyboard.instantiateViewControllerWithIdentifier("yourID") self.window?.rootViewController = initialViewController self.window?.makeKeyAndVisible() return true } 

Assign your storyboard ID this way:

Click on your storyboard then go to Identity Inspector at right side and give a storyboard id as shown in below image:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but he return "NSExeption, no controller with the identifier "MyId" "
yes because you are giving incorrect identifier to storyboard.
it is in the field need to StoryboardId ID? then check the checkbox below?
Don't check the checkbox ?
0
let mainStorybord : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)          Bool isAlreadyLogin = <...code for to check for already logged in or not...>         if  isAlreadyLogin         { var homeVC = storyboard.instantiateViewControllerWithIdentifier("<homeVC>") as! UIViewController // HERE <homeVC> will be your identifier for your sugue in storyboard for initial view after login.             let nav : UINavigationController = UINavigationController(rootViewController: homeVc)             self.window?.rootViewController = nav         }         else         { var loginVC = storyboard.instantiateViewControllerWithIdentifier("<loginVC>") as! UIViewController // HERE <loginVC> will be your identifier for your sugue in storyboard for login view             let nav : NavigationViewController = UINavigationController(rootViewController: loginVC)             self.window?.rootViewController = nav         } self.window?.makeKeyAndVisible() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.