从UIView的子类中推入视图控制器

本文介绍如何在Swift UI自定义视图CategoryTableView中实现didSelectRowAtIndexPath时推入新视图控制器。CategoryTableView继承自UIView并内嵌UITableView,通过设置parentViewController属性来引用父控制器HomeViewController,从而在适当的时候调用导航或模态展示方法。
问题描述:
创建了一个视图CategoryTableView,继承UIView。CategoryTableView包含了一个UITableView。我将CategoryTableView 作为子类添加到HomeViewController 中,HomeViewController 是UIViewController的子类。目前,我需要在didSelectRowAtIndexPath 执行时推入一个新的controller。但是在CategoryTableView中怎么推入或显示另一个视图控制器?

不能在CategoryTableView去导航控制器。


解决方案:
CategoryTableView.h

Objective C code
?
1
@property  ( retain nonatomic ) parentViewController *parent;  //create one property for parent view like this


CategoryTableView.m
Objective C code
?
1
2
3
4
5
6
7
8
@sythesize  parent;
 
- ( void )tableView:( UITableView  *)tableView didSelectRowAtIndexPath:( NSIndexPath  *)indexPath
{
     [parent.navigationController . . .];  // preform action
     //OR..
     [parent presentModalViewController: . . .];  // present modal view
}


parent.m
Objective C code
?
1
2
3
4
//while calling your CategoryTableView assign self to your parent object
 
     CategoryTableView *tblView = [CategoryTableView alloc] init];
     tblView.parent =  self ;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值