使用prepareForSegue从标签获取标签/ id

时间:2014-02-23 02:07:45

标签: objective-c ios7

我有一组UILabel,并希望将所选项目的ID放入标签中。像这样:

UILabel *miII = [[UILabel alloc] initWithFrame:CGRectMake(530, 0, 25, 25)];
miII.tag=item.id;

我有以下内容,我可以设置destinationViewController的itemId属性。我遇到的问题是如何从UILabel访问标签?或者有更好的方法吗?我已经将我的经验包含在评论中,并且没有使用UITableView。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSLog(@"prepareForSegue: %@", segue.identifier);
    ItemDetailViewController *myVC = [segue destinationViewController];

    //[myVC setItemId:12]; //  <-- hard-coding this works
    [myVC setItemId:sender.view.tag];   // this doesn't work

}

- (void)tapRecognized:(UIGestureRecognizer *)sender
{
    NSLog(@"that tap was recognized with %d", sender.view.tag); // <-- this works
    [self performSegueWithIdentifier: @"ItemSegue" sender: self];


}
事先提前

1 个答案:

答案 0 :(得分:0)

sol'n相当简单,但基于其他q和a有点神秘。我做了以下事情:

@interface ListViewController ()
{
@private

int _itemId;
}

.....


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // NSLog(@"prepareForSegue: %@", segue.identifier);
    // NSLog(@"prepareForSegue: %@", sender);

    ItemDetailViewController *myVC = [segue destinationViewController];
    [myVC setItemId:_itemId];  // obviously need property for itemId on ItemDetailViewController

}

- (void)tapRecognized:(id)sender
{
    NSLog(@"that tap was recognized with %d", [(UIGestureRecognizer *)sender view].tag);
    _itemId=[(UIGestureRecognizer *)sender view].tag;

在ItemDetailiViewController.h ...

@interface ItemDetailViewController : UIViewController

@property (nonatomic) int itemId;