【问题标题】:Parse error when trying to assign a delegate other than self尝试分配除 self 以外的委托时解析错误
【发布时间】:2015-11-30 03:53:45
【问题描述】:

我在尝试使用 PFLoginViewController (Parse) 时遇到一个奇怪的错误。

PFLoginViewController 类接受一个委托。这是我将委派给的课程:

class AuthenticationController: UIViewController, PFLogInViewControllerDelegate {

    func logInViewController(logInController: PFLogInViewController, shouldBeginLogInWithUsername username: String, password: String) -> Bool {
        return true
    }
}

它符合 Parse 所期望的 PFLogInViewControllerDelegate。这是我分配委托并调用登录的控制器:

class MyAccountTableViewController: UITableViewController {
    @IBAction func handleLogInTap(sender: UIButton) {
        let loginViewController = PFLoginViewController()
        loginViewController.delegate = AuthenticationController()
        self.presentViewController(loginViewController, animated: true, completion: nil)
    }
}

尝试登录时,PFLoginViewController.m (_loginAction) 中会运行以下内容:

NSString *username = _logInView.usernameField.text ?: @"";
NSString *password = _logInView.passwordField.text ?: @"";

if (_delegateExistingMethods.shouldBeginLogIn) {
    if (![_delegate logInViewController:self shouldBeginLogInWithUsername:username password:password]) {
        return;
    }
}

如果我po _logInView.usernameField 我得到正确的回复:

<PFTextField: 0x106c1a480; baseClass = UITextField; frame = (0 252; 375 44); text = 'myusername'; clipsToBounds = YES; opaque = NO; gestureRecognizers = <NSArray: 0x10b61cad0>; layer = <CALayer: 0x10ad0eb60>>

但是,如果我在设置之后po username

error: Couldn't materialize: couldn't get the value of variable username: variable not available Errored out in Execute, couldn't PrepareToExecuteJITExpression

password 也是如此。监视列表显示为nil

我相信这与指定另一个类作为委托而不是使用self 有关。我可以通过使MyAccountTableViewController 符合PFLogInViewControllerDelegate 然后将self 分配给代表来使其工作。但我需要将该登录逻辑分解为一个单独的类。

我被困住了。有什么想法吗?

【问题讨论】:

  • 什么是_loginView - AuthenticationController 类的另一个实例?

标签: ios objective-c swift parse-platform


【解决方案1】:

这一行的问题是您创建了类AuthenticationController,并且它在函数handleLogInTap 的末尾被释放。

loginViewController.delegate = AuthenticationController()

你需要在你的课堂上引用它MyAccountTableViewController,它应该很好。

private var authentication = AuthenticationController()

@IBAction func handleLogInTap(sender: UIButton) {
    let loginViewController = PFLoginViewController()
    loginViewController.delegate = authentication
    self.presentViewController(loginViewController, animated: true, completion: nil)
}

【讨论】:

  • 我认为它的生命周期是直到范围结束
  • @IslamQ。是啊谢谢。你说的对。我将编辑答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多