默认选择器值 iPhone
有没有办法为选择器设置默认值?我保存所有选择器中最后选定的行,并且希望能够让选择器在启动时加载这些保存的行。截至目前,我已经找到了这段代码:
[settingsPagePicker selectRow:3 inComponent:0 animated:YES];
它有效,但仅当用户点击选择器时才有效。我需要它在应用程序首次加载时工作。如果我将此代码放在 viewDidLoad 中,应用程序将崩溃。 有人知道在我的代码中放置它的正确位置以使其工作吗?
谢谢您的宝贵时间!
Is there a way to set a default value for a picker? I save the last selected row from all the pickers and I want to be able to have the pickers load those saved rows at start up. As of now I have found this code:
[settingsPagePicker selectRow:3 inComponent:0 animated:YES];
It works but only when the user taps the picker. I need it to work when the app is first loaded. If I put this code at viewDidLoad the app will crash.
Anyone know where the proper place to put this in my code to make it work?
Thank you for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用 -selectRow:inComponent:animated 之前,您是否检查了 settingsPagePicker 的数据源,以确保该索引处有可用数据(示例代码中的 3)?
您如何为数据源加载数据?您可以先在 viewDidLoad 中初始化数据源,然后在知道有可用数据后调用 selectRow。
更新:您的代码应该如下所示或类似的内容:
Have you checked your data source for the settingsPagePicker before calling -selectRow:inComponent:animated to make sure there is data available at that index (3 in your sample code)?
How are you loading your data for your data source? You can initialize your data source in viewDidLoad first and then call selectRow once you know there is data available.
UPDATE: Here is what your code should look like or something like it:
这是我课堂上的一些方法。
picker.dataSource 是 self
Here some methods from my class.
picker.dataSource is self