如何将indexpath.row移动到新的视图控制器

时间:2014-05-25 11:14:27

标签: ios objective-c

我正在尝试将此indexPath.row移至视图控制器pageView

所以这就是我的尝试:


首先从相应的按钮

获取indexPath行
[cell.nav addTarget:self action:@selector(naviguate:) forControlEvents:UIControlEventTouchUpInside];
    cell.nav.tag=indexPath.row;

在表格单元格内有一个按钮nav


然后我将导航到pageView

-(void)naviguate:(id)sender {
    [UIView animateWithDuration:0.5
                          delay:0
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{

                         [_tableView setFrame:CGRectMake(0, 569, _tableView.frame.size.width, _tableView.frame.size.height)];
                     }
                     completion:^(BOOL finished){
                         [self performSegueWithIdentifier:@"link" sender:self];
                     }];

}

其中link是隐含标识符


这就是我试图分享它的方式

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UIButton *theButton=(UIButton *)sender;
    if([segue.identifier isEqualToString:@"link"])
    { 
         NSLog(names[theButton.tag]);
         controller.name.text = names[theButton.tag];
    }
}

其中namepageView

中的标签

但我收到错误:

-[ImagesTableViewController tag]: unrecognized selector sent to instance

没有记录怎么回事?我做错了什么?

2 个答案:

答案 0 :(得分:1)

不要从imageViewController设置控件(标签)属性,而是要将NSString传递给pageView的新实例。

执行segue后,在viewDidLoad

中设置标签文字

将属性添加到pageView.h

@property (strong, nonatomic) IBOutlet NSString *blogName;

更改segue代码

controller.name.text = names[theButton.tag];

 controller.blogName = names[tag];
页面viewDidLoad

中的

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.name.text = _blogName;
}

答案 1 :(得分:0)

进一步了解@bobnoble的解释:

naviguate方法中,您可以致电

[self performSegueWithIdentifier:@"link" sender:self];

请注意,你通过" self"作为发件人。这意味着在prepareForSegue中,sender参数将包含当前视图控制器。

如果您的naviguate方法是IBAction,正如它有发件人参数所示,为什么不改变它:

-(IBAction) naviguate:(id)sender 
{
  [UIView animateWithDuration:0.5
    delay:0
    options: UIViewAnimationOptionCurveEaseOut
    animations:^
    {
      [_tableView setFrame:CGRectMake(0, 569, _tableView.frame.size.width, 
      _tableView.frame.size.height)];
    }
    completion:^(BOOL finished)
    {
      [self performSegueWithIdentifier:@"link" sender: sender]; //Changed to pass sender
    }
  ];
}

这样就可以将发送方从您的操作方法传递给seque。

我仍然会更改您的prepareForSegue方法,以确保发件人响应"标记"在尝试从中获取标记值之前的选择器:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
  UIButton *theButton=(UIButton *)sender;
  if([segue.identifier isEqualToString:@"link"])
  { 
    int tag = -1
    if (![theButton respondsToSelector: @selector(tag)])
      NSLog(@"Sender does not respond to 'tag'!");
    else
    {
      int tag = theButton.tag;

      NSLog(names[tag]);
      controller.name.text = names[theButton.tag];
    }
   }
}