android - 有没有像Android for iOS的intent.putextra这样的方法?

标签 android ios android-intent

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

在发送数据的 Activity 处:

  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();
    }

正在接收的 Activity :

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 解决这个问题吗?

最佳答案

Intent 中的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"];

关于android - 有没有像Android for iOS的intent.putextra这样的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40537695/

相关文章:

Android Intent 的节电模式( Lollipop )

java - OnClickListener 在 startActivity 上抛出 NullPointerException

java - 如何防止剪贴板监视器复制剪贴板数据两次?

ios - 如何避免将 REVERSED_CLIENT_ID 硬编码到 iOS Info.plist 中以进行开发/生产 Google 登录?

java - 打开 URL 的 Intent 不适用于一个应用程序,但适用于另一个应用程序

iphone - MPMoviePlayer 不播放视频

ios - 如何在 ios 中访问共享扩展中的 keyWindow?

使用 ActivityInstrumentationTestCase2 进行 Android 单元测试,当先前的测试用例导致启动另一个 Activity 时,getActivity() 挂起

android - W/IInputConnectionWrapper(1066) : showStatusIcon on inactive InputConnection

android - 带传递解析对象的 popBackStack