尝试分配除self以外的委托时解析错误

时间:2015-11-30 03:53:46

标签: ios objective-c swift parse-platform

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

PFLoginViewController类接受委托。这是我将委托的课程:

class AuthenticationController: UIViewController, PFLogInViewControllerDelegate {

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

它符合P {期待的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分配给代理人,从而使其工作正常。但我需要将登录逻辑分解为单独的类。

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

1 个答案:

答案 0 :(得分: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)
}