Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from second ViewController to first ViewController

I have 2 ViewControllers, in 1st - TableView and in 2nd - button with label on it. When I click on the button in 2nd ViewController I need to go back on TableView and set in

cell.detailTextLabel.text

text from label on the button.

For go back to first view I use:

[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:1] animated:YES];

but how I can set label from second view to:

cell.detailTextLabel.text

in first view?????

like image 986
rubik Avatar asked May 02 '26 22:05

rubik


1 Answers

I would define a protocol & delegate in the second view controller

@protocol SecondViewController;

@interface SecondViewController : UIViewController

@property (nonatomic, assign) id<SecondViewController> delegate;

@end


@protocol SecondViewController <NSObject>
- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button;
@end   

then when the button is tapped call the delegate:

- (IBAction)buttonTapped:(UIButton *)sender
{
    // do somthing..

    // then tell the delegate about the button tapped
    [self.delegate secondViewController:self didTappedOnButton:sender];
}  

In your first view controller implement the protocol

@interface FirstViewController : UIViewController <SecondViewControllerDelegate>

when you push the second view controller, set the first as the second delegate:

- (void)someMethodThatPushTheSecondViewController
{
    SecondViewController *svc = [[SecondViewController alloc] init];
    [self.navigationController pushViewController:svc animated:YES];
    svc.delegate = self;
}  

And implement the delegate method to get notified when the button tapped

- (void)secondViewController:(SecondViewController *)controller didTappedOnButton:(UIButton *)button
{
    // do somthing after button tapped
    // you can get the button title from button.titleLabel.text
}
like image 107
user1590031 Avatar answered May 05 '26 12:05

user1590031



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!