2012-07-25 66 views
0

我有一个视图,其中包含一些文本字段和选择器视图和一个按钮,单击按钮时,所有这些文本框和选取器视图应该再次出现在同一视图中。当我点击按钮时,我会看到初始视图以及如何使用以前的视图加载相同的视图。在同一视图中动态添加视图

在此先感谢..

+0

你是如何创建这些文本字段和选择器。 – ArunGJ 2012-07-25 06:22:15

+0

来自IBoutlet我连接 – 2012-07-25 06:25:05

回答

0

设置按钮点击帧的添加子视图(文本字段选择器和视图),可以出现这些子视图到主视图。

否则,您可以先将它们全部放在主视图中并最初隐藏它们。然后点击按钮,将它们设置为Hidden : NO

+0

我无法得到您的答案..bcoz最初它应该出现,并在填充该文本域后,如果我再次需要相同的领域我想点击按钮说,相同的领域应该再次出现以前的字段 – 2012-07-25 06:26:34

+0

你的意思是你想让文本字段再次出现没有文字? – 2012-07-25 06:35:46

+0

s..this表示在同一视图中再次加载相同的xib,并出现前面的视图 – 2012-07-25 06:37:48

0

这里是方法:

  1. 在您的浏览器:添加一个按钮和一个UIScrollView。

  2. 创建一个独立的视图,说MyView包含所有的TextField和拾取器。 (你可以通过xib或通过代码创建)

  3. 在你的控制器的ViewDidLoad方法中,在UIScrollView上添加这个MyView。

  4. On onButtonPressed同样在UIScrollView上添加MyView。

+0

你能否详细解释一个我已经添加了所有文本框和选择器视图通过IB请告诉我一个示例代码,如果可能的话。 – 2012-07-25 06:54:08

+0

wt实际上这里子视图..我们应该声明任何东西该子视图 – 2012-07-25 07:11:22

+0

如果写入代码 - (void)viewDidLoad { [textField1 removeFromSuperview]; [textField2 removeFromSuperview]; [pickerView removeFromSuperview]; ..... } textfields r没有显示在视图中。 – 2012-07-25 07:27:09

0

如果我得到了你的问题,那么这段代码可能会帮助你。我只在按钮单击事件中添加文本字段。所以我认为它可能会让你知道另一种观点,你可以用另一种观点来做同样的事情。我正在给你.m文件,希望你能写出.h文件。这里是我的.m文件看起来像 -

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 
@synthesize btn = _btn; // IBOutlet of UIButton 
@synthesize txtFieldOne = _txtFieldOne; // IBOutlet of UITextField 
@synthesize txtFieldTwo = _txtFieldTwo; // IBOutlet of UITextField 

- (void)viewDidLoad 
{ 
    newYCoordinateForNewTxtFld = 40.0; // newYCoordinateForNewTxtFld is of type CGFloat 
    frame = self.txtFieldTwo.frame; // frame is of type CGRect 

    [super viewDidLoad]; 
} 


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return true; 

} 

// TextField Delegate Method you might required in your project 
- (BOOL)textFieldShouldReturn:(UITextField *)textField 
{ 
    [textField resignFirstResponder]; 
    return YES; 
} 

- (IBAction)btnClicked:(id)sender 
{ 

    // I am giving u a simple idea, so written simple code. You can make code more usable with ur view by adding various functions, loops, etc ,etc. 
    int tagValue = 0; 

    UITextField *newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)]; 
    newTextField.delegate = self; 
    newTextField.tag = tagValue++; 
    frame = newTextField.frame; 
    newTextField.backgroundColor = [UIColor whiteColor]; 
    [newTextField setBorderStyle:UITextBorderStyleRoundedRect]; 
    [self.view addSubview:newTextField]; 

    newTextField = nil; 
    newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)]; 
    newTextField.tag = tagValue++; 
    newTextField.delegate = self; 
    frame = newTextField.frame; 
    newTextField.backgroundColor = [UIColor whiteColor]; 
    [newTextField setBorderStyle:UITextBorderStyleRoundedRect]; 
    [self.view addSubview:newTextField]; 

} 
@end