我CategoryTableView
从UIView
. 并CategoryTableView
包含一个UITableView
. 我从. CategoryTableView
_ 现在,我想在执行时推送一个新的视图控制器。但是,在 中 ,我如何推送或呈现另一个视图控制器。我无法访问.HomeViewController
UIViewController
didSelectRowAtIndexPath
CategoryTableView
CategoryTableView
问问题
5965 次
2 回答
7
类别表视图.h
@property (retain, nonatomic) parentViewController *parent; //create one property for parent view like this
分类表视图.m
@sythesize parent;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[parent.navigationController . . .]; // preform action
//OR..
[parent presentModalViewController: . . .]; // present modal view
}
父母.m
//while calling your CategoryTableView assign self to your parent object
CategoryTableView *tblView = [CategoryTableView alloc] init];
tblView.parent = self;
于 2013-04-03T05:38:36.017 回答
2
您需要使用自定义委托来实现这一点......
在CategoryTableView.h
@protocol CategoryTableViewDelegate <NSObject>
-(void)pushViewControllerUsinDelegate:(UIViewController *)viewController;
@end
@interface CategoryTableView : UIView
@property (nonatomic, retain) id<CategoryTableViewDelegate> delegate;
@end
在CategoryTableView.m
@implementation CategoryTableView
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Create the required UIViewControllers instance and call the delegate method.
UIViewController *viewController = [[UIViewController alloc] init];
[self.delegate pushViewControllerUsinDelegate:viewController];
}
@end
在HomeViewController.h
@interface HomeViewController : UIViewController <CategoryTableViewDelegate>
@end
在HomeViewController.m
@implementation HomeViewController
-(void)viewDidLoad
{
[super viewDidLoad];
//initialization of CategoryTableView like this...
CategoryTableView *categoryTableViewInstance = [[CategoryTableView alloc] init];
[categoryTableViewInstance setDelegate:self];
}
-(void)pushViewControllerUsinDelegate:(UIViewController *)viewController
{
[self.navigationController pushViewController:viewController animated:YES];
}
于 2013-04-03T05:45:54.593 回答