Question

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?

Was it helpful?

Solution 2

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];

OTHER TIPS

on click button event

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

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

    }

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];

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];

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top