在 iphone sdk 中动态创建 UITextField 时遇到问题
在我的iPhone应用程序中,我在scrollView中动态创建了一些UITextField。并在xib中添加了一个按钮。在该按钮的 TouchUpInside 事件中,我打开 UIImagePickercontroller 以打开照片库并在 UIImageView 上拍摄选定的图像。
现在,当 modalview 被关闭时,我的 UITextField 中的值就会消失。
如何保留 UITextField 值?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
使用 for 循环我将 txt 添加到 NSMutableArray fieldArray 中。
这是我从 TextFields EDIT 获取值的代码
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(@"TF : %@",tf.text);
if (tf.text.length<=0) {
tf.text=@"";
}
[insertValues addObject:[NSString stringWithFormat:@"'%@'",tf.text]];
}
: 当我在此文本字段中显示数据库中的值并尝试编辑它时。它给了我空值。
可能是什么原因?如果还有其他替代方案吗?请帮我
In my iphone app, I have created some UITextField in scrollView dynamically. And added one button in xib. On that button's TouchUpInside event, I opened UIImagePickercontroller to open photo library and taking selected image on UIImageView.
Now when modalview is dissmissed, values in my UITextField disappears.
How do I retain the UITextField values?
txt = [[UITextField alloc] initWithFrame:CGRectMake(10.0f, 30.0f, 200.0f, 30.0f)];
[txt addTarget:self action:@selector(keyDown:)forControlEvents:UIControlEventEditingDidEndOnExit];
txt.textColor = [UIColor blackColor];
txt.borderStyle = UITextBorderStyleBezel;
txt.autocorrectionType = UITextAutocorrectionTypeNo;
[fieldArray addObject:txt];
Using for loop I am adding txt to NSMutableArray fieldArray.
Here is the code where I fetch values from TextFields
NSMutableArray *insertValues = [[NSMutableArray alloc]init];
//Taking values from textFields
for (int i=1; i<=nooffields; i++) {
UITextField *tf = (UITextField *)[self.view viewWithTag:i];
NSLog(@"TF : %@",tf.text);
if (tf.text.length<=0) {
tf.text=@"";
}
[insertValues addObject:[NSString stringWithFormat:@"'%@'",tf.text]];
}
EDIT :
And also when I display values from database in this textFields and try to edit it. It gives me null values.
What may be the reason? If there any other alternatives? please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
滚动视图用于填充的信息应存储在滚动视图外部,例如 NSMutableDictionary 中。因此,每次调用
cellForRowAtIndexPath
时,它都可以从该源检索信息。因此,我建议您将 UITextField 的文本存储在字典中。您可以通过其标签号对其进行索引。这样你就不会丢失其中包含的任何内容。我希望它能帮助你
干杯
The information that your scrollview uses to get populated should be stored outside the scrollview, in a
NSMutableDictionary
for example. So everytimecellForRowAtIndexPath
gets called it can retrieve the information from that source. So I advise you to store theUITextField
's text within the dictionary. You can index it by its tag number. That way you won't loose whatever it contained.I hope it helps you
Cheers
我终于找到了解决我的问题的方法。
当调用presentModalViewController 时,我暂时将textField 的值存储到数据库中。
我将这些值检索回 viewWillAppear 上的文本字段中。
这可能不是一个非常有效的解决方案,但它对我的情况有效。
希望这对某人有帮助。
感谢您的所有回复。
I finally got a work around for my problem.
I am temporarily storing my textField's values into database when presentModalViewController is called.
These values I retrieve back into the textFields on viewWillAppear.
This may not be a very efficient solution but it worked in my case.
Hope this helps someone.
Thanks for all your responses.