簡體   English   中英

屬性保留在iOS中

[英]Property retain in ios

好的簡單問題,但是找不到答案。

我需要將我的viewcontroller傳遞給在按下按鈕時調用的類中的方法“ 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

只需將NSMutableArray作為ivar添加到您的類中:

@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; [textField release]; textField = nil; 您想釋放對象以減少計數器,但不要將其設置為nil (在dealloc中除外)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM