iPhone:从文件和 UISegmentedControl 读取文本
首先,我是一个完全的初学者。
也就是说,我认为一个雄心勃勃的长期项目/学习经验是创建一个显示每日报价的应用程序,就像我们祖母浴室里的那些俗气的每日日历一样。我希望它每天有两个,每个由 UISegmentedControl 中的一个选项卡表示。这是长期的。现在我很高兴能够获得一天的报价功能。
关于问题:
如何将保存在 .txt 或 .rtf 文件中的文本显示在 UITextView 中?最好不要使用“stringWithContentsOfFile”,因为 Xcode 告诉我它已被弃用。
如何从不同的文件(或者同一文件的不同部分...?)获取内容,以便在用户点击第二个片段时显示?
如果我能让它运行以满足这两个条件并且我了解发生了什么,我将认为这一天是成功的。谢谢!
First off, I'm a complete beginner.
That said, I thought an ambitious longer-term project/learning experience would be to create an app that displayed daily quotes, like those cheesy day-by-day calendars our grandmothers have in their bathrooms. I want it to have two per day, each one represented by a tab in a UISegmentedControl. That's the long term. Right now I'd be happy with getting a single day's worth of quotes functioning.
Onto the questions:
How can I get text saved in a .txt or .rtf file to be displayed in a UITextView? Preferably without using 'stringWithContentsOfFile,' since Xcode is telling me that's deprecated.
How can I get content from a different file (or maybe a different portion of the same file...?) to be displayed when the user taps the second segment?
If I can get it running so that those two conditions are met and I understand what's going on, I'll consider the day a success. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1.
error和encoding是可选的,两者都可以传入nil。但是,如果您关心错误,或者文件的编码方式,那么在创建字符串后,它们将在其中包含有用的信息。
2.
将 Interface Builder 中的
valueChanged
出口设置为控制器上的 IBAction,例如setSegmentValue:
。然后,假设您有一个引号字符串数组:1.
The error and encoding are optional, and you can pass in nil for both. But if you care about the error, or what encoding the file was in they will have useful info in them after the string is created.
2.
Set the
valueChanged
outlet in Interface Builder to an IBAction on your controller, such assetSegmentValue:
. Then, assuming you have an array of quote strings:尽管
stringWithContentsOfFile:
已被弃用,但stringWithContentsOfFile:usedEncoding:error:
并未弃用。这是用于读取文件的标准方法。至于第二个问题,您只需测试分段控件的状态并根据它执行操作即可。诚然,这是一个高水平的答案,但应该可以帮助您继续下去。
Even though
stringWithContentsOfFile:
is deprecated,stringWithContentsOfFile:usedEncoding:error:
is not. That is the standard method to use for reading from files.As for the second question, you simply test the state of the segmented control and perform as action based on it. Admittedly this is a high level answer but should get you going.