2012-07-25 25 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