iPhone:从文件和 UISegmentedControl 读取文本

发布于 2024-08-21 19:09:27 字数 412 浏览 3 评论 0原文

首先,我是一个完全的初学者。

也就是说,我认为一个雄心勃勃的长期项目/学习经验是创建一个显示每日报价的应用程序,就像我们祖母浴室里的那些俗气的每日日历一样。我希望它每天有两个,每个由 UISegmentedControl 中的一个选项卡表示。这是长期的。现在我很高兴能够获得一天的报价功能。

关于问题:

  1. 如何将保存在 .txt 或 .rtf 文件中的文本显示在 UITextView 中?最好不要使用“stringWithContentsOfFile”,因为 Xcode 告诉我它已被弃用。

  2. 如何从不同的文件(或者同一文件的不同部分...?)获取内容,以便在用户点击第二个片段时显示?

如果我能让它运行以满足这两个条件并且我了解发生了什么,我将认为这一天是成功的。谢谢!

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:

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

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-08-28 19:09:27

1.

NSError *error = nil;
NSStringEncoding stringEncoding;
NSString *fileText = [NSString stringWithContentsOfFile:@"/path" usedEncoding:&stringEncoding error:&error];
myTextView.text = fileText;

error和encoding是可选的,两者都可以传入nil。但是,如果您关心错误,或者文件的编码方式,那么在创建字符串后,它们将在其中包含有用的信息。

2.

将 Interface Builder 中的 valueChanged 出口设置为控制器上的 IBAction,例如 setSegmentValue:。然后,假设您有一个引号字符串数组:

- (IBAction)setSegmentValue:(id)sender {
    UISegmentedControl *control = (UISegmentedControl*)sender;
    NSString *quote = [quotes objectAtIndex:control.selectedSegmentIndex];
    myTextView.text = quote;
}

1.

NSError *error = nil;
NSStringEncoding stringEncoding;
NSString *fileText = [NSString stringWithContentsOfFile:@"/path" usedEncoding:&stringEncoding error:&error];
myTextView.text = fileText;

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 as setSegmentValue:. Then, assuming you have an array of quote strings:

- (IBAction)setSegmentValue:(id)sender {
    UISegmentedControl *control = (UISegmentedControl*)sender;
    NSString *quote = [quotes objectAtIndex:control.selectedSegmentIndex];
    myTextView.text = quote;
}
流年里的时光 2024-08-28 19:09:27

尽管 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文