Zita Noriega Estrada Zita Noriega Estrada - 29 days ago 10
iOS Question

Is there a method like intent.putextra of Android for iOS?

I'm programming an App in both languages: Android & iOS, but I'm trying to find a way to use a similar method to move data from a ViewController to another, like Intent on Android does it. My code on Android is:

At the Activity that is sending the data:

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


The Activity that is receiving:

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


Does someone know how to do it on iOS? By the way, for iOS I'm programming on Xcode 8, so can somebody help me to solve this with Swift3?

Answer

The extras in an intent is just a map of Parcelable values. In iOS you can pass NSDictionary to your UIViewController and then parse it with similar effect. Like so:

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

Then in ViewControllerB:

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