didSelectRowatIndexPath - 将数据传递给另一个View Controller

时间:2014-05-06 12:25:25

标签: ios objective-c ios7 ios7.1

我很难将当前选定的TableView行传递给其他ViewController?怎么这么容易?

这是我的didSelectRowAtIndexPath

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

    WishlistItem *wish = [self.wishlistItem objectAtIndex:indexPath.row];
    // Pass these data to DetailViewController
    NSLog(@"Wish Item: %@", wish.wishlistItem);
    NSLog(@"Description: %@", wish.descWishItem);
    NSLog(@"Target Date: %@", wish.targetDate);
}

如何将这些数据传递给具有

的DetailViewController
@property (strong, nonatomic) IBOutlet UILabel *wishLabel;
@property (strong, nonatomic) IBOutlet UITextView *descriptionTextView;
@property (strong, nonatomic) IBOutlet UILabel *detailTargetDate;

我想知道在按下行时如何转到DetailViewController

这就是全部。 TIA。如果我需要提供有关我的问题的更多信息,请告诉我。感谢。

6 个答案:

答案 0 :(得分:1)

DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:dvc animated:YES];
dvc.wishLabel.text = wish.wishlistItem;
dvc.descriptionTextView.text = wish.descWishItem;
dvc.detailTargetDate.text = wish.targetDate;

答案 1 :(得分:0)

您应该为行选择定义一个seque并以prepareForSegue:方法传递数据, 你应该在Wish *类型中声明一个公共readwrite属性,并用wish命名,这样你就可以将选中的WishlistItem对象传递给DetailViewController实例。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"DetailViewSegue"]) {
        WishlistItem *wish = [self.wishlistItem objectAtIndex:self.tableView.indexPathForSelectedRow.row];
        DetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.wish = wish;
    }
}

答案 2 :(得分:0)

像这样传递变量。在DetailViewController中为变量wishtolistItem,desWichItem和targetdat创建属性。

在viewcontroller中的didSelectRowAtIndexPath方法中创建detailViewController的对象。

 DetailViewController *playviewcontroller = [[DetailViewController alloc] init];

playviewcontroller.wishlistItem.text = variableWhichtYouWantTopass1
playviewcontroller.descWishItem.text = variableWhichYouWantTopass2;
playviewcontroller.targetDate.text  =variableWhichYouWantTopass1;

 [self.navigationController pushViewController:playviewcontroller animated:YES];

答案 3 :(得分:0)

你需要做这样的事情。如果您正在使用界面构建器,则声明控制器的init部分需要更改,但我确定您可以解决这个问题。

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

    WishlistItem *wish = [self.wishlistItem objectAtIndex:indexPath.row];

    // Pass these data to DetailViewController
    DetailViewController *controller = [[DetailViewController alloc] init];

    controller.wishLabel.text = wish.wishlistItem;
    controller.descriptionTextView.text = wish.descWishItem;
    controller.detailTargetDate.text = wish.targetDate;

    [self.navigationController pushViewController:controller animated:YES];
}

答案 4 :(得分:0)

为此,您可以使用 segue 。首先,您需要在故事板中为目标视图控制器创建一个segue。为此segue指定一个标识符。然后在控制器中实现这些方法

#pragma mark - Navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
     if ([segue.identifier isEqualToString:SEGUE_IDENTIFIER]) {
         DestinationController *dest = (DestinationController *)[segue destinationViewController];
         //here you can pass the data
         dest.wishLabel.text = wish.wishlistItem;
         dest.descriptionTextView.text = wish.descWishItem;
         dest.detailTargetDate.text = wish.targetDate;
         // or you can pass the entire wish object
         dest.wish = wish;
     }
 }

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return NO;
}

只有在用户选择单元格时才需要转到目标视图控制器。这就是我们在第二个委托方法中返回NO的原因。 现在实现 didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == ROW_INDEX {
        [self performSegueWithIdentifier:SEGUE_IDENTIFIER sender:self];
        wish = [self.wishlistItem objectAtIndex:indexPath.row];
    }
}

此处, performSegueWithIdentifier 用于导航

答案 5 :(得分:0)

如果您使用的是Storyboard,请遵循以下代码。我想这会对你有帮助。

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *detailView_controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ProductID"]; // set your storyboard identifier
  detailView_controller.wishlistItem = wishLabel.text;
  detailView_controller.descItem = descriptionTextView.text;
  detailView_controller.dateItem = detailTargetDate.text;
  [self.navigationController pushViewController:detailView_controller animated:YES];
}

在DetailViewController.h类

 @property (nonatomic, retain) NSString *ProductName, *wishlistItem;
 @property (nonatomic, retain) NSString *ProductName, *descItem;
 @property (nonatomic, retain) NSString *ProductName, *dateItem;

在DetailViewController.m类

 @implementation DetailViewController
 @synthesize wishlistItem;
 @synthesize descItem;
 @synthesize dateItem;

- (void)viewDidLoad
{
    NSLog(@"wishlistItem: %@", self.wishlistItem);
    NSLog(@"descItem: %@", self.descItem);
    NSLog(@"dateItem: %@", self.dateItem);
}

我正在分享屏幕截图,用于设置storyboard的标识符。 (见右侧栏)

在故事板上选择detailsView控制器并设置标识符。

身份 - >故事板ID - >产品ID

enter image description here

我认为这会对你有所帮助.. :)