2012-05-09 54 views
3

我有一個json數組,具有某些對象:名字,姓氏,唯一的ID。我能夠解析它們並將它們保存在字典中,並檢查它們是否爲NULL條件。設置變量的按鈕標籤

我已經創建了一個表格,我可以顯示名稱。另外,我在每一行都有一個按鈕,並且我已經標記了它們。我想要的是,當我點擊按鈕時,我能夠看到我的控制檯中從服務器獲取的唯一ID。我需要傳遞這個唯一的ID到下一個視圖,因爲使用這個唯一的ID將幫助我顯示與該ID在服務器上相對應的視圖。

Click here to see what I have done so far

我已經加入這個我viewDidLoad方法做了一些改變:

for(NSDictionary *items in infos) 
     { 
      .... 
      .... 
     for(int i =0; i < len; i++) 
      { 
      ... 
      ... 
      ... 


      for(NSDictionary *newItems in infoArray) 
       { 
       ... 
       ... 

       NSString *folks_IdRcv = [[items objectForKey:strr]objectForKey:@"folks_id"]; 
       NSLog(@"FolksID = %@", folks_IdRcv); 

       NSString *folksIDString = [NSString stringWithFormat:@"%@", folks_IdRcv, nil]; 
       NSLog(@"folkid_display=%@", folksIDString); 


       if((fName && lName && email_Id && number_id) && displayName && folksIDString != NULL) 
        { 

         ... 
         ...        
         [folksIDArray insertObject:folksIDString atIndex:ii]; 
       NSLog(@"FolksID String Name = %@", [folksIDArray objectAtIndex:(ii)]); 
       ii++; 
           } 
           } 
          } 
         } 

NSMutableArray *nArray = [[NSMutableArray alloc]initWithCapacity:len]; 


for(int j =0; j<len ;j++) 
    { 

     ... 
     ... 
     [nArray addObject:[folksIDArray objectAtIndex:j]]; 
     NSLog(@"FID Element=%@", nArray); 
} 

self.infos = mArray; 
[super viewDidLoad]; 
} 

而對於這個特殊的代碼,我得到如下回應:

FID Element=(
400386528950544, 
162622322666106, 
643171434889706 
) 

現在,我有一個表格視圖,其中每行有一個按鈕,並且我能夠顯示每行中的名字和姓氏。我現在想要的是,只要點擊按鈕,該特定名稱的唯一標識應該保存在標籤中,以便我可以通過按鈕單擊操作將該標識發送到服務器。

請幫助我。

回答

2

在這裏,我認爲你可以使唯一的ID陣列的陣列和這個數組應申報.h文件或應該是類成員。比使用下面的代碼。

-(IBAction)buttonClicked1:(id *)sender{ 
    NSString *UIDString = [uidarray objectAtIndex:[sender tag]]; // uidarray is unique id array 
    NSLog(@"uid for particular button=%@", UIDString);// this will give you kid for button that you tapped or clicked. 
} 

希望,這將幫助你..

+0

你試圖告訴我,因爲我已經在nArray中捕獲了唯一的id元素,所以我應該創建一個nArray的arrayWithArray並在這之後使用上面的代碼。 – madLokesh

0

使用本UIButton* btn .....

btn.tag. 

,當按下按鈕

UIButton *button = (UIButton*)sender; 
thetatosendserver = button.tag 
1

我對你的要求,一個想法... 在TableView中的委託方法.....

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
.............your another code........ 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    CGRect frame = CGRectMake(0.0, 0.0, 14, 14); 
    button.frame = frame; // match the button's size with the image size 

    [button setBackgroundImage:image forState:UIControlStateNormal]; 

    // set the button's target to this table view controller so we can interpret touch events and map that to a NSIndexSet 
    [button addTarget:self action:@selector(checkButtonTapped:event:) forControlEvents:UIControlEventTouchUpInside]; 
    button.backgroundColor = [UIColor clearColor]; 
    cell.accessoryView = button;  
    return cell; 
} 

粘貼此方法後, ...

- (void)checkButtonTapped:(id)sender event:(id)event 
{ 
    NSSet *touches = [event allTouches]; 
    UITouch *touch = [touches anyObject]; 
    CGPoint currentTouchPosition = [touch locationInView:Yourtableview]; 
    NSIndexPath *indexPath = [Yourtableview indexPathForRowAtPoint: currentTouchPosition]; 
    if (indexPath != nil) 
    { 
     [self tableView: Yourtableview accessoryButtonTappedForRowWithIndexPath: indexPath]; 
    } 
} 

and also in accessoryButtonTappedForRowWithIndexPath tablev的委託方法iew ....

- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 

     NSMutableDictionary *item = [self.arrTypes objectAtIndex:indexPath.row];//sel.arrTypes is Array Data..use your Data here and 
     ....... do somthing here which you want ...... 
} 
+0

我一定會嘗試這個,以及如果接受的答案可以解決長期的問題...直到現在它的工作完美..也......你的答案cud被用於將來的參考...thnx一噸爲您的幫助 – madLokesh

1

標記是另一種方式是發件人超視圖。請參閱下面的代碼(不按規定設置標籤)

- (IBAction)clickedButton:(id)sender 
{ 

    UIButton *button = (UIButton *)sender; 

    UITableViewCell *cell = (UITableViewCell *)button.superview; 

    UITableView *tableView = (UITableView *)cell.superview; 

    NSIndexPath *indexPath = [tableView indexPathForCell:cell]; 

    NSString* id = [Array objectAtIndex:indexPath.row]; 
} 

,如果您使用的標籤,然後確保標籤必須是唯一的。

+0

Thnx Mangesh的有用的信息,我一定會嘗試你的方法,如果我發現Nit的方法有任何問題。我只是試用了Nit的方法和完美的工作方式,直到現在我能夠做出一些事情。 – madLokesh