我将如何保存和加载 UITextField?

发布于 2024-12-02 12:54:50 字数 165 浏览 0 评论 0原文

我到处搜索并尝试了很多代码,但似乎没有什么对我有用。我需要做的就是加载(在 viewDidLoad 上)一个文本字段并在按下按钮时保存它。

执行此操作的最简单方法是什么?我正在使用单窗口应用程序,我没有视图控制器(这可能会有所不同?)

谢谢,

詹姆斯

I have searched everywhere and tried lots of code but nothing seems to be working for me. All I need to do is to load (on viewDidLoad) a text field and save it when a button is pressed.

What is the easiest method of doing this? I am working with a single window application, I don't have a view controller (this may make a difference?)

Thanks,

James

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

℡寂寞咖啡 2024-12-09 12:54:51

创建一个 NSMutableDictionary 作为属性,然后...

单击按钮时:

-(IBAction)buttonClicked:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];   
NSString *devicePath = [documentsDirectory stringByAppendingPathComponent:@"yourfile.txt"]; 
[self.dictionary setObject:self.textField.Text key:@"textField"];
[self.dictionary writeToFile:devicePath atomically:YES];
}

在 viewDidLoad 上,您可以通过以下方式获取文件的值:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youefile"
                                                     ofType:@"txt"];
self.dictioary = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] autorelease];

Create a NSMutableDictionary as property and ...

when your button is clicked:

-(IBAction)buttonClicked:(id)sender
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];   
NSString *devicePath = [documentsDirectory stringByAppendingPathComponent:@"yourfile.txt"]; 
[self.dictionary setObject:self.textField.Text key:@"textField"];
[self.dictionary writeToFile:devicePath atomically:YES];
}

On your viewDidLoad, you can get the value of the file by:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"youefile"
                                                     ofType:@"txt"];
self.dictioary = [[[NSMutableDictionary alloc] initWithContentsOfFile:filePath] autorelease];
欲拥i 2024-12-09 12:54:51

使用专门为此场景创建的 NSUserDefaults 类。

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

// saving a NSString (you usually do this when the value is being submitted)
[defs setObject:textField.text forKey:@"aKey"];
[defs synchronize]; //this commits the new value - shouldn't be necessary because this is doe automatically, but just in case...

// loading a NSString (you usually do this on viewDidLoad)
testField.text = [defs objectForKey:@"aKey"];

该值跨会话存储。
您也可以存储其他值类型(NSString 除外)。
对要存储值的每个字段使用不同的键。
这也可用于存储应用程序配置/选项/等

Use the NSUserDefaults class which was specifically made for this scenario.

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];

// saving a NSString (you usually do this when the value is being submitted)
[defs setObject:textField.text forKey:@"aKey"];
[defs synchronize]; //this commits the new value - shouldn't be necessary because this is doe automatically, but just in case...

// loading a NSString (you usually do this on viewDidLoad)
testField.text = [defs objectForKey:@"aKey"];

The value is stored across sessions.
You can store other value types as well (other than NSString).
Use a different key for each field for which you want to store values.
This can also be used to store app configurations/options/etc

撩动你心 2024-12-09 12:54:50

使用 SQLite 数据库:

-(IBAction) checkNotes{

sqlite3_stmt *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat: @"SELECT Notes FROM NotesTable WHERE UserID = (\"%@\")", userID.text]; 

    const char *query_stmt = [querySQL UTF8String];

    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_ROW) {

        NSString *notesField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

        Notes.text = notesField;

        [notesField release];

    }else{
        Status.text = @"Not found";

    }

    sqlite3_finalize(statement);
}
sqlite3_close(contactDB);    

}

您可以尝试一下并根据您的需要调整此代码。
要实现 SQLite3,请检查网络。但我想这对你将来会有很大帮助。我认为这将是最灵活的,因为它还允许您创建关系数据库。

Using an SQLite database:

-(IBAction) checkNotes{

sqlite3_stmt *statement;

const char *dbpath = [databasePath UTF8String];

if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
{
    NSString *querySQL = [NSString stringWithFormat: @"SELECT Notes FROM NotesTable WHERE UserID = (\"%@\")", userID.text]; 

    const char *query_stmt = [querySQL UTF8String];

    sqlite3_prepare_v2(contactDB, query_stmt, -1, &statement, NULL);

    if (sqlite3_step(statement) == SQLITE_ROW) {

        NSString *notesField = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(statement, 0)];

        Notes.text = notesField;

        [notesField release];

    }else{
        Status.text = @"Not found";

    }

    sqlite3_finalize(statement);
}
sqlite3_close(contactDB);    

}

You can play around a little bit and adapt this code to your needs.
For implementing SQLite3, check the net. But it will help you much in the future I guess. I think it would be the most flexible, because it will also allow you to create relational databases.

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