I am using a button on UITableViewCell
but the button coding is done on UITableViewCell.m file. I have made a selector on UIButton
touchUpInside but when on selector when I used the code to UINavigationController
it is not allowing me as it is not a UIViewController
. Kindly help.

- 12,477
- 4
- 45
- 118

- 578
- 4
- 13
-
relevant code ? search on delegate pattern – Shubhank Mar 21 '14 at 12:20
-
use protocol for doing this task crate your own custom delegate. that called from your `tableviewcell.m` – Nitin Gohel Mar 21 '14 at 12:26
-
can you please give me some code so i can try and implement. – Ravi Sharma Mar 21 '14 at 12:27
-
why you not put IBAction in to table class instead of cell class. – Nitin Gohel Mar 21 '14 at 12:27
-
@nitin that is needs to be done in this way only. i cant to on table view . please help. – Ravi Sharma Mar 21 '14 at 12:28
4 Answers
You can track your event from your table view cell to view controller using protocol as well,
Example:
in your MyCell.h
@protocol CellDelegate
-(void)itemSelected;
@end
@interface MyCell : UITableViewCell
@property (nonatomic, assign) id<CellDelegate> delegate;
@end
in your MyCell.m
@implementation MyCell
@synthesize delegate;
-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self)
{
UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(200.0, 5.0, 100.0, 40.0)];
[button addTarget:self action:@selector(itemSelected) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor redColor];
[self addSubview:button];
button = nil;
}
return self;
}
-(void)itemSelected
{
[delegate itemSelected];
}
@end
in your ViewController.h
@interface ViewController : UIViewController <CellDelegate>
in your ViewController.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * cellID = @"CellID";
MyCell * cell = (MyCell *) [tableView dequeueReusableCellWithIdentifier:cellID];
if (!cell)
{
cell = [[MyCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
cell.delegate = self;
}
return cell;
}
//Cell Delegate Method
-(void)itemSelected
{
//Your navigation action here
}
Thanks!

- 3,241
- 3
- 17
- 34
-
this does have all the basic info on how to properly use the button and use in viewcontroller. If it is not working. show your code. how have you ported to your project so that further help can be given – Shubhank Mar 21 '14 at 13:05
-
but this is not working on tableview cell and deletegates are not calling. – Ravi Sharma Mar 21 '14 at 13:39
-
Have you added the CellDelegate to the .h in your tableview controller ? Change: [delegate itemSelected]; to if ((delegate respondsToSelector:@selector(itemSelected)]) { [delegate itemSelected]; else NSLog(@"DELEGATE IS NIL OR DOESN'T IMPLEMENT METHOD"); – CW0007007 Mar 21 '14 at 14:53
You need to use the delegate protocol. See my answer here on how to create a delegate:
How to declare events and delegates in Objective-C?
Also, post some code on what you have tried and also how you create your cells.
Remove the button action from UITableViewCell.h and its definition from UITableViewCell.m file. Instead just make an property outlet in tableviewcell.h file. Then in UIViewController
cellForRowAtIndexPath:
method just add target selector on the cell buttton. Ex -
[cell.myButton addTarget:self action:@selector(pushViewControllerNow) forControlEvents:UIControlEventTouchUpInside];
Then just implement this method in the same UIViewController.m
-(void) pushViewControllerNow {
YourViewController vc = ....//Instantiate your view controller from nib
[self.navigationController pushViewController:vc animated:YES];
}

- 881
- 7
- 29

- 977
- 1
- 7
- 24
-
Dont do this, this can cause crashing as the uitableViewCells are reused, the buttons can be released during the dequeue. – CW0007007 Mar 21 '14 at 14:52
-
You can post a notification when the button is tapped. Handle this notification in your view controller and push next view controller;
Like this:
In your ViewController subscribe to receive the notification. This can be done in viewDidLoad
for example:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(timeToPushViewController:)
name:ButtonOnCellTappedNotification
object:nil];
and provide corresponding method:
- (void)timeToPushViewController:(NSNotification *)notification
{
NSDictionary *userInfo = [notification userInfo];
NSString *userId = userInfo[@"user_id"];
[self.navigationController pushViewController:[[MyViewController alloc] initWithUserId:userId]];
}
then provide method for tapped button:
- (void)buttonOnCellTapped:(UIButton *)sender
{
NSNotification *notification = [NSNotification notificationWithName:ButtonOnCellTappedNotification
object:nil
userInfo:@{
@"user_id" : @"some_user_id"
}];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
This is all you need)))

