Stack在這個問題上有很多問題,但他們都沒有解決我遇到的問題。iOS 8 Swift 1.2 and Parse - 嘗試在ViewController上呈現UIAlertController,它的視圖不在窗口層次結構中
我使用ParseUI作爲我的應用程序的登錄和註冊部分。我想像發生的是UIAlertController
當用戶(例如)沒有在用戶名和密碼字段中輸入任何文本時顯示。
這裏是我的MasterViewController
代碼:
class MasterViewController: UIViewController, PFLogInViewControllerDelegate,
PFSignUpViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if (PFUser.currentUser() == nil) {
var logInViewController = LoginViewController()
logInViewController.delegate = self
var signUpViewController = SignUpViewController()
signUpViewController.delegate = self
logInViewController.signUpController = signUpViewController
self.presentViewController(logInViewController, animated: true, completion: nil)
}
}
func logInViewController(logInController: PFLogInViewController,
shouldBeginLogInWithUsername username: String, password: String) -> Bool {
if (!username.isEmpty || !password.isEmpty) {
return true
} else {
let alertController = UIAlertController(title: "Failed to login.",
message: "Login Failure. Please try again.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
self.presentViewController(alertController, animated: true, completion: nil)
return false
}
}
func logInViewController(logInController: PFLogInViewController,
didFailToLogInWithError error: NSError?) {
println("Failed to log in.")
}
func signUpViewController(signUpController: PFSignUpViewController,
shouldBeginSignUp info: [NSObject : AnyObject]) -> Bool {
if let password = info["password"] as? String {
return count(password.utf16) >= 8
} else {
return false
}
}
func signUpViewController(signUpController: PFSignUpViewController,
didFailToSignUpWithError error: NSError?) {
println("Failed to sign up.")
}
func logInViewController(logInController: PFLogInViewController,
didLogInUser user: PFUser) {
let installation = PFInstallation.currentInstallation()
installation["user"] = PFUser.currentUser()
installation.saveInBackground()
self.dismissViewControllerAnimated(true, completion: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func signUpViewControllerDidCancelSignUp(signUpController:
PFSignUpViewController) {
println("User dismissed signup.")
}
}
閱讀這似乎像它會是回答其他用戶的回答後,我添加了下面的類,以我的工作區:
import Foundation
class AlertHelper: NSObject {
func showAlert(fromController controller: MasterViewController) {
var alert = UIAlertController(title: "abc", message: "def", preferredStyle: .Alert)
controller.presentViewController(alert, animated: true, completion: nil)
}
}
而且然後相應地修改我的方法:
func logInViewController(logInController:PFLogInViewController, shouldBegin LogInWithUsername用戶名:字符串,密碼:字符串) - >布爾{
if (!username.isEmpty || !password.isEmpty) {
return true
} else {
let alertController = UIAlertController(title: "Failed to login.",
message: "Login Failure. Please try again.", preferredStyle: .Alert)
let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
alertController.addAction(defaultAction)
var alert = AlertHelper()
alert.showAlert(fromController: self)
return false
}
}
在這一點上,我不太清楚自己能做什麼。有一個想法是以編程方式將UILabel
添加到我的LoginViewController
和SignUpViewController
,並根據用戶登錄和註冊的錯誤(或缺少)更改內容,但我希望發出警報。
編輯:這是我的LoginViewController
中的代碼。它被分類爲了定製外觀。我的SignUpViewController
的代碼幾乎完全相同。
進口的UIKit 進口解析 進口ParseUI
class LoginViewController: PFLogInViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = ckPurple
let label = UILabel()
label.textColor = UIColor.whiteColor()
label.text = "Welcome."
label.sizeToFit()
logInView?.logo = label
// Do any additional setup after loading the view.
}
您的回答非常接近。相反,它是'loginController.presentViewController(alertController,animated:true,completion:nil)'(在註冊的情況下,它是'signUpController',因爲我必須在委託方法中引用'PFLoginViewController'和PFSignupViewController'。 – damianesteban
好的,我把'loginViewController'改爲'loginController'。很高興我的回答對你有幫助,請考慮標記爲正確。 – johnpatrickmorgan