有没有类似于Android for for intent的intent.putextra的方法?

时间:2016-11-10 21:56:47

标签: android ios android-intent

我正在用两种语言编写应用程序:Android& iOS,但我试图找到一种方法来使用类似的方法将数据从ViewController移动到另一个,就像Android上的Intent那样。我在Android上的代码是:

在发送数据的活动中:

  public void goToUserScreen(UserModel userM) {
        userModel = userM;
        Intent intent = new Intent(HomeActivity.this, ProfileActivity.class);
        intent.putExtra("USER_MODEL", userModel);
        intent.putExtra("LOGIN_MODEL", loginModel);
        intent.putExtra("NOTIFICATION_MODEL", notificationModel);
        startActivity(intent);
        finish();
    }

正在接收的活动:

Intent intent = getIntent();
        loginModel =            (LoginModel) intent.getSerializableExtra("LOGIN_MODEL");
        notificationModel =     (NotificationModel) intent.getSerializableExtra("NOTIFICATION_MODEL");
        userModel =             (UserModel) intent.getSerializableExtra("USER_MODEL");

有人知道如何在iOS上执行此操作吗?顺便说一句,对于iOS我在Xcode 8上进行编程,那么有人可以帮助我用Swift3解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

意图中的extras只是Parcelable值的地图。在iOS中,您可以将NSDictionary传递给UIViewController,然后以类似的效果解析它。像这样:

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.intentDictionary = [[NSDictionary alloc] init];
[self pushViewController:viewControllerB animated:YES];

然后在ViewControllerB中:

LoginModel *loginModel = [intentDictionary objectForKey:"LOGIN_MODEL"];
NotificationModel *notificationModel = [intentDictionary objectForKey:"NOTIFICATION_MODEL"];
UserModel *userModel = [intentDictionary objectForKey:"USER_MODEL"];