I develop project right now and I use MVVM-C Architecture pattern and Programmatically UIKit.
I use TabBarController and UINavigationController. I have a 4 tab in TabBarController:
->HomeTab
->SearchTab
->NotificationTab
->ProfileTab
Home Tab has 2 page:
->HomePage: page where the timeline appears like instagram.
->AccountPage: the page that shows another user's account when I click on a user in the timeline
SearchTab and NotificationTab is not important
ProfileTab has 2 page:
->ProfilePage:the profile page of the user of the app
->AccountPage:the page that shows another user's account when I click on my followers button.
This approach is the same on instagram. When I click on a profile in the timeline, I can see another user's page. I can also access the same user in the followers section of my own profile page.
The same pages appear in different tabbar.
My question is that when I create this accountViewController(AccountPage) on both home and profile, 2 accountViewController instances are created in memory. I am not sure if this is the right approach? Can I create a single instance of this accountViewController and access it as both homeTab and profileTab?
What is the correct way to do this? How does instagram do this?
I develop project right now and I use MVVM-C Architecture pattern and Programmatically UIKit.
I use TabBarController and UINavigationController. I have a 4 tab in TabBarController:
->HomeTab
->SearchTab
->NotificationTab
->ProfileTab
Home Tab has 2 page:
->HomePage: page where the timeline appears like instagram.
->AccountPage: the page that shows another user's account when I click on a user in the timeline
SearchTab and NotificationTab is not important
ProfileTab has 2 page:
->ProfilePage:the profile page of the user of the app
->AccountPage:the page that shows another user's account when I click on my followers button.
This approach is the same on instagram. When I click on a profile in the timeline, I can see another user's page. I can also access the same user in the followers section of my own profile page.
The same pages appear in different tabbar.
My question is that when I create this accountViewController(AccountPage) on both home and profile, 2 accountViewController instances are created in memory. I am not sure if this is the right approach? Can I create a single instance of this accountViewController and access it as both homeTab and profileTab?
What is the correct way to do this? How does instagram do this?
Share Improve this question asked 2 days ago SwiftniscateSwiftniscate 195 bronze badges 1- 1 No, you can't use only one instance of a view controller and show it in two places. Remember, a view can only have one superview and a view controller can only have one parent view controller. – HangarRash Commented 2 days ago
1 Answer
Reset to default 0If you want to use the same controller instance for any Account Page on your app you can go with this:
Use a shared Coordinator class to manage both controllers. Just make sure that your ViewModel for the controller is handling the user's data to avoid any inconsistencies.
class AccountCoordinator {
var accountVC: AccountViewController?
func loadAccount(user: User, navigationController: UINavigationController) {
if let existingVC = accountVC {
existingVC.viewModel.updateUser(user) // Update existing controller
} else {
let viewModel = AccountViewModel(user: user)
let viewController = AccountViewController(viewModel: viewModel)
accountViewController = viewController
navigationController.pushViewController(viewController, animated: true)
}
}
}
So basically whenever you want to show the account page you will run it through this coordinator.