繁体   English   中英

在ios中从另一个视图中关闭呈现的视图控制器

[英]Dismiss Presented View Controller from Another View in ios

我有一个名为AViewController 它包含一个按钮,用于显示一个名为LinkedInWebVC的新ViewController 现在,我想从A LinkedInWebVC

我的代码如下:

-(IBAction)button_tap:(id)sender
{
_loginWebViewController = [[LinkedInWebVC alloc]initWithNibName:@"LinkedInWebVC" bundle:nil];

    [self presentViewController:_loginWebViewController
                       animated:YES
                     completion:^{
                         [self.oauth1Controller loginWithWebView:_loginWebViewController.webView completion:^(NSDictionary *oauthTokens, NSError *error) {
                             if (!error) {
                                 // Store your tokens for authenticating your later requests, consider storing the tokens in the Keychain
                                 self.oauthToken = oauthTokens[@"oauth_token"];
                                 self.oauthTokenSecret = oauthTokens[@"oauth_token_secret"];

                             }
                             else
                             {
                                 NSLog(@"Error authenticating: %@", error.localizedDescription);
                             }

                             [self dismissViewControllerAnimated:YES completion: ^{
                                 self.oauth1Controller = nil;
                             }]; `*****Problem is here, not dismiss LinkedInWebVC*****`
                         }];
                     }];

}

而不是调用self尝试在_loginWebViewController上调用dismiss方法

 [_loginWebViewController dismissViewControllerAnimated:YES completion: ^{
                                 self.oauth1Controller = nil;
                             }];

你可以改变:

[self dismissViewControllerAnimated:YES completion..]

[_loginWebViewController dismiss]

因为这个“自我”是presentingViewController。

请按照以下简单步骤从secondView控制器调用视图,然后从mainView Controller取消视图,并立即调用第三个视图控制器

步骤1:创建一个包含协议的.h文件。

//  ViewDelegate.h

#import <Foundation/Foundation.h>

@protocol ViewDelegate <NSObject>
- (void)DismissSecondView;
@end

步骤2:在viewController.h文件中继承如下所示的委托

//  ViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#import "ThirdViewController.h"
#import "ViewDelegate.h"


@interface ViewController : UIViewController<ViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *FirstView;

-(void)PresentThirdView;
@end

步骤3:在ViewController.m文件中实现该方法

//  ViewController.m

#import "ViewController.h"

@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];
}

- (void)didReceiveMemoryWarning {
  [super didReceiveMemoryWarning];
}

- (IBAction)ChangeViewAction:(id)sender {
  SecondViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];
  vc.delegate = self;//important step
  [self presentViewController:vc animated:YES completion:nil];
}
- (void)DismissSecondView{
  [self dismissViewControllerAnimated:YES completion:nil];
  [self PresentThirdView];
}
-(void)PresentThirdView{
  ThirdViewController * tvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ThirdViewController"];
  [self presentViewController:tvc animated:YES completion:nil];
}
@end

步骤4:在SecondViewController.h文件中为委托分配属性

//  SecondViewController.h

#import <UIKit/UIKit.h>
#import "ViewController.h"
#include "ViewDelegate.h"


@interface SecondViewController : UIViewController
@property (nonatomic, assign) id <ViewDelegate> delegate;
@end

步骤5:在SeconViewController.m //的动作中调用该方法

#import "SecondViewController.h"
#import "ViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
}
- (IBAction)DismissAction:(id)sender {
  [self.delegate DismissSecondView]; //calling DismissSecondView Method
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end

暂无
暂无

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

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