使用 NSDictionary 计算 UITextField 中的单词
简而言之,我的问题是,如果用户输入一个单词,例如“mvg”,我希望文本字段将该单词注册为数字,以便我可以用它进行计算,我该怎么做?
我制作了一个这样的字典:
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt: 20], @"mvg",
[NSNumber numberWithInt: 15], @"vg",
[NSNumber numberWithInt: 10], @"g", nil];
现在我需要字典来注册用户输入并将单词翻译成数字。有人知道我该怎么办吗?
In short, my question is, if the user types a word for example "mvg" I want the textfield to register the word as a number, so I can calculate with it, how do I do that?
I made a dictionary like this:
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt: 20], @"mvg",
[NSNumber numberWithInt: 15], @"vg",
[NSNumber numberWithInt: 10], @"g", nil];
Now I need the dictionary to register the users typing and translate the words into the numbers. Anyone know how I should do?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要设置视图控制器和视图。您已经有了文本字段和按钮(视图)。现在您需要让按钮在按下时执行操作。如果您已在 Interface Builder 中设置了 UI,则应该能够简单地将按钮的“Touch Up Inside”出口连接到控制器中的操作。如果您以编程方式执行此操作,则需要将按钮的操作和目标设置为适当对象(通常是视图控制器)上的适当方法。然后,该方法将从其他文本字段(应该是对象中的出口,应该是视图控制器)读取值并进行计算。
You need to set up a view controller and the views. You already have the text fields and buttons (the views). Now you need to make the button perform an action when pressed. If you've set the UI up in Interface Builder, you should be able to simply connect the "Touch Up Inside" outlet of the button to your action in the controller. If you're doing it programmatically, then you need to set the action and target for the button to the appropriate method on the appropriate object (usually the view controller). That method will then read the values from the other text fields (which should be outlets in the object, which should be the view controller) and do your calculations.