gpt4 book ai didi

ios - 类似终端的应用程序 : UItextFied vs UITextView

转载 作者:行者123 更新时间:2023-11-29 10:47:58 25 4
gpt4 key购买 nike

在此先感谢您的帮助。我试图提高我的 Objective-c 学习曲线,并用很多案例挑战自己。我尝试做一个简单的应用程序来模拟终端 session 的行为:第一步:提示正在等待,我输入第一个命令:例如。日期。然后我得到一个结果。第二:提示在结果下方再次等待。然后我给出第二个命令:时间等等

我用 UItextField 做了很多测试来输入不同的文本和命令,用 UITextView 来显示结果。我还使用 NSMutable 数组存储所有输入/结果。没有什么能很好地工作。我想就此事征求您的意见,并希望您指出最好的方法或代码源来学习重现终端 gui。数组是不是一个好的解决方案,如何将textField 放在textView 的末尾等等?谢谢+

最佳答案

这只是您想要实现的目标的一般方法。

使用单个 UITextView 进行输入和输出。

首先,向您的 UITextView 添加一个简单的字符,例如“>”,以便用户在该字符后开始输入。

实现此 UITextView 委托(delegate)方法以在用户点击“返回”时进行监听:

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

if([text isEqualToString:@"\n"]) {

// Handle what happens when user presses return

return NO;
}

return YES;
}

我在这里要做的是当用户按下回车键时,获取整个 UITextField 的内容,并使用类似 NSArray *stringArray = [_textField.text componentsSeparatedByString: @"> "];。这样,数组的最后一个元素就是用户输入的最后一个命令。然后,测试命令并将适当的答案附加到 UITextView。不要忘记在它后面添加@"\n>",以便提示用户一个新的命令行。

这里剩下要做的是防止用户删除你的“>”。

这是一个想法,可能还有许多其他方法可以实现。如果您需要有关某事的更多详细信息,请发表评论!


剧透警告:完整代码

在我的 Storyboard中,我只是将 UITextView 链接到 ViewController.h,名称为 textView。请注意,以下代码不处理用户从 UITextView 中删除文本的操作。您可以通过在控制台中键入“hello”来测试代码。

ViewController.m :

#import "ViewController.h"

@interface ViewController () {
// Store supported commands and outputs
NSDictionary *commands;
}

@end

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

// Initialize textView
_textView.text = @">";
[_textView becomeFirstResponder];

// Init supported commands with associated output
commands = [NSDictionary dictionaryWithObjectsAndKeys:@"Hello World !", @"hello", nil];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {

// Deleting something
if([text isEqualToString:@""]) {

UITextPosition *beginning = textView.beginningOfDocument;
UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
UITextPosition *end = [textView positionFromPosition:start offset:range.length];
UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];

NSString *textToReplace = [textView textInRange:textRange];

NSLog(@"%@", textToReplace);

if ([textToReplace isEqualToString:@">"]) return NO;

return YES;
}

if([text isEqualToString:@"\n"]) {

// Handle what happens when user presses return
NSArray *stringArray = [_textView.text componentsSeparatedByString: @">"];
NSLog(@"Last command : %@", [stringArray lastObject]);
[self handleCommand:[stringArray lastObject]];

return NO;
}

return YES;
}

-(void)handleCommand:(NSString*)command {

NSString *output = [commands objectForKey:command];
// If an unsupported command was typed
if (output == nil) {
output = @"Unknown command";
}

// Write output to the textView
_textView.text = [_textView.text stringByAppendingString:@"\n"];
_textView.text = [_textView.text stringByAppendingString:output];
_textView.text = [_textView.text stringByAppendingString:@"\n>"];
}

这是来自模拟器的 textView 的内容:

>hello
Hello World !
>yes
Unknown command
>
Unknown command
>hello
Hello World !

关于ios - 类似终端的应用程序 : UItextFied vs UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21733250/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com