我曾尝试使用与我遇到的问题类似的答案。我是 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”
然后使用:
// 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/