- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在此先感谢您的帮助。我试图提高我的 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/
使用 Guake 终端这个可自定义且强大的适合各种用户的工具快速访问你的终端。 Guake 终端:GNOME 桌面中自上而下终端 Guake 是一款为 GNOME
我是 python 的新手,正在尝试运行 python 2.7 script .获得了 python 2.7 的 pip 并从 mac 终端 shell 安装了 pyCrypto 的依赖项。 我想尝试
我正在寻找一种在 Swift (macOS) 中运行终端命令的方法。我遇到了this发布,但我似乎无法获得任何解决方案。我正在尝试从我的应用程序关闭我的 mac,就像您可以从终端执行的那样(osasc
我在 macOS 上使用 bash 终端。 用户名、计算机名和文件路径占据了大部分行,所以如果我写一个长命令,我会从一行开始,然后在下一行继续。 相反,我希望行光标从用户名和计算机名下方的下一行开始。
是否有一个变量或函数可以告诉我光标的实际位置? #!/usr/bin/env perl use warnings; use 5.012; use Term::ReadKey; use Term::Ca
如何在 Mac Os X(10.6.8) 上的 gnuplot 中启用 tikz 终端? 我有工作 tikz 的 latex 。现在我从 http://www.lua.org/ 安装了 lua并下载g
我正在学习一个名为 Starting a Django 1.4 Project the Right Way 的教程,其中提供了有关如何使用 virtualenv 和 virtualenvwrapper
我正在尝试用java编写一个unix终端模拟器。我有很多麻烦。我似乎无法更改程序的工作目录,因此“cd”等命令无法正常工作。我的问题是,如果我运行一个需要用户输入的命令,有什么方法可以将该输入发送到正
我在这方面完全是个新手(Mac leopard 中的终端),我希望能从网络上获得生命线,因为我确实碰壁了。 我想在终端中以 root 身份运行脚本。该脚本保存为扩展名为 .rtf 的文本文件。我已经插
尝试在我的 osascript 命令中包含引号 ' ' 时遇到了一个奇怪的问题。 如果我尝试转义一个正常的可转义字符,它就可以正常工作。示例: osascript -e 'tell app "Find
我正在制作一个控制台 Java 应用程序,您可以在其中输入控制台命令,例如 Macintosh/Ubuntu/Windows 命令提示符上的终端,然后将其输出到日志。 我想知道,在执行系统/控制台命令
在终端中输入 mysql 命令并按回车键会换行。 但有时当我犯错时,即使用分号结束语句也无法退出此模式。 Ctrl + c 退出mysql。我怎样才能退出插入模式? 最佳答案 你必须用 ';' 结束
我正在尝试编写一个 C 代码来打开 xeyes 应用程序,然后那些眼睛在特定的时间段内不断改变其颜色.. 我尝试通过执行具有一种中心颜色的 xeyes、添加 3 秒的延迟、终止进程并在循环内打开具有另
是否有一种语法允许我在 System.out.println() 行 的同一行中读取用户的输入? 例子: What is your name?:(<-- Output) Jack (<-- In
我有一个 Wordpress 上传文件夹,该文件夹使用子文件夹构建了几个月。 wolfr2:uploads wolfr$ tree . . |-- 2007 | |-- 08 | | |-
如何从 mac 终端使用 sqlite3 找出表的列名?我忘记了我给这些列起的名字,我也不知道这些名字是怎么来的。谢谢! 最佳答案 来自 http://www.sqlite.org/sqlite.ht
我需要我的终端发送一个未使用的控制字符或转义序列,它在所有层都没有效果:被shell(bash,…)忽略,被行编辑器(readline,…)忽略,被所有应用程序(vim,less,mutt,…)忽略。
我做了一个文本编辑器,我想把它移植到 Linux 上,这样我就可以通过 SSH 远程使用它。我不太了解 Linux 终端,所以也许我遗漏了一些明显的东西,因为我简直不敢相信在 2013 年远程终端仍然
我最近想放一个 java 类文件供下载,人们可以在终端中运行它。这是一个 Minecraft 命令生成器,因此下载它的人不一定具有最大的心智能力(当然,我指的是 8 岁的 child ,他们不知道自己
我有一个文件“test.txt”,里面有一个数字列表,就像这样 1 3 4 2 3 40 312 53 243 321 423 ...etc 我还有一个可执行文件,它是一种排序算法,例如 hea
我是一名优秀的程序员,十分优秀!