2012-10-25 26 views
0

我有一個數組,用於創建我的UITextField。但UITextField保持在同一幀?這是我的代碼。當你按下按鈕時它會創建一個新的UITextField。如何在不同的框架上製作UITextField?

-(IBAction)textFieldcreating{ 


    textfieldform = [[NSMutableArray alloc] init]; 
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 100, 40)]; 
    textField1.borderStyle = UITextBorderStyleRoundedRect; 
    textField1.font = [UIFont systemFontOfSize:15]; 
    textField1.placeholder = @"enter text"; 
    textField1.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField1.keyboardType = UIKeyboardTypeDefault; 
    textField1.returnKeyType = UIReturnKeyDone; 
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    textField1.delegate = self; 

    [textfieldform addObject:textField1]; 

    int counter; 
    counter = 0; 
    if(counter < [textfieldform count]) 
    { 


      textField1.frame = CGRectMake(textField1.frame.origin.x+30, textField1.frame.origin.x+40, textField1.frame.size.width+40, textField1.frame.size.height+50); 
      [self.view addSubview:textField1]; 

     counter++; 
    } 

} 

回答

0

你在做什麼是創建一個新的Array,並保持xOrigin和yOrigin同所有UITextViews!

做這樣的事情。

在.h文件中,在.m文件聲明

@property (nonatomic, retain) NSMutableArray *textfieldform; 
@property (nonatomic, readwrite) int yOrigin; 

,在viewDidLoad功能,

self.textfieldform = [NSMutableArray arrayWithCapacity:0]; 
yOrigin = 0; 

現在,你的函數看起來是這樣的。

-(IBAction)textFieldcreating{ 


    //textfieldform = [[NSMutableArray alloc] init]; NO NEED OF THIS LINE NOW 
    textField1 = [[UITextField alloc] initWithFrame:CGRectMake(10, yOrigin, 100, 40)]; 
    textField1.borderStyle = UITextBorderStyleRoundedRect; 
    textField1.font = [UIFont systemFontOfSize:15]; 
    textField1.placeholder = @"enter text"; 
    textField1.autocorrectionType = UITextAutocorrectionTypeNo; 
    textField1.keyboardType = UIKeyboardTypeDefault; 
    textField1.returnKeyType = UIReturnKeyDone; 
    textField1.clearButtonMode = UITextFieldViewModeWhileEditing; 
    textField1.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;  
    textField1.delegate = self; 

    [textfieldform addObject:textField1]; 

    yOrigin = yOrigin + 40 + 10; //old yorigin + btn height + y offset 
} 

如果你想在xOrigin變化太大,則採取另一種變量,xOrigin

+0

它不會產生任何的UITextField當我按下按鈕。我該怎麼辦? – chineseGirl

+0

@chineseGirl oops ...我忘了添加這一行..'[self.view addSubview:textField1];'現在它會工作。 – mayuur

+0

我會在哪裏放? – chineseGirl