除了有关如何保存 UITabBarController 的 Tab 键顺序的文档之外,我无法找到任何其他信息,以便保存用户的自定义以供下次应用程序启动。我在网上搜索过,但无法找到任何通过正确代码执行此操作的博客文章或文章。
我意识到我必须使用 UITabBarController (didEndCustomizingViewControllers:) 的委托方法,但我不确定如何在保存用户想要选项卡的顺序状态方面最好地实现持久性。
有人可以发布一些代码,指出我吗方向正确,或者您有已保存内容的链接? :)
谢谢
I am having problems finding any other information than the docs for how to save the tab order for my UITabBarController, so that the user's customization is saved for next app launch. I have searched online, but have been unable to find any blog posts or articles that goes through the proper code for doing this.
I realize I have to use the delegate methods for the UITabBarController (didEndCustomizingViewControllers:) but I am not sure how I best approach persistance in terms of saving the state of the order the user wants the tabs in.
Can someone post some code, point me in the right direction or perhaps you have a link for something saved? :)
Thanks
发布评论
评论(8)
至于您要求的一些示例代码,我将简单地在此处发布我如何在我的应用程序中处理相同的任务。
快速介绍:我使用 NIB 文件来存储初始
UITabBarController
状态,并且为了使我的选项卡彼此不同,我只需为分配给每个的
填充在我的UITabBarItem
对象定义标签变量UIViewControllerUITabBarController
中。为了能够准确跟踪最后选定的选项卡(包括“更多”选项卡),我为UITabBarController
和UINavigationControllerDelegate
的UITabBarControllerDelegate
实现了以下方法> 其 moreNavigationController。它们是:下面是保存选项卡顺序的代码:
如您所见,我一直将选项卡索引的顺序存储在
NSUserDefaults
的数组中。在应用程序在
applicationDidFinishLaunching:
方法中启动时,我使用以下代码重新排序了UIViewControllers
:这非常棘手,可能看起来很奇怪,但不要忘记我的
UITabBarController 完全在 nib 文件中创建。如果您以编程方式构建它,您可以简单地执行相同的操作,但遵循保存的顺序。
PS:并且不要忘记在应用终止时同步
NSUserDefaults
。我希望这会有所帮助。如果有不清楚的地方请评论并询问。
As far as you've asked for some sample code I will simply post here how I dealt with the same task in my app.
Quick intro: I was using a NIB file for storing initial
UITabBarController
state and to differ my tabs one from another I simply defined tag variables forUITabBarItem
objects assigned to eachUIViewController
stuffed in myUITabBarController
. To be able to accurately track last selected tab (including the 'More' one) I've implemented following methods forUITabBarControllerDelegate
of myUITabBarController
andUINavigationControllerDelegate
of its moreNavigationController. Here they are:And here's the code for saving the tabs order:
As you can see I've been storing the order of tabs' indexes in an array in
NSUserDefaults
.On app's launch in
applicationDidFinishLaunching:
method I reordered theUIViewControllers
using following code:It's quite tricky and may seem strange, but don't forget that my
UITabBarController
was fully created in a nib file. If you construct it programmatically you may simply do the same but following the saved order.P.S.: and don't forget to synchronize
NSUserDefaults
when your app terminates.I hope this will help. If something is not clear please do comment and ask.
首先我投票赞成之前的答案,但后来我注意到它是多么复杂。它可以而且应该被简化。
这一切都发生在 AppDelegate 中。您将 UITabBarController 的委托设置为 Interface Builder 中的 AppDelegate 实例。 SharedSettingsService 是为我保存数据的。基本上它可以是 NSUserDefaults 前端或任何你喜欢的东西(例如 CoreData)。所以一切都很简单,Interface Builder 在这里有所帮助,而不是让事情变得更复杂。
First I voted up the previous answer, but then I noticed how ridiculously complex it is. It can and should be simplified.
All this happens in AppDelegate. You set UITabBarController's delegate to AppDelegate instance in Interface Builder. sharedSettingsService is what persists the data for me. Basically it can be a NSUserDefaults front-end or anything you like (CoreData for example). So everything is simple, Interface Builder helps here, not makes things more complex.
也许是迟到了,但在学校学习 Swift 还不到两个月,而且我花了十五个小时以上的时间来学习 Swift,因为我在互联网上找不到合适的解释。
这是 Swift 中的一个解决方案
给所有 tabItem 一个标签,从 1 开始。您可以在每个单独的视图中执行此操作。如果您有六个视图,则在这种情况下,它们的 tabItem 将有一个唯一的编号,每个编号范围在 1 到 6 之间。
将 UITabBarControllerDelegate 添加到所有 ViewController 的类中,以便您可以使用稍后介绍的函数第 5 点。
添加以下全局变量 (作为示例,就在上面的代码之后),以便您可以从类中的任何函数将变量本地保存在手机上。
将 tabBarController 委托给视图,以便视图可以更新对 tabBarController 的任何更改。将以下内容放入您的viewDidLoad()中。
实现以下代码。当用户编辑选项卡视图时,此选项将激活。该代码的作用是按照[ViewControllers]的标签顺序(用户更改标签后)并将其保存在手机本地。第一个 viewController 的标记以整数形式保存在变量“0”中,第二个标记保存在名为“1”的变量中,依此类推。
打印结果会告诉您选项卡的新顺序。你不需要什么,但我认为很高兴看到后台发生的事情。所有这一切只是为了保存选项卡的顺序。现在,您必须在程序启动时检索它们。
从 UIViewControl 文件切换到 AppDelegate.swift。
最后,在 func application(application: UIApplication, didFinishLaunchingWithOptions... 中编写以下内容,您可以在 AppDelegate.swift 的顶部找到它。
tabBarController 包含的所有视图都作为数组存储在 tabBarController.viewControllers 中。
此代码基本上创建了一个名为 junkViewControllers 的数组。然后,循环根据 UIViewController 的标签,按照先前存储的变量的顺序添加程序中现有的 UIViewController。当所有这些完成后,缩短为 tabBar 的 tabBarController 将被数组 覆盖。 >垃圾ViewController。
Perhaps late to the game, but been learning Swift for less than two months at school and sat perhaps more than fifteen hours with this because I couldn't find a decent explanation on the interwebz.
Here is a solution in Swift
Give all your tabItem's a tag, starting at 1. You do this in each separate view. If you got six views, their tabItems will in that case have a unique number each ranging between 1 and 6.
Add UITabBarControllerDelegate to all the ViewControllers' classes so you can use the function explained later in point 5.
Add the following variable globally (right after the code above, as an example) so you can save variables locally on the phone from any function within the class.
Delegate the tabBarController to the view so the view can update any changes to the tabBarController. Put the following into your viewDidLoad().
Implement the following code. This one will activate when the user is editing the tab view. What the code does is taking the [ViewControllers]'s tags in the order they are in (after the user changed it) and saves it locally on the phone. The first viewController's tag is saved as an integer in the variable "0", the second tag in a variable called "1", and so on.
The prints tell you the new order of the tabs. Nothing you will need, but I think it's nice to see what's happening in the background. All this was only to save the order of the tabs. You will now have to retrieve them when the program is starting.
Switch from your UIViewControl file to AppDelegate.swift.
Finally, write the following in func application(application: UIApplication, didFinishLaunchingWithOptions... that you can find at the top of AppDelegate.swift.
What is good to know is that all views that a tabBarController contains is stored as an array in tabBarController.viewControllers.
This code basically creates an array called junkViewControllers. The for-loop then adds the existing UIViewControllers from the program in the order from the previous stored variables, based on the UIViewControllers' tags. When all this is done, the tabBarController shortened to tabBar is overwritten with the array junkViewController.
这就是我的秘诀,建立在 Rickard & 的答案的基础上。杰斯珀。所有代码都进入主 TabBarController。
This is what did the trick for me, building on the answers from Rickard & Jesper. All of the code goes into the main TabBarController.
我将解释如何以编程方式执行此操作。注意:这是使用 ARC,因此您可能必须根据需要插入保留/释放调用。
您可以使用
UITabBarItem
的tag
属性进行排序。对于要添加到UITabBarController
的每个UIViewController
,请确保每个都有唯一的标签
。大概您只会使用它们的标签的默认排序顺序,因此您原始的第一个视图控制器将是 0,然后是 1、2、3 等。
在 AppDelegate 的
didFinishLaunchingWithOptions
当它们全部实例化并添加到数组中后,您将通过查询 NSUserDefaults 来检查用户是否自定义了其订单。默认情况下,您将存储用户自定义的选项卡栏顺序的数组。这将是一个 NSNumbers 数组(最后一个代码片段中解释了如何创建它)。使用它们创建一个新的“排序”视图控制器数组并将其传递给选项卡栏控制器。如果他们没有自定义顺序,默认将返回 nil,您可以简单地使用未排序的数组。
要创建自定义排序顺序,请使用 UITabBarController 的委托方法:
I will explain how to do this programmatically. NOTE: This is using ARC, so you may have to insert retain/release calls as needed.
You use the
tag
property of theUITabBarItem
for sorting. For everyUIViewController
that you are adding to theUITabBarController
, make sure that each has a uniquetag
.Presumably you would just use their default sorting order for their tags, so whatever you have as your original first view controller would be 0, followed by 1, 2, 3, etc.
Set up your UIViewControllers in the AppDelegate's
didFinishLaunchingWithOptions
as you normally would, making sure that you are instantiating them in their "default order". As you do so, add them to an instance of a NSMutableArray.After they are all instantiated and added to the array, you will check to see if the user has customized their order by querying
NSUserDefaults
. In the defaults, you will store an array of the user's customized tab bar order. This will be an array of NSNumbers (how this is created is explain in the last code snippet). Use these to create a new "sorted" array of view controllers and pass that to the tab bar controller. If they haven't customized the order, the default will return nil and you can simply used the unsorted array.To create to customized sort order, use the UITabBarController's delegate method:
Rickard Elimää 进一步简化了答案。使用 tabBarController CustomizingViewControllers 的委托函数保存和加载自定义 ViewControllers 的“Swift”解决方案。
我就是这样做的。
Simplified Rickard Elimää answer even further. "Swift" solution to saving and loading Customized ViewControllers using the delegate function of tabBarController CustomizingViewControllers.
This is how I did it.
更新了 Swift 2.0 的答案
`
let tabBarOrderKey = "tabBarOrderKey"
}
`
请记住设置委托并在加载的视图中调用这些方法
Updated Answer for Swift 2.0
`
let tabBarOrderKey = "tabBarOrderKey"
}
`
Remember to set the delegate and call these methods in the view did load
我想分享我已经工作了近 3 天的代码,现在正在尝试各种组合,但有很多错误和失败 - 编码的一般寿命!哈哈。无论如何,下面的代码适用于 Swift 5。
这是如何:保存顺序的提取在 Sam 的代码 UITabBarController 中自定义选项卡时使用选项卡,但使用了 Swift 5 的 for 循环更新。
我的工作非常顺利,全部都在 TabBarViewController.swift 文件中。
因此,如果您只是将其粘贴到 Swift 文件中,请确保您的选项卡栏项目具有来自属性检查器的标签号,然后就可以开始了!
首先再次感谢 Sam 提供的代码。
I'd like to share the code I have been working on for nearly 3 days now trying all sorts of combinations with many errors and failure - the general life of coding! ha ha. Anyway the code below works with Swift 5.
It's the extraction of How to: Save order of tabs when customizing tabs in UITabBarController , Sam's Code, but with the for loop update for Swift 5.
I have got this working great, all in the TabBarViewController.swift file.
So if you just paste this into your Swift file, make sure your Tab Bar Items have a tag number from the Attributes Inspector and your good to go!
Thanks again to Sam for the Code in the first place.