0

I am creating an ios app with swift. I have an UIViewController and an "UserManager" class which inherits from NSObject. Basically, when the user opens the app, the code within UIViewController is executed. UIViewController calls a function in UserManager. When this function has finished, I want that a segue be performed (a segue called "connectSegue" in my code). I am a beginner...

What I have done (and didn't work!) :

Here is my ViewController :

import UIKit

var profileUser = UserManager()

class ViewController : UIViewController{

override func viewDidLoad() {login()}

func login()->Void {profileUser.createUser()}

func handleSegue()->Void {self.performSegueWithIdentifier("connectSegue",sender: self)}

}

Here is my UserManager class :

import Foundation

class UserManager: NSObject {

//Some properties

func createUser()->Void {

//Some code

var segueHandler = ViewController()
segueHandler.handleSegue()
}
}

How can I do that? Thank you

soling
  • 303
  • 1
  • 3
  • 11

1 Answers1

1

The problem is that your UserManager is creating a new instance of ViewController, which is not the one that called UserManager.

A better way to handle this is to call performSegueWithIdentifier in your ViewController, after calling login.

In general, you are better off not putting UI navigation inside non-UI classes. UserManager will be more useful if it does user functions only, not UI responsibilities. If UserManager is doing its work asynchronously, then you should consider using notifications to communicate back to your ViewController when the work is complete so it can perform the segue.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • I see, thank you for your answer. It was my first shot, to call performSegueWithIdentifier after login(), but it didn't work because login() hadn't the time to finish his part of work. So I wanna see how to manage it working with notifications. – soling Oct 28 '14 at 14:06
  • Check out http://stackoverflow.com/questions/2191594/send-and-receive-messages-through-nsnotificationcenter-in-objective-c – Mike Taverne Oct 28 '14 at 14:27