2013-02-11 49 views
0

我正在嘗試創建將數字輸入標籤的鍵盤接口。您可以更輕鬆地通過鍵盤輸入您的4位數字引腳進行輸入,就像打開鎖定的iOS設備時一樣。如何引用特定標籤

我有四個標籤,它們會顯示一次輸入的數字。我有12個包含數字的按鈕(加上一個取消和刪除按鈕)。每個按鈕都有一個標籤分配給它。 單擊該按鈕時,我想將該標籤作爲文本輸入到下一個空標籤。

我需要做的是引用setPin函數中的下一個空標籤。我是否以這種錯誤的方式去做,或者只是有一點我缺少?下面是我的代碼的修剪版本。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[self view] setBackgroundColor:[UIColor whiteColor]]; 
    [self setCurrField:1]; 

    // BUILD A VIEW TO CONTAIN THE LOGIN PANEL 
    UIView *pinView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 400, 500)]; 
    pinView.backgroundColor = [UIColor lightGrayColor]; 

    // CREATE BUTTONS 
    int nPos = 1; 
    int leftPos; 
    int topPos = 134; 

    while (nPos <= 12) 
    { 
     leftPos = (nPos%3 == 1) ? 19 : leftPos + 113 + 12; 
     topPos = (nPos%3 == 1) ? topPos + 61 + 10 : topPos; 

     // BUILD THE BUTTONS AND ADD TO THE PAGE 
     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [btn setTag:nPos]; 
     [btn addTarget:self action:@selector(setPin:) forControlEvents:UIControlEventTouchUpInside]; 
     [btn setFrame:CGRectMake(leftPos, topPos, 113, 61)]; 
     [btn setBackgroundColor: [UIColor darkGrayColor]]; 

     NSString *btnLabel; 
     if(nPos <= 9) 
     { 
      btnLabel = [NSString stringWithFormat:@"%i", nPos]; 
     } 
     else 
     { 
      switch (nPos) { 
       case 10: 
        btnLabel = @"Cancel"; 
        break; 

       case 11: 
        btnLabel = @"0"; 
        break; 

       case 12: 
        btnLabel = @"Delete"; 
        break; 

       default: 
        break; 
      } 
     } 
     [btn setTitle: btnLabel forState:UIControlStateNormal]; 

     [pinView addSubview:btn]; 

     nPos++; 
    } 

    // CREATE THE TEXTBOXES 
    UILabel *char1Label = [[UILabel alloc] initWithFrame:CGRectMake(19, 65, 83, 91)]; 
    UILabel *char2Label = [[UILabel alloc] initWithFrame:CGRectMake(113, 65, 83, 91)]; 
    UILabel *char3Label = [[UILabel alloc] initWithFrame:CGRectMake(207, 65, 83, 91)]; 
    UILabel *char4Label = [[UILabel alloc] initWithFrame:CGRectMake(301, 65, 83, 91)]; 

    [pinView addSubview:char1Label]; 
    [pinView addSubview:char2Label]; 
    [pinView addSubview:char3Label]; 
    [pinView addSubview:char4Label]; 

    [[self view] addSubview:pinView]; 

} 

-(void)setPin:(UIButton *)sender 
{ 
    if(sender.tag <= 9 || sender.tag == 11) 
    { 
     [--charLabel[self currField]Label-- setText:[sender tag]]; 
     [self currField++]; 
    } 
    elseif(sender.tag == 12) 
    { 
     [self currField--]; 
     [--charLabel[self currField]Label-- setText:@""]; 
    } 
} 

回答

0

這樣做的一種方法是將每個標籤放入一個NSMutableArray(以及您的視圖)。然後獲得在setPin每個標籤,你可以這樣做:

UILabel *label = [labelArray objectAtIndex:currField]; 
[label setText:[NSString stringWithFormat:@"%i", sender.tag]]; 
currField++; 
+0

我總是留下深刻的印象通過這個論壇回答問題的速度。謝謝你們的幫助。我認爲將標籤放入數組中的概念對我來說是最有意義的,所以我認爲我會嘗試。 – Typhoon101 2013-02-11 16:45:51

1

您需要穩守引用到textLabels,也許作爲一個屬性,你是班上

@property (nonatomic) NSArray* textLabels; // Choose a more descriptive name than this 

... 

self.textLabels = @[ char1Label, char2Label, char3Label, char4Label ]; 


... 

- (UILabel*) nextEmptyLabel { 

    for (UILabel* label in self.TextLabels) 
    { 
     NSString* trimmedText = [label.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; 
     if ([trimmedText length] == 0) { 
      return label; 
     } 
    } 

    return nil; // No non empty label found 
} 
+0

可能想修改你的長度檢查 - '[[label.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]' – 2013-02-11 16:39:24

相關問題