xcode 无法让 presentViewController 工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10147625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:22:07  来源:igfitidea点击:

Unable to get presentViewController to work

iphonexcodeipadpresentmodalviewcontroller

提问by user278859

I copied a working viewcontroller class from another project into a new project. I can't get the view to load in the new project. In the old project I used presentModalViewController. In the new I cannot get the view to load using either presentModalViewController or presentViewController

我将另一个项目中的一个工作视图控制器类复制到一个新项目中。我无法在新项目中加载视图。在旧项目中我使用了presentModalViewController。在新的我无法使用 presentModalViewController 或 presentViewController 加载视图

I am trying to load the present the view from my main view controller.

我正在尝试从我的主视图控制器加载当前视图。

Here is what my main view controller interface looks like...

这是我的主视图控制器界面的样子......

//  ViewController.h
#import <UIKit/UIKit.h>
#import "RequestDialogViewController.h"

@interface ViewController : UIViewController <RequestDialogViewControllerDelegate> {

}

- (void)requestDialogViewDidDismiss:(RequestDialogViewController *)controller withResponse:(NSString*)response;

I am using presentModalViewController like this...

我正在像这样使用 presentModalViewController ...

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"  bundle:nil];
navigationController = [[UINavigationController alloc] initWithRootViewController:requestIPViewController];
[self presentModalViewController:navigationController animated:YES];

and presentViewController like this...

和presentViewController这样的......

RequestDialogViewController *requestIPViewController = [[RequestDialogViewController alloc] initWithNibName:@"RequestDialogViewController"  bundle:nil];    
[self presentViewController:requestIPViewController animated:YES completion:nil];

What am I missing in the new project? The init method fires, but viewDidLoad does not and nothing is displayed.

我在新项目中缺少什么?init 方法会触发,但 viewDidLoad 不会触发,也不会显示任何内容。

Thanks

谢谢

回答by Dondragmer

If ViewControlleris the root view controller, it can't present a modal view controller from within its own viewDidLoad, because at that point it doesn't have information like the screen size.

如果ViewController是根视图控制器,它不能从它自己的内部呈现模态视图控制器viewDidLoad,因为此时它没有像屏幕大小这样的信息。

If other view controllers have already displayed, this will work. If the root view controller is a UINavigationController, you will see a view sliding in from the right while the modal view slides up from the bottom.

如果其他视图控制器已经显示,这将起作用。如果根视图控制器是 a UINavigationController,您将看到一个视图从右侧滑入,而模态视图从底部向上滑动。

Anyway, for your ViewController, the soonest you could present it is after it has become visible. Using a timer for this is unreliable; older and slower devices have dramatically longer load times.

无论如何,对于您的ViewController,您可以在其可见后尽快呈现它。为此使用计时器是不可靠的;较旧和较慢的设备具有更长的加载时间。

For more reliability, implement viewDidAppear:for ViewController. Do still use your timer system to add an additional delay; a fraction of a second should be sufficient. Although presenting the modal view controller from within viewDidAppearworked for me in the iOS 5.1 simulator, Presenting a modal view controller when loading another ViewControllersays it sometimes doesn't happen.

为了获得更高的可靠性,请实现viewDidAppear:for ViewController。仍然使用你的计时器系统来增加额外的延迟;几分之一秒就足够了。尽管viewDidAppear在 iOS 5.1 模拟器中从内部呈现模态视图控制器对我有用,但在加载另一个 ViewController 时呈现模态视图控制器表示有时不会发生。

回答by user278859

I have it resolved. I was trying to present the view from view did load of the main view controller. Not sure why it does not work there, but instead I am now setting a timer which calls a method to present the view controller after the main view loads and it works fine now using...

我已经解决了。我试图从视图中呈现视图确实加载了主视图控制器。不知道为什么它在那里不起作用,但我现在正在设置一个计时器,该计时器调用一个方法在主视图加载后呈现视图控制器,现在使用...

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

Thanks to those who replied.

感谢回答的人。

回答by SeanChense

As @Dondragmer said, if you want to present your viewController in root view's viewDidLoad, it will fail.Once your viewController is ready for that, you can present your new viewController. So, you can do that in

正如@Dondragmer 所说,如果您想在根视图的viewDidLoad 中展示您的viewController,它将失败。一旦您的viewController 准备好,您就可以展示您的新viewController。所以,你可以在

- (void)viewDidLayoutSubviews {
    //present here
}

回答by Bagusflyer

I encountered the same problem. But my situation is the presentViewControlleris called after the dismissViewControllerAnimatedfor another ViewController. My solution is to move the presentViewControllerto completion block of dismissViewControllerAnimated.

我遇到了同样的问题。但我的情况是在另一个 ViewControllerpresentViewController之后调用dismissViewControllerAnimated。我的解决方案是移动presentViewController到结束块dismissViewControllerAnimated

回答by Nate Symer

Present a modalViewController:

呈现一个 modalViewController:

For the benefit of all starting programmers, type it instead of copy paste.

为了所有初学者的利益,请输入它而不是复制粘贴。

myVC *viewController = [[myVC alloc]initWithNibName:@"myVC" bundle:nil];
viewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:viewController animated:YES];
[viewController release];

It looks like you were trying to present a nav controller as a view controller in the first sample, then you were using the wrong method in the second one.

看起来您试图在第一个示例中将导航控制器呈现为视图控制器,然后在第二个示例中使用了错误的方法。