ios - xcode 8 创建了第二个 Controller 作为弹出窗口,但只是黑屏

标签 ios objective-c xcode uiwebview

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

- (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

然后对于我的第二个 Controller ,它应该加载不同的网页但只是黑屏:

- (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 周的时间来搜索和尝试在此论坛上找到的不同解决方案,但它们都不适合我。不确定我是不是没有正确实例化我的 Controller ,还是缺少一些代码。感谢您提前抽出时间。

最佳答案

这一行:

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

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

假设您在 Storyboard中设计了 PopUpController,为其指定一个标识符,例如“MyPopUp”

enter image description here

然后使用:

// 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];

关于ios - xcode 8 创建了第二个 Controller 作为弹出窗口,但只是黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44637351/

相关文章:

ios - 快速等待异步函数返回值

iphone - Objective-C 中的 pi

iphone - NSString 到 NSNumber

ios - 验证应用程序时出现 iTunes 商店操作失败错误

ios - 如何在 swift 中制作动画(xcode 6 beta)

ios - 在移动 safari `position:fixed` 上覆盖 `platform=ios` 卡在 <ion-header> 后面

iphone - UIScrollView类型访问UIWebView

objective-c - 用于自动续订订阅的 MKStoreKit 4.0

ios - 如何使用按钮在 Google ios sdk map 中设置放大和缩小

ios - 为什么我的 Xcode 无法验证 swift iOS 项目