- 21,488
- 17
- 97
- 161
-
wont delegate pattern works much better in this case. Not every class would like to know this tableCell button was tapped – Shubhank Mar 21 '14 at 12:33
-
@Kabira of course delegate is an option either, but not EVERY CLASS would subscribe for the notification, just the needed view controller. Write an answer with delegate pattern, that would be nice – Andrey Chernukha Mar 21 '14 at 12:35
-
-
Just saying that how i know that only notification thing is ButtonOnCellTappedNotification this only? – Ravi Sharma Mar 21 '14 at 12:39
-
@RaviSharma i'm sorry so much my friend, but i'm not able to understand what you're trying to say. use the code i've given to you and it will work just like you need – Andrey Chernukha Mar 21 '14 at 12:41
-
ButtonOnCellTappedNotification is giving error there is no notification like this? – Ravi Sharma Mar 21 '14 at 12:43
-
oh, i see, i'm sorry. this is not a system notification. declare it like this #define ButtonOnCellTappedNotification @"button_on_cell_tapped" – Andrey Chernukha Mar 21 '14 at 12:45
-
-
@RaviSharma simply crying this is not working wont work. You need to ask in proper english and explain where the error is. Make a chat room and discuss properly. – Shubhank Mar 21 '14 at 13:08
-
@RaviSharma 1) define notification name #define ButtonOnCellTappedNotification @"button_on_cell_tapped" 2)in viewDidLoad subscribe for the notification 3) provide - (void)timeToPushViewController method in your viewController 4) provide method which will be called when the button is tapped - (void)buttonOnCellTapped:(UIButton *)sender. that's all you need))) – Andrey Chernukha Mar 21 '14 at 13:18
-
if it doesn't help send me your project and i'll make it work. andrey.chernukha@codeit.com.ua – Andrey Chernukha Mar 21 '14 at 13:22
-
my button is on other file .... as i have asked in the question. My whole code is on chatviewcontroller.m and my cell button on tableviewcell.m. – Ravi Sharma Mar 21 '14 at 13:24
-
so what? what's wrong about that? they're in different files - that's good!!! what's wrong? – Andrey Chernukha Mar 21 '14 at 13:25
-
i have no idea. probably they have deleted chats... don't these comments look like a chat? go on explain. but in very detail so i could understand everything. don't worry - we'll make that thing work – Andrey Chernukha Mar 21 '14 at 13:43
-
okk i just want to do chat thing. In that i am using NSBUBBLECHAT and there i have to do custom work like USERNAME to show. For that i have made some research and added a username field and a button along with where i want to click on that and to show user profile. But i am able to use selector but that selector is on UIBubbleTableViewCell.m file there i am not able to use ChatViewController *cht = [[ChatViewController alloc] init];[self.navigationController pushViewController:cht animated:YES]; so kindly help me to make it done somehow. – Ravi Sharma Mar 21 '14 at 13:49
-
omg. i expected you to explain why my answer doesn't work for you. my answer contains everything you need to resolve your issue. why it doesn't work for you? – Andrey Chernukha Mar 21 '14 at 13:51
-
-
your code is not working even the function is not calling on button click i dont know why – Ravi Sharma Mar 21 '14 at 13:52
-
-
-
UIButton *btnviewprfl = [UIButton buttonWithType:UIButtonTypeRoundedRect]; [btnviewprfl addTarget:self action:@selector(viewUserProfile:) forControlEvents:UIControlEventTouchDown]; btnviewprfl.tag = [[self.data.usernameLbl objectForKey:@"userid"] intValue]; btnviewprfl.frame = lbl.frame; btnviewprfl.backgroundColor = [UIColor clearColor]; [self.contentView addSubview:btnviewprfl]; – Ravi Sharma Mar 21 '14 at 14:06
-
-(void)viewUserProfile:sender{ NSLog(@"%@",self.nextResponder); ChatViewController *cht = [[ChatViewController alloc] init]; [cht.navigationController pushViewController:cht animated:YES]; //[cht viewUserProfile:sender]; } – Ravi Sharma Mar 21 '14 at 14:08
-
btnviewprfl - is the cell button? @selector(viewUserProfile:) - this selector is not called? do i understand everything right? – Andrey Chernukha Mar 21 '14 at 14:11
-
btnviewprfl is a simple button which is added in a view and that view is added in the cell. the selector is called on the button. – Ravi Sharma Mar 21 '14 at 14:21
-
you're driving me crazy)))) put that code NSNotification *notification = [NSNotification notificationWithName:ButtonOnCellTappedNotification object:nil userInfo:nil]; [[NSNotificationCenter defaultCenter] postNotification:notification]; to viewUserProfile: – Andrey Chernukha Mar 21 '14 at 14:24
-
That works but this is not passing the parameter like i just want to a particular user id to see profile of that user . can u please help me in that – Ravi Sharma Mar 21 '14 at 14:30
-
-
when i have just made it on table view then on load more it is calling two time on navigation how to make it only one time? – Ravi Sharma Mar 24 '14 at 10:49
-
remove view controller from subscribers when you're done [[NSNotificationCenter defaultCenter] removeObserver:self]; – Andrey Chernukha Mar 24 '14 at 10:55