0

I have one tableview in my viewcontroller and in that i have customTableViewCell.i have dynamic buttons which generated runtime in my customeview cell.and on that button's click i want to push my view controller which is holding my tableview controller with the customtableviewcell to another view controller so how can i do it ? in my custometableviewcell.m file i have this button.

CommentButton = [[UIButton alloc]init];
        CommentButton.frame = CGRectMake(CommentLabel.frame.origin.x+CommentLabel.frame.size.width+5, 5, 110, 35);
        CommentButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [CommentButton setTitle:@"Add Comment" forState:UIControlStateNormal];
        CommentButton.backgroundColor = [UIColor clearColor];
        [CommentButton addTarget:self action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];

folowing is my postnotification in customtableviewcell.m file which will called on comment button click

-(IBAction)GoToComment:(id)sender {

[[NSNotificationCenter defaultCenter] postNotificationName:@"GoToComment" object:nil];

}

above is my button on this i have registered on NSNotification which is called the following method

[[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(GotoComents)
                                                name:@"GoToComment" object:nil];

above line is in viewdidload of my viewcontroll in which i have tableview with that customtableviewcell.

and from customtableview cell i am posting the above notofication wich will push the new view controller

-(void)GotoComents
{  
    TableTextViewController *settingView=[[TableTextViewController alloc]initWithNibName:@"TableTextViewController" bundle:nil];
    [self.navigationController pushViewController:settingView animated:YES];




}

right now i have just did it with the nsnotofication center which just call from my customTableViewcell and invoke the viewcontroller method.

but is it right way to do it?if its not then can anyone please help me how can i achieve this.? remember that i have dynamic button which generated runtime.so please can any one guide me?

BhavikKama
  • 8,566
  • 12
  • 94
  • 164
  • Please, include some code. I cannot understand whether you need just show next view controller or pass data or maybe something else. – Stas Aug 06 '13 at 06:15
  • 1
    First thing is that your question is not clear what are you asking and what is your issue. and attach some code that you have tried. so user code understand your question. Be clear with your issue and question Too. what does meaning of this line **I have one customview cell in my app and in that i have customTableViewCell** – Nitin Gohel Aug 06 '13 at 06:27
  • @NitinGohel i have even posted the information that i have done it with postnotification now see what i want to explain... – BhavikKama Aug 06 '13 at 07:13
  • but what need of using Notification. simply go connect code put in your button action and as i told you in my Answer we can not push a viewcontroller without UINavigationController so it whould be a better to presentViewcontroller instead of pushViewcontroller. – Nitin Gohel Aug 06 '13 at 07:15
  • oh man...!!i have told that i am generating dynamic button...how can i connect it.... – BhavikKama Aug 06 '13 at 07:17

6 Answers6

2

on click button event

-(void)buttonAction:(UIButton*)sender
    {

    YourViewControllerVC *yvc=[YourViewControllerVC alloc] init];
    [self.yournavigatioonController pushViewController:yvc animated:YES];

    }
Kanhaiya Sharma
  • 1,040
  • 8
  • 20
  • in my customtableview cell i dont have access to navigation controller..this will obviously not work – BhavikKama Aug 06 '13 at 06:08
  • in your customtableview cell Add target-action for dynamically generated button: [myButton addTarget:targetObject action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside] – Kanhaiya Sharma Aug 06 '13 at 06:11
  • @BhavikKama haven't access UINavigation-controller then how to push a view-controller? – Nitin Gohel Aug 06 '13 at 06:33
  • What about `presentViewController:animated:completion:` method? – Stas Aug 06 '13 at 06:37
  • that's what i want to know??in custom cell view i have obviously dont have navigation controller access..but still the controll is in customview cell so i need to make it invoke any process that psuh my view controller which is holding my tableview with the custom view cell @NitinGohel – BhavikKama Aug 06 '13 at 07:04
1

You can declare your TableViewController subclass as custom cell delegate, create GoToComment method inside it, then, instead of

[CommentButton addTarget:self action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];

do

[CommentButton addTarget:self.delegate action:@selector(GoToComment:) forControlEvents:UIControlEventTouchUpInside];
Stas
  • 641
  • 8
  • 16
  • Please, write your question properly. Do you have a navigation controller? Do you use storyboard/segues? Do you create your cell programmatically or use IB? – Stas Aug 06 '13 at 06:22
  • @BhavikKama new answer. – Stas Aug 06 '13 at 07:24
  • so i have to write got comment in my viewcontroller file?? – BhavikKama Aug 06 '13 at 09:56
  • Yo mean GoToComment method? Yes. Please, look at delegation explanation here: http://developer.apple.com/library/ios/DOCUMENTATION/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW19 – Stas Aug 06 '13 at 10:31
0

Add target-action for dynamically generated button:

[myButton addTarget:targetObject action:@selector(actionMethod) forControlEvents:UIControlEventTouchUpInside];

Then implement actionMethod for the targetObject and it will be called whenever button is touched. In this action method you need the following code:

UIViewController *viewController = [[UIViewController alloc] init];
[self.navigationController pushViewController:viewController animated:YES];
Kirsteins
  • 27,065
  • 8
  • 76
  • 78
  • tagetobject will be the source view controller from where i have to push new viewcontroller??and in my targetobject view controller i have to write thta actioneMethod? – BhavikKama Aug 06 '13 at 06:17
0

you have not described your question correctly .. you can do what you want this way:

    LoginViewController * login_view = [[ LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
    UINavigationController *Nav01 = [[UINavigationController alloc] initWithRootViewController:login_view];

    Nav01.navigationBar.hidden = YES;
    login_view.fromProfileView = FALSE;
    [self presentModalViewController:Nav01 animated:YES];
Banker Mittal
  • 1,918
  • 14
  • 26
0

I think what you need is

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

With above code, you can find your current view controller inside your custom cell.

Evan JIANG
  • 690
  • 6
  • 5
-1

You got to add your button in this way :

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self 
           action:@selector(aMethod:)
 forControlEvents:UIControlEventTouchDown];

[view addSubview:button];

where aMethod is an IBAction returning method.

nithinbhaktha
  • 723
  • 5
  • 8