-1

i have an issue. I would like to change the view controller in swift.

This is a part of my code:

if success == "1" {
    NSLog("Login SUCCESS");

    var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
    prefs.setObject(mobile, forKey: "USERNAME")
    prefs.setInteger(1, forKey: "ISLOGGEDIN")
    prefs.synchronize()

    self.presentViewController(OtpVC(), animated: true, completion: nil)
}

my OtpVC file is:

class OtpVC: UIViewController {
    @IBOutlet weak var smsfield: UITextField!

    @IBAction func continueButton(sender: AnyObject) {
    }



    }

The problem now is that when is login successful the page change and goes all black!

How i can fix that? Thanks in advance.

ABakerSmith
  • 22,759
  • 9
  • 68
  • 78

3 Answers3

2

The page in blank is an indicator of that your view has not being loaded. And it seems like that's the case:

Please take a closer look, you are creating an instance of that class, but you are not instantiating the view of it.

In swift you can have single classes affecting several views in your storyboards.

The correct way, should be:

if success == "1" {
 NSLog("Login SUCCESS");

 var prefs:NSUserDefaults = NSUserDefaults.standardUserDefaults()
 prefs.setObject(mobile, forKey: "USERNAME")
 prefs.setInteger(1, forKey: "ISLOGGEDIN")
 prefs.synchronize()

 var storyboard = UIStoryboard(name: "Main", bundle: nil)

"Main" here should be the name of the storyboard in which the OtpVC view is.

 var controller = storyboard.instantiateViewControllerWithIdentifier("OtpVC") as! OtpVC

"OtpVC" here should be the storyboard identifier of your view

 self.presentViewController(controller, animated: true, completion: nil)

}

Update:

Another root cause may be that your identifier is not well set at storyboard.

At your Identity Inspector settings should be similar to:

enter image description here

Here in Module, like I have no such Class on my targets it says None.

This could be something you may want to have a look at. Maybe in your storyboard you are referencing other target than the one with such OptVC class implementation.

Community
  • 1
  • 1
Hugo Alonso
  • 6,684
  • 2
  • 34
  • 65
  • @VaultDesign I'm glad to help!, please up-vote all the answers you think were useful in this thread... and always, this keeps people motivated and more likely to answer your questions in the future ;-) – Hugo Alonso May 11 '15 at 18:59
1

You can also use segues.

Set a segue from one view controller to the one where you want to shift in the storyboard. Also set the identifier of the segue in storyboard.

Now use below code

self.performSegueWithIdentifier("identifier", sender: self)
1
self.presentViewController(OtpVC(), animated: true, completion: nil)

With the line above, you'll create a new instance of OtpVC programmatically. It's not a problem if that's what you want and the view controller is coded properly to be initialized like that.
But because it's all black I assume you've created OtpVC in storyboard so you'll either need to use a segue like Sukhdeep Singh Kalra has suggested or instantiate it with:

let destination = storyboard?.instantiateViewControllerWithIdentifier("identifier") as! OtpVC
presentViewController(destination, animated: true, completion: nil)

Both options, you'll have to set an identifier.

To set an identifier for the latter, go to storyboard and click on your view controller. Make sure the view controller is selected by clicking the left yellow button on top of your view controller. Then in identity inspector, type in the identifier name and replace "identifier" in my example with that name.

Eendje
  • 8,815
  • 1
  • 29
  • 31
  • there is one error, it say : UIStoryboard does not have a member named mainStoryboard – VaultDesign May 11 '15 at 17:35
  • sorry, i used my own code.. let me fix that. Fixed. – Eendje May 11 '15 at 17:35
  • The app crash : Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key txtUsername.' – VaultDesign May 11 '15 at 17:41
  • Where does this error points at? Did you set up the view controller properly? Some more information about your error in this [post](http://stackoverflow.com/questions/3088059/this-class-is-not-key-value-coding-compliant-for-the-key) – Eendje May 11 '15 at 17:44
  • when i hit "Signup"button it works the if statement that say that login is correct, than i have the problem – VaultDesign May 11 '15 at 17:46
  • it points at @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { – VaultDesign May 11 '15 at 17:46
  • Did you set your view controller's class as `OtpVC`? – Eendje May 11 '15 at 17:48
  • You should read [this](http://stackoverflow.com/questions/3088059/this-class-is-not-key-value-coding-compliant-for-the-key). It's about the error you just had. Could be a few things like the one's I've mentioned before or your connections aren't set up correctly, i.e. pointing to old objects. – Eendje May 11 '15 at 17:52
  • Before you change the view controller, can you check the storyboard by adding the code: `println(storyboard)` and check if it's not nil? I've updated the answer to make it more clear. – Eendje May 11 '15 at 18:00
  • the console doesn't show it, it show only the error – VaultDesign May 11 '15 at 18:04
  • It seems your `OtpVC` view controller still has a connection to `txtUsername`, but `OtpVC` doesn't have that IBOulet. Did you have one before and deleted it? – Eendje May 11 '15 at 18:10
  • No, never :\ txtUsername have a connection on LoginVC – VaultDesign May 11 '15 at 18:13
  • `'[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key txtUsername.'` Have you checked the connections in "Connections Inspector"? – Eendje May 11 '15 at 18:19
  • i solve the issue, i delete a segue, anyway, the storyboard on console say is = Optional() but the view doesn't appear. – VaultDesign May 11 '15 at 18:26
  • Yea I thought so, thats why I've edited my answer. Read it again, you assign a "destination" and pass it to `presentViewController(_:)` – Eendje May 11 '15 at 18:29
  • Let me know if you succeeded :) – Eendje May 11 '15 at 18:43