[英]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.