简体   繁体   English

如何在呈现的View Controller和呈现的View Controller之间建立关系?

[英]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. 我仍在学习Objective-C,并且很难从展示VC中解散展示的VC。

I read that to achieve this, you establish delegates to send the right messages back to the presenting VC from the presented VC. 我读到要实现此目的,您需要建立代表以将正确的消息从所呈现的VC发送回所呈现的VC。

My storyboard looks like so: 我的情节提要看起来像这样: 在此处输入图片说明

The issue I have is if I click UIBUtton2, it doesn't go back to Main VC. 我遇到的问题是,如果单击UIBUtton2,它不会返回到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. 但是,单击从VC1到VC2的任何单元格,然后单击UIButton3即可实现过渡到VC1的转换。

MainVC.h: MainVC.h:

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

MainVC.m: MainVC.m:

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

....

VC1 .h: 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: VC1.m:

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

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

VC2.h: VC2.h:

@protocol VC2Delegate <NSObject>

@required

- (void)didGoBackToVC1;

@end

@interface VC2 : UIViewController

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

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

VC2.m: 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. 从上面的评论中我了解到,我看到您的主VC正确显示了VC1 Modally。 So I guess you have this code in your VC1.m 所以我想您的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'. 接下来,确保您的VC1中的单元格具有“触发的序列”选择类型为“显示”。 And you probably had this code in your VC1.m 而且您可能在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 接下来在VC2.m中,您也应该拥有这个

- (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. 希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何暂时隐藏同时呈现视图控制器的呈现视图控制器 - How to temporarily hide a presented View controller that is also presenting a view controller 使用CAShapeLayer遮罩呈现的视图控制器,也遮罩呈现的视图控制器 - masking presented view controller with CAShapeLayer also mask the presenting view controller 呈现视图 controller 也可以是呈现视图 controller 吗? - Can a presented view controller also be a presenting view controller? 呈现视图 controller 位于呈现视图 controller 下方 - Presented view controller place under presenting view controller 旋转呈现的视图并锁定呈现视图控制器的方向 - Rotate presented view and lock the orientation of presenting view controller 为什么模式视图(由详细视图控制器呈现)呈现视图控制器是拆分视图控制器? - Why modal view's (presented by detail view controller) presenting view controller is split view controller? 如何在没有用户能够看到呈现视图的情况下使用模态视图控制器启动iOS应用程序? - How to start an iOS app with a modal view controller already presented without the user being able to see the presenting view? 调用viewWillAppear时显示视图控制器的问题被解除 - Issue with calling viewWillAppear of presenting view controller when presented one is dismissed 消除演示的VC时,Presenting View Controller失去子视图 - Presenting View Controller loses subviews when dismissing presented VC iOS /自动版式:取消已演示的控制器后,更改为演示视图 - IOS/Autolayout: Change to Presenting View After Presented Controller Dismissed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM