简体   繁体   中英

How to setup a relationship between the presenting View Controller and the presented View Controller?

I'm still learning objective-c and having a difficult time dismissing presented VCs from presenting VCs.

I read that to achieve this, you establish delegates to send the right messages back to the presenting VC from the presented VC.

My storyboard looks like so: 在此处输入图片说明

The issue I have is if I click UIBUtton2, it doesn't go back to Main VC. In fact, it does nothing.

However, clicking on any cells from VC1 segue to VC2 and clicking on UIButton3 transition back to VC1 as achieved.

MainVC.h:

#import "VC1.h"
@interface MainVC : UIViewController <VC1Delegate>
....

MainVC.m:

- (void)didGoBackToMainVC
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

....

VC1 .h:

#import "VC2.h"
@protocol VC1Delegate <NSObject>

@required

- (void)didGoBackToMainVC;

@end

@interface VC1 : UIViewController <UITableViewDataSource, UITableViewDelegate,
VC2Delegate>

@property (weak, nonatomic) id <VC1Delegate> delegate;

- (IBAction)UIButton2:(UIButton *)sender;

VC1.m:

- (void)didGoBackToVC1
{   
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)UIButton2:(UIButton *)sender
{
    [self.delegate didGoBackToMainVC];
}

VC2.h:

@protocol VC2Delegate <NSObject>

@required

- (void)didGoBackToVC1;

@end

@interface VC2 : UIViewController

@property (weak, nonatomic) id <VC2Delegate> delegate;

- (IBAction)UIButton3:(UIButton *)sender;

VC2.m:

- (IBAction)UIButton3:(UIButton *)sender
{
    [self.delegate didGoBackToSponsors];
}

I'm sure I'm not understanding this relationship properly. Can someone tell me what I'm doing wrong?

Thanks

From what I understand in your comments above I saw that your Main VC shows VC1 Modally correctly. So I guess you have this code in your VC1.m

- (IBAction)goToMainVC:(id)sender
{
  [self dismissViewControllerAnimated:YES completion:nil];
}

Next make sure your Cell in VC1 has 'Triggered Seques' Selection type of 'Show'. And you probably had this code in your VC1.m

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
  if ([segue.identifier isEqualToString:@"pushVC2"]) {

    VC2 *vc2 = (VC2 *)segue.destinationViewController;

  }
}

Next in your VC2.m you also should have this

- (IBAction)dismissVC2:(id)sender
{
  [self dismissViewControllerAnimated:YES completion:nil];
}

If these are all you have, it should work like a charm. And no Delegates needed in this case. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM