我正在尝试获得类似于 Parse 登录/注册屏幕的行为。
场景
他们的 LoginController
以模态形式呈现。它有一个名为 signup
的按钮,按下该按钮时,会将 signup
屏幕显示为另一个带有 close
按钮的模式。 close
按钮让用户返回登录屏幕,当用户注册时,它会将用户带回主屏幕。
到目前为止,我一切正常,只有一个问题。
问题
在用户注册 SignController
后,我如何将用户引导回 MainController
?
到目前为止我做了什么
这是我目前所拥有的,但是,在用户注册成功后,我无法将用户重定向到 MainController
。
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
主 Controller .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
登录 Controller .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
注册 Controller .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
最佳答案
我认为这可以通过在这里更多地分离关注点来实现 - 特别要注意,注册和登录 Controller 都假定它们是模态呈现的。相反,让他们要求他们的代表处理显示/隐藏。
# from login/signup controller
delegate.should_hide(self)
# from main controller
def should_hide(controller)
self.dismissViewControllerAnimated(true, completion: nil)
end
这是你问题的答案
# from signup controller
def should_hide(controller)
self.dismissViewControllerAnimated(true, completion: -> do
delegate.should_hide(self)
end)
end
这将一次隐藏一个每个窗口,您可能不希望这样做...如果您想同时隐藏两个窗口,试试这个:
# 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
关于ios - 如何创建像 Parse 这样的登录/登录 iOS 屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22485955/