开发者

Call UINavigationController from subview in UITableViewCell

I have a table view in which I have subclassed the cells. In these cells I add a subview of a UIView. When sliding the cell I add another UIView to the subclass of UITableViewCell.

I would like to present a ModalViewController when pressing a button inside the second UIView (subview in UITableViewCell). I do not have a navigation controller in this view, ther开发者_如何学编程efore I am passing the navigation controller from the view controller my table view is inside of and down to my second UIView.

Here, I call it as you normally would but nothing happens.

ComposeCommentViewController *ccvc = [[ComposeCommentViewController alloc] initWithNibName:@"ComposeCommentViewController" bundle:nil];
[navController presentModalViewController:ccvc animated:YES];

Does anyone have an idea what I might do wrong or have another solution?

EDIT: This is how I set navController

First I pass it to my subclass of UITableViewCell.

if (feedCell == nil)
{
    feedCell = [[FeedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    [feedCell setNavigationControllerForSlidedView:[self navigationController]];
}

The subclass has the method setNavigationControllerForSlidedView: which looks like this:

- (void)setNavigationControllerForSlidedView:(UINavigationController *)navController
{
    [feedSlidedView setNavController:navController];
}

In my FeedSlidedView I have declared and synthesized UINavigationController *navController;


The way you are going about this runs contrary to MVC (model-view-controller) design practices. You have a number of mechanisms for accomplishing what you want within the MVC framework that Apple provides in its SDK. Probably the simplest, in my opinion, would be to add a target-action to the button in the subview of your UITableViewCell. In your view controller's tableView:cellForRowAtIndexPath: method, add something like the following:

[button addTarget:self action:@selector(presentComposeComment:) forControlEvents:UIControlEventTouchUpInside];

In this case, self would be the UIViewController that is responsible for the UITableView in question. You would then include the method for the selector above in that view controller:

- (void)presentComposeComment:(id)sender {
    ComposeCommentViewController *ccvc = [[ComposeCommentViewController alloc] initWithNibName:@"ComposeCommentViewController" bundle:nil];
    [self presentModalViewController:ccvc animated:YES];
}

Note that I am not sending the presentModalViewController:animated: message to the navigation controller, but rather the view controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