【问题标题】:objective-C : get individual strings for textfields created programmaticallyObjective-C:获取以编程方式创建的文本字段的单个字符串
【发布时间】:2013-10-30 11:39:14
【问题描述】:

在完成this query 的解决方案后,当我打印 mutableArray 时,我会在日志中得到输出,如下所示。

*Date Project name :* Save button : (
    Def
)

其中“Def”是在动态创建的文本字段中输入的文本。我想提取“Def”并在单击保存按钮时显示在日志中。这是代码

- (IBAction)save:(id)sender {
    for(UITextField *field in self.scroll.subviews)
    {
        if([field isKindOfClass:[UITextField class]])
        {
            if([[field text] length] > 0)
            {
                NSMutableArray *mutableTextArray = [[NSMutableArray alloc] init];
                [mutableTextArray addObject:field.text];
                NSLog(@"Save button : %@", mutableTextArray);
                //NSString *str = [str stringByAppendingString:[mutableTextArray objectAtIndex:0]];
                //[self fetchStrings:str];
            }
        }
    }
}

- (void) fetchStrings:(NSString*)enteredString
{
    NSLog("%@", enteredString); //want 'def' to be displayed here
}

【问题讨论】:

  • mutableTextArray 应该是实例级变量而不是本地变量。然后它将可以跨实例方法访问。
  • @Deepak Thakur 你的日志不可读..你能张贴截图先生吗?
  • 我通过使用这个 NSLog(@"Save button %@", mutableTextArray);

标签: ios objective-c


【解决方案1】:
- (IBAction)save:(id)sender {
    for(UIView *field in self.scroll.subviews)
    {
        if([field isKindOfClass:[UITextField class]])
        {
            if(((UITextField *)field).text.length > 0)
            {
                [mutableTextArray addObject:field.text];//mutableTextArray declare this locally in Interfacefile and then initialize the array in viewDidLoad

            }
        }
    }
    NSLog(@"%@", mutableTextArray); 
}

在创建 UITextField 时,设置标签并像这样使用。希望对你有用

【讨论】:

  • 我认为你必须在最里面的 if 条件中有一个数组。直接调用方法不起作用。
  • @DeepakThakur - (void) fetchStrings:(NSString*)enteredString;在你的接口文件中声明它会调用这个方法
  • @DeepakThakur:在 if(((UITextField *)field).text.length > 0) 内部设置断点并检查条件是否满足?
  • 删除标签==100 后生效。此外,添加 break 只会给我第一个文本字段数据。我还需要处理其余的..
  • 我需要将 fetchStrings 的参数从 nsstring 更改为 nsarray 以获取所有值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
相关资源
最近更新 更多