2012-10-24 136 views
0

好簡單的問題,但無法找到答案..物業保留IOS

我需要我的視圖 - 控制傳遞給方法「saveinfo」被按下按鈕時,被稱爲類,內如何使viewcontroller對「saveinfo」方法可見,所以我可以在那裏使用它?

好吧,我會添加全班。基本我需要按下按鈕時獲取文本字段信息。但我無法訪問我的saveinfo方法中的textFields和TableControll變量。

@implementation Settings 

- (id)init: (TableViewController*) TableControll { 
    NSMutableArray *textFields = [[NSMutableArray alloc] initWithCapacity:5]; 
    UITextField *textField = nil; 

    for (int i = 0; i < 3; i++) { 
     textField = [[UITextField alloc] initWithFrame:CGRectMake(0.0f, 0.0f+(i*35), 120.0f, 30.0f)]; 
     textField.backgroundColor = [UIColor whiteColor]; 
     [textField setBorderStyle:(UITextBorderStyleRoundedRect)]; 
     [TableControll.view addSubview:textField]; 

     [textFields addObject:textField]; 
     [textField release]; textField = nil; 
    } 
    UITextField *textName = textFields[0]; 
    textName.placeholder = @"Vardas"; 

    UITextField *textNo = textFields[1]; 
    textNo.placeholder = @"Telefonas"; 
    textNo.keyboardType = UIKeyboardTypeNumberPad; 
    UITextField *textPin = textFields[2]; 
    textPin.keyboardType = UIKeyboardTypeNumberPad; 
    textPin.placeholder = @"Pin"; 

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    button.frame = CGRectMake(150, 20, 160, 30); 
    [button setTitle:@"Advanced settings" forState:UIControlStateNormal]; 
    [TableControll.view addSubview:button]; 
    UIButton *save = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
    save.frame = CGRectMake(150, 60, 160, 30); 
    [save setTitle:@"Save settings" forState:UIControlStateNormal]; 
    [TableControll.view addSubview:save]; 
    [button addTarget:self action:@selector(goAdvanced) 
    forControlEvents:UIControlEventTouchUpInside]; 
    [save addTarget:self action:@selector(saveInfo) 
    forControlEvents:UIControlEventTouchUpInside]; 

    return self; 
} 

-(void)goAdvanced { 
    AppDelegate *newControll = (AppDelegate*)[UIApplication sharedApplication].delegate; 
    [newControll ChangeController]; 
} 

-(void)saveInfo { 

    for(int i=0;i<5;i++) { 
     UITextField *tempTxtField=[_textFields objectAtIndex:i]; 
     NSLog(@"do it %@",tempTxtField.text); 
    } 

} 

@end 
+3

給我們一些更多的信息:什麼是應用程序的結構? 控制器A - > B類或它們在2類中不相關? 你是什麼主控制器?它保留你需要的控制器嗎? –

回答

1

只需添加NSMutableArray爲伊娃到類:

@implementation TableControll { 
    NSMutableArray *_textFields; 
} 

- (id)init: (TableViewController*) tableControll { 
    _textFields = [[NSMutableArray alloc] initWithCapacity:5]; 

    //init the textfields 
    //and add it as subview 
} 

// skipping the rest of the implementation 

-(void)saveInfo { 
    for(int i=0;i<5;i++) { 
     UITextField *tempTxtField=[_textFields objectAtIndex:i]; 
     NSLog(@"do it %@",tempTxtField.text); 
    }     
} 
@end 

,你應該檢查是否可以重用UITextFields或必須重新初始化的NSMutableArray每次。看起來你不使用ARC,但你不應該寫[textField release]; textField = nil;之類的東西。你想釋放對象來減少計數器,但不要將它設置爲nil(dealloc除外)。

+0

謝謝!我會嘗試這個明天,看看它是否有效。 – Datenshi

+0

謝謝!一切都如預期般運作。 – Datenshi