【问题标题】:xcode 8 created a second controller as a popup but just get black screenxcode 8 创建了第二个控制器作为弹出窗口,但只是黑屏
【发布时间】:2017-11-22 01:41:38
【问题描述】:

我尝试使用类似的答案来解决我遇到的问题。我是 Xcode 的新手,我有一个控制器,其中包含一个加载我的网站的 uiwebview。我有编码来检测是否按下了某个链接。我遇到的问题是当按下某个链接时,我需要我的第二个控制器充当弹出窗口并显示不同的网页。一切正常,并且该链接被按下我的 NSLog 说它在第二个控制器中,但显示为黑色。这是我在加载我的网页的主视图控制器中的代码:

- (void)viewDidLoad {
   [super viewDidLoad];
   self.webViewer.delegate=self;
   NSString *fullURL = @"https://www.example.com?first_run=true";
   NSURL *url = [NSURL URLWithString:fullURL];
   //NSLog(@"first page loaded=%@",url);
   NSURLRequest *requestObj = [NSURLRequest requestWithURL: url];
   [_webViewer loadRequest:requestObj];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
   self.webViewer.delegate=self;
   NSURL *strurl = request.URL;
   NSString *urlString = strurl.relativeString;
   NSLog(@"link clicked=%@",urlString);
   if(//not the right link do this){
   }
   else{//is the right link do this
      NSLog(@"found");
      self.popup = [[PopUpController alloc] init];
      [self.popup showInView:self.view animated:YES];
      return NO;
   }
}
@end

那么对于我的第二个控制器,它应该加载不同的网页,但只是得到一个黑屏:

- (void)viewDidLoad {
   self.popupView.delegate=self;
   NSString *popupURL = @"https://www.example2.com";
   NSURL *url_popup = [NSURL URLWithString:popupURL];
   NSLog(@"popup page loaded=%@",url_popup);
   NSURLRequest *requestObj2 = [NSURLRequest requestWithURL: url_popup];
   [_popupView loadRequest:requestObj2];
}
- (void)showAnimate{
   self.view.transform = CGAffineTransformMakeScale(1.3, 1.3);
   self.view.alpha = 0;
   [UIView animateWithDuration:.25 animations:^{
      self.view.alpha = 1;
      self.view.transform = CGAffineTransformMakeScale(1, 1);
   }];

}
- (void)showInView:(UIView *)aView animated:(BOOL)animated{
   CGFloat x = self.view.center.x - (aView.frame.size.width);
   CGFloat y = self.view.center.y - (aView.frame.size.height);
   [aView setFrame:CGRectMake(x,y,aView.bounds.size.width,aView.bounds.size.height)];
   [aView addSubview:self.view];
   if (animated) {
      [self showAnimate];
   }
}

任何帮助将不胜感激。我已经尝试了 3 周来搜索和尝试在这个论坛上找到的不同解决方案,但它们都不适用于我的。不确定我只是没有正确实例化我的控制器还是我缺少的一些代码。感谢您的宝贵时间。

【问题讨论】:

    标签: ios objective-c xcode uiwebview


    【解决方案1】:

    这一行:

    self.popup = [[PopUpController alloc] init];
    

    只创建一个PopUpController 的实例,但不会使用它加载任何 UI 元素。

    假设您在故事板中设计了PopUpController,给它一个标识符,例如“MyPopUp”

    然后使用:

    // storyboard reference
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @"Main" bundle:[NSBundle mainBundle]];
    
    // instantiate PopUpController from the storyboard
    self.popup = (PopUpController *)[storyboard instantiateViewControllerWithIdentifier:@"MyPopUp"];
    
    // show it
    [self presentViewController:self.popup animated:YES completion:nil];
    

    【讨论】:

    • 我收到此错误:WebKit 在 webView:decidePolicyForNavigationAction:request:frame:decisionListener:delegate: Storyboard () 中丢弃了未捕获的异常不包含视图控制器带有标识符“MyPopUp”。我将故事板标识符命名为 MyPopUp,但它仍然说它不存在
    • "不包含标识符为 'MyPopUp' 的视图控制器" ...您需要将标识符分配给情节提要中的视图控制器 - 不是 重命名故事板。在我编辑的答案中查看图片...
    • 非常感谢 DonMag!这解决了它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-18
    • 2020-10-30
    • 1970-01-01
    • 2018-06-04
    相关资源
    最近更新 更多