簡體   English   中英

如何將indexpath.row移動到新的視圖控制器

[英]How to move indexpath.row to a new view controller

我正在嘗試將此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

什么都沒有記錄下來怎么辦? 我究竟做錯了什么?

不要從設定的控制(標簽)屬性imageViewController ,而是要在一個通過NSString的瀏覽量這個新的實例。

執行完序列后,在viewDidLoad設置標簽文本

pageView.h添加屬性

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

更改密碼

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;
}

讓@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
    }
  ];
}

這樣,您就可以將發件人從您的操作方法傳遞到序列。

我仍然會更改您的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];
    }
   }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM