2012-05-09 127 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