【问题标题】:View is not loading after dismissViewControllerAnimated关闭视图控制器动画后视图未加载
【发布时间】:2014-02-15 23:13:06
【问题描述】:

我的 firstViewController 中有一个 webView。 我让用户在另一个viewController(secondViewController)中更改url中的一些参数。

我正在展示第二个视图控制器以让用户执行此操作。 当他完成时(他正在单击“完成”按钮并调用dismissViewControllerAnimated), 我想获取 url 更改并将其加载到视图控制器 webView。但是 webView 似乎没有被加载......这就是问题所在。

这是一个例子。

firstViewController.h:

.......
#import "SecondViewController.h"
.....
@property (strong, nonatomic) IBOutlet UIWebView *webView;
.....

firstViewController.m:

@synthesize webView;

SecondViewController *csvc = [self.storyboard instantiateViewControllerWithIdentifier:@"second"];
csvc.OLDurlInWebView = webView.request.URL.absoluteString;
csvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:csvc animated:YES completion:nil];
......
......
......
//I'm calling this method after dismissViewControllerAnimated from secondViewControllerClass
-(void)showInView:(NSString *)str {
    [self view];
    [self loadView];
    [self view];
    [self openURL:[NSURL URLWithString:str]];
    UIActionSheet *sheet = [[UIActionSheet alloc]initWithTitle:@"TEST" delegate:nil cancelButtonTitle:@"TEST" destructiveButtonTitle:nil otherButtonTitles:@"TEST", nil];
    NSLog(@"current:%@ isLoaded:%d",webView.request.URL.absoluteString,self.isViewLoaded); //output 'current:(null) isLoaded:1'
    [sheet showInView:self.view];//this sheet test will not show, the app will crash:'Sheet can not be presented because the view is not in a window:.......'
}
-(void)openURL:(NSURL *)url {
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];

    NSLog(@"Trying to open %@ in webbrowser", url);

    [webView loadRequest:request];

}

secondViewController.h:

.......
#import "FirstController.h"
.......
@interface SecondViewController : UIViewController {
NSString *OLDurlInWebView;
.......
}
.......
@property (strong, nonatomic) NSString *OLDurlInWebView;
- (IBAction)doneButton:(id)sender;
.......

secondViewController.m:

@synthesize OLDurlInWebView;

......
- (IBAction)doneButton:(id)sender {
    [self dismissViewControllerAnimated:NO/*YES*/ completion:nil]; //completion???
}
-(void)viewDidDisappear:(BOOL)animated {
    NSLog(@"SecondViewC didDisappear");
    WebViewController *wb = [self.storyboard instantiateViewControllerWithIdentifier:@"web"];

    NSString *newurl = @"http://facebook.com"; //let the user load facebook as example
    [wb view];
    [wb loadView]; //testing everything, nothing works
    [wb view];

    [wb performSelector:@selector(showInView:) withObject:newurl afterDelay:5];//the webView will not load after 5 seconds. Why?
}

好的,从头开始:

在 firstViewController 中:

我正在展示第二个视图控制器。

现在我们在 secondViewController 中:

用户现在将更改 url,但在本例中,他会将 url 更改为 facebook.com。

现在我们将把这个新的 url (facebook.com) 发回给 firstViewController。用户 点击 doneButton 和dismissViewControllerAnimated 将被调用。

尝试使用这个新的 url(facebook) 回到 firstViewController。

我已经测试了我所知道的一切,但这还不够。我已经测试过从 secondViewController 调用 firstViewController 中的正常函数。 webView 将无法加载请求。

然后我测试将代码放入 viewDidDisappear 并让视图加载 5 秒,但它不起作用。

前段时间,当我的应用程序在应用程序委托中处理 URL 方案链接时,我遇到了这个问题。 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 例如,如果您不想使用 URL 方案链接中的 url 或文本加载另一个 webView/label? 如何从应用程序委托执行此操作?这是和上面一样的问题。

我做错了什么/我不知道什么? 如何“等待”视图加载,然后能够加载例如带有 url 的 webView。?

【问题讨论】:

    标签: ios uiview uiviewcontroller webview


    【解决方案1】:

    必须在第一个 VC 上调用 Dismiss。你可以像这样从第二个开始做:

    [self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
    

    此外,要获取信息,您可以使用委托或第一个视图中的属性:

    解散前调用:

    [self.presetingViewController setMyProperty:urlToPassBack];
    

    【讨论】:

    • setMyProperty 将是presetingViewController 上的一个属性。你可以在继承 UIViewController 时定义它,然后将其用作呈现的 viewController。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多