开发者

Making an alternative to UITabBarController

开发者 https://www.devze.com 2023-02-07 17:12 出处:网络
I\'m making a custom UIViewController, which is similar to a UITabBarController, as there are some buttons which switch between views. However I\'m unsure whats the best way to swit开发者_如何学Cch th

I'm making a custom UIViewController, which is similar to a UITabBarController, as there are some buttons which switch between views. However I'm unsure whats the best way to swit开发者_如何学Cch the views:

  1. Have a UIView in the nib file, and add/remove the viewController's views as subviews, as they are needed.

  2. Have a UIView in the nib file (as an IBOutlet), and replace the UIView with the viewController's view so that they are subviews of the myTabBarController's view directly.

  3. Don't have a UIView in the nib, and programmatically set the frame of the viewControllers as they are added, so they are subviews of the myTabBarController's view directly


I had to do something similar once, and in my case it was simpler to have my master "switching" view (for lack of a better term) maintain a list of UIViewControllers. That way, I was able to maintain the state of the child view controllers even when the corresponding view was not visible or had even been destroyed (to save memory, for example), which made it simpler to keep track of the info on each "page". In my approach, I simply programmatically added each UIViewController to the switch view. Basically your approach #3.

That said, there's nothing wrong with your approaches #1 and #2. They'll do the job. The only thing I don't particularly like about #1 is that it doesn't scale as easily, since you've statically set which views are the children of your switcher at compile time, and cannot easily change that at runtime.


I'm using this approach from Red Artisan's Marcus Crafter. It works remarkably well.

0

精彩评论

暂无评论...
验证码 换一张
取 消