在 iPhone 中单击“保存”按钮设置文本字段数据

发布于 2024-11-29 20:12:13 字数 1299 浏览 3 评论 0原文

我有一个模态视图控制器,上面有两个按钮:取消和保存。有一个可编辑的 UITextField。 每当我单击“保存”按钮时,我都会保存它,但文本不会保存,因为当我单击按钮打开模态视图控制器时,文本会消失。不知道我的代码有什么问题。 这是我的代码:

- (void)viewWillAppear:(BOOL)animated {

self.cancel = self.navigationItem.leftBarButtonItem;
self.save = self.navigationItem.rightBarButtonItem;

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];

[super viewWillAppear:animated];
}

-(IBAction) cancelAction{
[[self parentViewController] dismissModalViewControllerAnimated:YES];   
  }

  -(IBAction) saveAction{
 NSString *text = [textFieldBeingEdited text];

[textFieldBeingEdited setText:text];
[self setDescription:text];


[[self parentViewController] dismissModalViewControllerAnimated:YES];   
   }

我不确定是否必须使用以下代码将文本保存在文本字段中:

- (void)textFieldDidEndEditing:(UITextField *)textField
   {
[self dismissModalViewControllerAnimated:YES];
   }

I have a modalviewcontroller and have two buttons on it: Cancel and Save. There is a UITextField which is editable.
Whenever I click on save button I do save it but the text doesnot get save because when I click on the button to open the modalviewcontroller, the text disappears. Dont know whats wrong with my code.
Here is my code :

- (void)viewWillAppear:(BOOL)animated {

self.cancel = self.navigationItem.leftBarButtonItem;
self.save = self.navigationItem.rightBarButtonItem;

UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];

UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle:@"Save" style:UIBarButtonItemStylePlain target:self action:@selector(saveAction)];
self.navigationItem.rightBarButtonItem = saveButton;
[saveButton release];

[super viewWillAppear:animated];
}

-(IBAction) cancelAction{
[[self parentViewController] dismissModalViewControllerAnimated:YES];   
  }

  -(IBAction) saveAction{
 NSString *text = [textFieldBeingEdited text];

[textFieldBeingEdited setText:text];
[self setDescription:text];


[[self parentViewController] dismissModalViewControllerAnimated:YES];   
   }

I am not sure if I have to use the following code to save the text in textfield :

- (void)textFieldDidEndEditing:(UITextField *)textField
   {
[self dismissModalViewControllerAnimated:YES];
   }

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

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

发布评论

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

评论(1

木森分化 2024-12-06 20:12:13

当视图出现时,我在您的代码中没有看到您在 UITextField 中设置文本的任何地方。您可以向 viewWillAppear 方法添加一些代码,以在视图出现时设置文本,例如:(

[textField setText:[self description]];

看起来您正在将 NSString 存储在描述变量中。)

另外,如果这是某种可以被视为用户默认值的字符串,您可以将字符串存储在那里。我以前用过这个,它很好地填充了我的 UITextField

代码示例:

-(void)viewDidLoad
{
stringValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"string"];
[textField setText:stringValue];
}

-(IBAction)saveAction
{
stringValue = [textField text];
[[NSUserDefaults standardUserDefaults] setObject:stringValue forKey:@"string"];
}

I don't see in your code anywhere that you are setting the text in the UITextField when the view appears. You can add some code to your viewWillAppear method to set the text when the view appears, such as:

[textField setText:[self description]];

(It looks like you are storing the NSString in the description variable.)

Also, if this is some kind of string that could be considered a User Default, you can store the string there. I've used this before and it populates my UITextField nicely.

Code Example:

-(void)viewDidLoad
{
stringValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"string"];
[textField setText:stringValue];
}

-(IBAction)saveAction
{
stringValue = [textField text];
[[NSUserDefaults standardUserDefaults] setObject:stringValue forKey:@"string"];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文