【问题标题】:Programatically create segue from xib file to viewcontroller in storyboard以编程方式从 xib 文件创建 segue 到情节提要中的视图控制器
【发布时间】:2016-04-27 16:20:21
【问题描述】:

我使用的库在 Objective-c 中有一个 xib 文件

我的故事板有 swift 类

我需要从那个 xib 文件创建 segue 到我的故事板并传递一些数据。

试过了,没有运气

XIB 文件

#import "IncomingMessageCell.h"

@implementation IncomingMessageCell

- (void)awakeFromNib {

    UIImage * balloonImage = [UIImage imageNamed:@"bubble-left"];
    balloonImage = [balloonImage resizableImageWithCapInsets:(UIEdgeInsets){36, 32, 18, 12}];
    self.bubbleView.image = balloonImage;

    self.avatarView.layer.cornerRadius = 25;
    self.avatarView.clipsToBounds = YES;

    self.avatarView.userInteractionEnabled = YES;

    UITapGestureRecognizer *tapGesture1 = [[UITapGestureRecognizer alloc] initWithTarget:self  action:@selector(tapGesture:)];

    tapGesture1.numberOfTapsRequired = 1;

    [tapGesture1 setDelegate:self];

    [self.avatarView addGestureRecognizer:tapGesture1];
    }

- (void) tapGesture: (id)sender
{
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"yourVcIdentifier"];
[self.navigationController pushViewController:vc animated:YES];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end

这怎么可能?

【问题讨论】:

  • 你在哪里试试这个?你现在的 VC 是什么?
  • 我更新了我的问题
  • 那么当你点击时会发生什么?
  • 无法构建我在“IncomingMessageCell *”类型的对象上找不到属性“navigationController”错误
  • 我看到 IncomingMessageCell 不是 UIVIewController 类型。您需要有一个代表来保存对导航控制器的引用。 IncomingMessageCell 是如何创建的?

标签: ios objective-c segue xib


【解决方案1】:

在 ViewController 中使用此单元格保存 tableview。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    IncomingMessageCell *cell = ...;
    cell.avatarView.userInteractionEnabled = YES;

    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
    tapGesture.numberOfTapsRequired = 1;
   [cell.avatarView addGestureRecognizer:tapGesture];
    cell.avatarView.tag = 500 + indexPath.row;  // To detect cell that image belongs to, because you use dequeue cell
    return cell;
}

- (IBAction)handleTapGesture:(UITapGestureRecognizer *)tapGesture{
    //Do you action
    NSLog(@"Row tap : %ld",tapGesture.view.tag - 500);
    UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"yourVcIdentifier"];
    [self.navigationController pushViewController:vc animated:YES];
}

【讨论】:

  • 我已经可以处理点击,但我不知道如何转到主情节提要
  • 是的,但是要处理 segue,您应该将代码放在 ViewController 而不是 Cell 中。试试我的方法,你可以把你的代码推送到另一个视图:- (IBAction)handleCellBtnClicked:(UIButton *)sender{ //Do you action UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"yourVcIdentifier"]; [self.navigationController pushViewController:vc animated:YES]; }
  • 我们不能使用来自 cellForRowAtIndexPath 的制表符吗?
  • 您不必在 cellForRow 方法中使用 Tapgesture。你可以使用 didSelectRow 方法@UtkuDalmaz
  • @TejaNandamuri 我不想使用整个单元格进行 segue,我只需要 uiimageview
猜你喜欢
  • 1970-01-01
  • 2018-04-10
  • 2012-11-25
  • 2015-07-05
  • 2012-06-08
  • 2017-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-07-21
相关资源
最近更新 更多