尝试制作iPhone应用程序并浏览我之前推荐的教程和书籍:)我正在尝试查找有关scanf /将用户输入数据从文本字段存储到变量中的信息,以后我可以使用在我的程序中。文本字段实际上是一个数字字段,所以我试图保存他们输入的整数,而不是文本,因为在我的情况下不会有任何整数。我在错误的道路上吗?任何帮助将不胜感激。
答案 0 :(得分:1)
我认为而不是scanf,你只是想从文本字段中获取值作为NSString指针。
如果在界面中使用UITextField,则可以通过将变量声明为IBOutlet并将其连接到Interface Builder中,将UITextField连接到类中的成员变量。
然后,您可以使用[UITextField变量名] .text。
将文本值作为NSString指针访问。有许多有用的函数可以使用NSStrings或将字符串转换为其他数据类型,如整数。
希望这有帮助!
答案 1 :(得分:1)
如果我基本上是在尝试 保存作为数字的输入
开头
你想要NSNumberFormatter。数据格式化程序( Apple Guide)处理字符串之间的转换以及输出格式。
答案 2 :(得分:0)
以下是如何从文本字段中获取整数的示例。
在你的.h文件中:
#include <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UITextField *myTextField;
}
@property (nonatomic, retain) IBOutlet UITextField *myTextField;
- (IBAction)buttonPressed1:(id)sender;
@end
在您的.m文件中:
#include "MyViewController.h"
@implementation MyViewController
@synthesize myTextField;
- (IBAction)buttonPressed1:(id)sender {
NSString *textInMyTextField = myTextField.text;
// textInMyTextField now contains the text in myTextField.
NSInteger *numberInMyTextField = [textInMyTextField integerValue];
// numberInMyTextField now contains an NSInteger based on the contents of myTextField
// Do some stuff with numberInMyTextField...
}
- (void)dealloc {
// Because we are retaining myTextField we need to make sure we release it when we're done with it.
[myTextField release];
[super dealloc];
}
@end
在界面构建器中,将视图控制器的myTextField出口连接到要从中获取值的文本字段。将buttonPressed1操作连接到按钮。
希望这有帮助!