如何解雇2视图控制器

时间:2015-12-24 18:15:13

标签: ios objective-c

我有一个问题需要支持:

假设我有控制器A和视图控制器B.视图控制器A存在视图控制器B.之后视图控制器B加载内容完成。在View controller B,我有一个按钮。 当触摸内部触摸事件按钮时,如何关闭两个视图控制器?

这里是我的代码:在视图控制器A

UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"mainstoryboard" bundle:nil];
ViewControllerB *viewcontrollerB = [storyBoard instantiateViewControllerWithIdentifier:@"viewcontrollerB"];
[self presentViewController:viewcontrollerB animated:YES completion:nil];

视图控制器B中的代码

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

感谢您帮助我。你会节省我的时间。 :)

2 个答案:

答案 0 :(得分:0)

使用委托。按下按钮时调用delegate,并在委托方法实现中写下以下内容:

[self dismissViewControllerAnimated:NO completion:nil];

答案 1 :(得分:0)

在ViewControllerB上使用此委派

在ViewControllerB.h上执行此操作

@protocol ViewControllerDismissDelegate;

@interface ViewControllerB : UIViewController

@property(nonatomic) id<ViewControllerDismissDelegate> delegate;

@end

@protocol ViewControllerDismissDelegate <NSObject>

@optional
- (void)didViewControllerBDismiss;
@end

在ViewControllerB.m上执行此操作

// Button action
- (void)buttonDidClick {
    if ([self.delegate respondsToSelector:@selector(didViewControllerBDismiss)]) {
        [self dismissViewControllerAnimated:NO completion:nil];
        [self.delegate didViewControllerBDismiss];
    }
}

在此处详细了解代理和数据来源 https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html