【问题标题】:UIViewController instance issueUIViewController 实例问题
【发布时间】:2016-05-13 05:46:00
【问题描述】:

我正在尝试在单独的 swift 文件 (Login.swift) 上设置 NSUserDefaults 变量以确定我的用户是否已登录。如果用户确实已登录,则 LoginViewController 将消失,而 HomeViewController 将显示。但是我被抛出了这个错误

在“UIViewController”类型上使用实例成员“dismissViewControllerAnimated”;您的意思是改用“UIViewController”类型的值吗?

这是我的代码

//success
else if value["username"] != nil && value["password"] == nil && value["message"] == nil
{
 NSUserDefaults.standardUserDefaults().setBool(true, forKey: "isUserLoggedIn")

 LoginViewController.dismissViewControllerAnimated(true, completion:nil)

 if let api_key = value["api_token"].string
 {
    print("The token is " + api_key)
 }
 else
 {
    print("error parsing api token")
 }
 //pass data to Users class
 _ = Users.init(Name: value["name"].string, Email: value["email"].string, Id: value["id"].int, ProfilePicture: value["profile_picture"].string, Username: value["username"].string)
 }

【问题讨论】:

  • dismissViewControllerAnimated 是和对象方法,为什么你用类名来调用它..?

标签: ios uiviewcontroller swift2


【解决方案1】:

创建 LoginViewController 的对象。 使用该对象然后调用dismissViewControllerAnimated 函数。 不允许从类名直接调用

【讨论】:

    【解决方案2】:

    dismissViewControllerAnimated 它的 Instance 方法不是 class 方法。看起来你是在上课打电话。

    如果你在同一个控制器上使用:

    [topVC dismissViewControllerAnimated:YES completion:nil];
    

    如果您不在该控制器中,请使用

     UIViewController *topVC =  [UIApplication sharedApplication].keyWindow.rootViewController.presentedViewController;
     [topVC dismissViewControllerAnimated:YES completion:nil];
    

    【讨论】:

      猜你喜欢
      • 2014-04-19
      • 1970-01-01
      • 2013-04-27
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 2019-06-16
      • 2011-04-10
      • 2011-10-23
      相关资源
      最近更新 更多