calling pushViewController from a inside a custom UITableViewCell class
I have a custom UITableViewCell which is part of a navigation controller. The cell has a button and when that b开发者_如何学Goutton 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?
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.
Create some instance variables in the BarViewController
class and set them before you push the controller.
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];
Follow the example already given.
精彩评论