【问题标题】:Dynamically insert more UITextFields动态插入更多 UITextFields
【发布时间】:2013-03-10 12:00:27
【问题描述】:

我有一个附加屏幕截图中描述的要求。单击蓝色添加按钮时,必须在最后一个文本字段和 textview 之间再插入一个UItextfield,并且必须在动态插入的新 uitextfield 旁边出现蓝色添加按钮。任何人都可以建议如何实现这一目标。我已将所有字段放在UIScrollview 中。

滚动视图未启用:

【问题讨论】:

  • 你试过了吗???
  • 检查我的答案,它会按照你的屏幕截图成功运行..

标签: iphone ios cocoa-touch uiscrollview


【解决方案1】:

你可以这样做:

在 .h 中,我有一个名为 previousTextField 的插座,它与第一个插座相连。顾名思义,它将存储最新添加的文本字段。

找到正在运行的project here

-(IBAction)addTextField:(id)sender{
    float x=previousTextField.frame.origin.x;
    float y=previousTextField.frame.origin.y+50;//get y from previous textField and add 10 or so in it.
    float w=previousTextField.frame.size.width;
    float h=previousTextField.frame.size.height;
    CGRect frame=CGRectMake(x,y,w,h);
    UITextField *textField=[[UITextField alloc] initWithFrame:frame];
    textField.placeholder = @"Enter User Name or Email";


    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.font = [UIFont systemFontOfSize:15];
    textField.autocorrectionType = UITextAutocorrectionTypeNo;
    textField.keyboardType = UIKeyboardTypeDefault;
    textField.returnKeyType = UIReturnKeyDone;
    textField.clearButtonMode = UITextFieldViewModeWhileEditing;


    [self.view addSubview:textField];
    previousTextField=textField;
}

只是一个想法/算法如何去,而不是编译器检查

我错过了,改变 + 按钮的位置。我认为你可以做到:)

编辑:在上述方法中添加以下内容

//move addbutton which is an outlet to the button
CGRect frameButton=CGRectMake(addButton.frame.origin.x, addButton.frame.origin.y+50, addButton.frame.size.width, addButton.frame.size.height);
[addButton removeFromSuperview];
[addButton setFrame:frameButton];
[self.view addSubview:addButton];

【讨论】:

  • 我收到以下错误...使用未声明的标识符 NSFrame。我是否必须导入任何包或在哪里声明 NSFrame
  • 对不起,应该是 NSMakeRect,我只是在这里输入,没有检查:p
  • 我无法查看添加的文本字段。你能看看吗
  • @iHungry,谢谢你的好回答。我的 req 略有不同..button 也应该与新创建的 uitextfiled 一起移动。但是,我能得到它..非常感谢。
  • @RamuPasupuleti:感谢 iHungry:D。代替我给他学分 rofl。
【解决方案2】:

在添加按钮上,在子视图中添加一个 textfeild 并给 textfeild 一个唯一的标签,以便帮助您区分所有 textfeilds。对于设置框架,请参考您最后一个 textfeild 框架并相应地设置 y 坐标。

UITextField *textField = [[UITextField alloc] initWithFrame:yourFrame];
textField.delegate = self;
textField.tag=yourtag;
[scrollview addSubview:textField];
[textField release];//if arc is disable

【讨论】:

    【解决方案3】:

    首先获取 UItextField 的框架。现在增加前一个 textFeild 的 yPos 和 UITetField 的新位置。并重新排列低于该 textField 的 UITextField 只需增加每个 TextField 的 yPos 。

    希望这会对你有所帮助。

    万事如意!!!

    【讨论】:

      【解决方案4】:

      试试这个 ::

      在.h文件中

      @property(nonatomic, retain) IBOutlet UIScrollView *scr;
      @property(nonatomic, retain) IBOutlet UITextField *txt;
      @property(nonatomic, retain) IBOutlet UITextView *txtVw;
      @property(nonatomic, retain) IBOutlet UIButton *btn;
      
      -(IBAction)click:(id)sender;
      

      在 .m 文件中

      int cnt;
      
      float x = 0.0, y = 0.0, w = 0.0, h = 0.0;
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          scr.delegate = self;
          scr.contentSize = CGSizeMake(0, 400);
      
          cnt = 0;
      }
      
      -(IBAction)click:(id)sender
      {
          if (cnt == 0) {
             x=txt.frame.origin.x;
             y=txt.frame.origin.y + 50;//get y from previous textField and add 10 or so in it.
             w=txt.frame.size.width;
             h=txt.frame.size.height;
          }
          else
          {
             y+=50;
          }
      
          UITextField *textField=[[UITextField alloc] initWithFrame:CGRectMake(x, y, w, h)];
          textField.borderStyle = UITextBorderStyleRoundedRect;
          textField.placeholder = @"Enter User Name or Email";
      
          [scr addSubview:textField];
      
          [btn setFrame:CGRectMake(248, y, 30, 30)];
      
          float y1 = y + 100;
          [txtVw setFrame:CGRectMake(orign_x, y1, origin_w, origin_h)];
      
          cnt++;
      }
      

      希望对你有帮助。

      谢谢。

      【讨论】:

      • 然后根据文本字段设置scrollview的contensize。
      • 连同新的文本字段,我们必须创建新按钮。当用户点击新按钮时,应该会出现另一个文本字段
      • @RamuPasupuleti:在您的屏幕上,按钮正在移动。我的代码就是以这种方式工作的。或者您可以动态添加新按钮。但我认为,这不是你的需要。
      【解决方案5】:

      查看我的代码的输出..

      您可以使用以下代码...我已经完成并且它正在按照您的屏幕截图运行.. 在你的 .h 文件中声明这个变量

            int newY,newY1;
            UIButton *btn;
      

      然后您的 .m 文件在 viewDidLoad 方法中添加以下代码

         UITextField *txt=[[UITextField alloc]init];
         txt.frame=CGRectMake(50,30,140,30);
         txt.placeholder = @"Enter User Name or Email";
         txt.borderStyle = UITextBorderStyleRoundedRect;
      
         [self.view addSubview:txt];
         newY=txt.frame.origin.y;
         btn=[UIButton buttonWithType:UIButtonTypeContactAdd];
         btn.frame=CGRectMake(250,30, 30, 30);
         [btn addTarget:self action:@selector(addtxtField) forControlEvents:UIControlEventTouchUpInside];
         [self.view addSubview:btn];
      

      & 然后创建一个 addtxtField 方法..见下文

       -(void)addtxtField
      {
      newY=newY+50;
      
      UITextField *nwtxt=[[UITextField alloc]init];
      nwtxt.frame=CGRectMake(50,newY,140,30);
      nwtxt.placeholder = @"Enter User Name or Email";
      nwtxt.borderStyle = UITextBorderStyleRoundedRect;
      [self.view addSubview:nwtxt];
      
      btn.frame=CGRectMake(250,newY, 30, 30);
      [self.view addSubview:btn];
      

      }

      它是 100% 根据您的要求运行...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-26
        • 2011-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多