4

CategoryTableViewUIView. 并CategoryTableView包含一个UITableView. 我从. CategoryTableView_ 现在,我想在执行时推送一个新的视图控制器。但是,在 中 ,我如何推送或呈现另一个视图控制器。我无法访问.HomeViewControllerUIViewControllerdidSelectRowAtIndexPathCategoryTableViewCategoryTableView

4

2 回答 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 回答