我正在用两种语言编写一个应用程序: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/