views:

62

answers:

4

I have a custom UITableViewCell which is part of a navigation controller. The cell has a button and when that button is pressed I want to push a view onto my navigation controller.

I can do this pretty easily by using a selector in my cellForRowAtIndexPath method.

[cell.fooButton addTarget:self action:@selector(pushBarViewController:) forControlEvents:UIControlEventTouchUpInside];

And then inside of the pushBarViewController method I can perform the push operation

[self.navigationController pushViewController:barViewController animated:YES];

This works pretty well, but now I need to pass in some information contained in my custom UITableViewCell into my barViewController. My action:@selector will not allow me to pass in any arguments so I can't use this method.

I've created an action on the button press inside the custom UITableViewCell but I can't push the view from in here as there is no navigation controller.

Can anyone help?

A: 

How about this:

-(void)pushBarViewController{
    YourBarViewController *barViewController = [[YourBarViewController alloc] init];
    barViewController.myString = @"this string will be passed to the new view";
    [self.navigationController pushViewController:barViewController animated:YES];
    [barViewController release];
}

and in the header file YourBarViewController.h, define

NSString *myString;

with

@property(nonatomic, retain) NSString *myString;

and synthesize this in the implementation file.

Andy
The problem is that I have the data I need in my UITableViewCell. I want that data available in my BarViewController. The question is how do I get the data contained in my UITableViewCell into my BarViewController.
KJF
A: 

Create some instance variables in the BarViewController class and set them before you push the controller.

nevan
I cannot push the controller inside my UITableViewCell class, so the question is how do I get the data from my UITableViewCell class into my BarViewController.
KJF
You're going to need a reference to your cell then. When a button triggers an action it usually sets itself as the sender, so you can identify where the message came from. I'm presuming that the information you want to send is referenced from the button? You should have something like `pushBarViewController:(id)sender` with the `sender` being the button. When you put the button on the cell, you could also give the button an `int` ivar, corresponding to the cell position (and the position in the data array). Then when you get the message from a button, find out what the int was, then the info.
nevan
A: 

Follow the example already given.

David Frantz
I don't get you, which example?The one from Andy?This example doesn't work for me as the myString instance variable is set inside the method pushBarViewController that's located in the BarViewController class.The data I need resides in an instance of UITableViewCell. I need to somehow pass the data from my UITableViewCell instance into my BarViewController instance.
KJF
A: 

I managed to figure it out. I kept this:

[cell.fooButton addTarget:self action:@selector(pushBarViewController:) forControlEvents:UIControlEventTouchUpInside];

And then in the pushBarViewController I did this:

UIButton* button = (UIButton *)sender;
CustomCell *cell = (CustomCell *)[button superview];
NSUInteger row = [customTableView indexPathForCell:cell].row;
Foo *foo = [fooArray objectAtIndex:row];

Now that I have access to foo I can alloc my barViewController and set

barViewController.foo = foo

And then do the usual push.

[self.navigationController pushViewController:barViewController animated:YES];    
KJF