最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - How to use same UIViewController instance in different Tab in UITabBarController - Stack Overflow

programmeradmin2浏览0评论

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
Add a comment  | 

1 Answer 1

Reset to default 0

If 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.

发布评论

评论列表(0)

  1. 暂无评论