从模态视图呈现视图控制器给出错误

Posted

技术标签:

【中文标题】从模态视图呈现视图控制器给出错误【英文标题】:Presenting viewcontroller from modal view giving error 【发布时间】:2013-09-22 05:44:24 【问题描述】:

在尝试从模式视图转到普通视图控制器 (PBSViewControllerDataDetail.h) 时调试我的应用程序时遇到断言失败错误(不是呈现模态视图)

基本上我想做的是:

ViewController1,打开模态视图并执行异步请求。当它返回时,将用户发送到第二个普通视图控制器,该控制器将向用户呈现在模态视图中获得的数据。

在模态试图呈现第二个视图控制器之前,其他一切都有效。我收到错误消息(第一行是我在尝试呈现第二个 VC 的方法中的 NSLog 输出)

2013-09-22 07:24:11.410 PBSDashboard[566:1303] flipToDataView->Send user to PBSViewControllerDataDetail
2013-09-22 07:24:11.411 PBSDashboard[566:1303] *** Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:], /SourceCache/UIKit_Sim/UIKit-2380.17/UIWindowController.m:211

请注意:我在这个项目中使用 NavigationViewController 并考虑重新启动整个批次并使用一个,但目前我只是想了解如何从 Modal 转到普通 ViewController那不是原来的。

我尝试清理目标,从头开始重新创建第二个 VC,但均未成功。

代码 sn-ps

PBSRequestViewController.h

#import <UIKit/UIKit.h>
#import "Unirest.h"
#import "PBSDaySales.h"

@class PBSRequestViewController;

@protocol PBSRequestViewControllerDelegate
    - (void) requestViewControllerDidFinish:(PBSRequestViewController *)controller;
@end

@interface PBSRequestViewController : UIViewController

@property (weak, nonatomic) IBOutlet id <PBSRequestViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet NSString *calendType;
@property (weak, nonatomic) IBOutlet NSString *targetDate;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;


- (void) runUnirestRequest:(NSString*)urlToGet;
- (void) loadDefaultSettings;
- (NSString*) buildRequestUrl:(NSString*)serverUrl withPort:(NSString*)serverPort withCalenderType:(NSString*)calendarType withDateStringParameter:(NSString*)selectedTargetDate;

- (IBAction)btnBack:(id)sender;

- (PBSDaySales*) deserializeJsonPacket:(NSDictionary*)httpJson;
- (void)createActivityIndicator;
- (void)flipToDataView;

@end

PBSRequestViewController.m(只是第二个 vc 的调用函数)

// Method will forward the user on to the data view.
// Used after the uniRestRequest is completed and data exists.
- (void) flipToDataView

    NSLog(@"flipToDataView->Send user to PBSViewControllerDataDetail");
    PBSViewControllerDataDetail *dataVc = [[PBSViewControllerDataDetail alloc] initWithNibName:@"PBSViewControllerDataDetail" bundle:nil];

    dataVc.daySalesData = daySalesFigures;

    [self presentViewController:dataVc animated:YES completion: nil];

PBSViewControllerDataDetail.h

#import <UIKit/UIKit.h>
#import "PBSDaySales.h"

@interface PBSViewControllerDataDetail : UIViewController
    @property (weak, nonatomic) PBSDaySales *daySalesData;
@end

PBSViewControllerDataDetail.m

#import "PBSViewControllerDataDetail.h"

@interface PBSViewControllerDataDetail ()

@end

@implementation PBSViewControllerDataDetail

@synthesize daySalesData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        // Custom initialization
    
    return self;


- (void)viewDidLoad

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"View loaded");


- (void)didReceiveMemoryWarning

    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.


@end

PBSAppDelegate.m

#import "PBSAppDelegate.h"

#import "PBSViewController.h"

@implementation PBSAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
        self.viewController = [[PBSViewController alloc] initWithNibName:@"PBSViewController_iPhone" bundle:nil];
     else 
        self.viewController = [[PBSViewController alloc] initWithNibName:@"PBSViewController_iPad" bundle:nil];
    
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;

【问题讨论】:

【参考方案1】:

要从模态变为“正常”,您应该在模态控制器后面显示“正常”控制器(通常在您的根导航控制器中),然后关闭模态。将导航控制器作为窗口的根视图控制器使生活更轻松,因为它可以是模式的宿主。在任何时候,您都不应该尝试使用从模态到“正常”的转换,它始终是对模态的忽略。如果有意义的话,模态在屏幕层中处于概念上不同的级别...

【讨论】:

谢谢,我将重建项目以使用导航控制器。似乎我从一开始就错了。干杯

以上是关于从模态视图呈现视图控制器给出错误的主要内容,如果未能解决你的问题,请参考以下文章

从模态呈现的视图控制器内部呈现 UIPopoverController

关闭从模态呈现的视图控制器翻转的视图控制器

进行模态搜索时出现错误的 ViewController(使用 ECSlidingViewController)

UIAlertController 错误当在 swift 中以模态呈现的视图控制器中调用时

参考从模态(objective-c)呈现视图控制器

从 Xib 呈现模态视图控制器