Domanda

I'm trying to get a behavior similar to the Parse login/signup screen.

Scenario Their LoginController is presented as a modal. It has a button named signup, which when pressed, shows the signup screen as another modal with the close button. close button lets the user go back to the login screen and when the user signs up it takes the user back to main screen.

I have everything working so far with only one problem.

Question

How do I direct the user back to MainController after he signs up on the SignController?

What I've done so far

This is what I have so far, however, I'm not able to redirect the user to MainController after they signup successfully.

AppDelegate.rb

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.rootViewController = MainController.alloc.init
    @window.makeKeyAndVisible
    true
  end
end

MainController.rb

class MainController < UIViewController

  def viewDidLoad
    super
    @sessionId = NSUserDefaults.standardUserDefaults
    if @sessionId["token"] == nil
      login = LoginController.alloc.init
      login.delegate = self
      self.presentViewController(login, animated:true, completion:nil)
    end
  end

  def logged_in
    self.dismissViewControllerAnimated true, completion:nil
  end

end

LoginController.rb

class LoginController < UIViewController

  attr_accessor :delegate

  def viewDidLoad
    super
    # show username/pwd
    # show button for login
    # show button for signup
  end

  def press_login_button
    delegate.logged_in
  end

  def press_signup_button
    signup = SignupController.alloc.init
    signup.delegate = self
    self.presentViewController(signup, animated:true, completion:nil)
  end

  def signed_up
    self.dismissViewControllerAnimated true, completion:nil #close signup screen
    delegate.logged_in #close login screen
  end
end

SignupController.rb

class SignupController < UIViewController

  attr_accessor :delegate

  def viewDidLoad
    super
    # show username/pwd
    # show button for close
    # show button for signup
  end

  def press_close_button
    #go back to login controller. how?
    self.dismissViewControllerAnimated true, completion: nil
  end

  def press_signup_button
    delegate.signed_up
  end
end
È stato utile?

Soluzione

I think this can be accomplished by separating concerns a little bit more here - in particular, note that the Signup and Login controllers both assume they were presented modally. Instead, have them ask their delegate to handle the showing/hiding.

# from login/signup controller
delegate.should_hide(self)

# from main controller
def should_hide(controller)
  self.dismissViewControllerAnimated(true, completion: nil)
end

And here's the answer to your question

# from signup controller
def should_hide(controller)
  self.dismissViewControllerAnimated(true, completion: -> do
    delegate.should_hide(self)
  end)
end

This will hide each window one at a time, which you might not want... if you want to hide both windows at the same time, try this:

# from signup controller
def should_hide(controller)
  # this will ask the main controller to hide the presented controller...
  # and BOTH controllers will hide at the same time!
  delegate.should_hide(self)
end

Altri suggerimenti

I'm not able to provide you ruby code to demonstrate, but what you are looking for is called an exit or unwind segue. If you are targeting IOS 6 or later you can use the functionality.

on the controller you are going to exit to you need to setup a method to unwind. It would look something like this in objc, and then you call this from the controller you are exiting from:

- (IBAction)myExitSegue:(UIStoryboardSegue*)sender {


}

Here is a link to more info on this: http://www.freelancemadscience.com/fmslabs_blog/2012/9/24/advanced-storyboard-techniques.html

you can also check this stack overflow question here: Xcode 4.5 Storyboard 'Exit'

hope that helps.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top