【发布时间】:2014-12-11 20:02:05
【问题描述】:
我已经在这个问题上搜索了很长时间,但找不到解决我问题的答案。如果我遗漏了任何类似的问题并且有有效的解决方案,请发布指向它们的链接。
不管怎样……
我有一个充满按钮的菜单,单击这些按钮会打开一个 UIWebView,它会打开一个特定的网页。一切正常,但只有在第二次尝试之后。说得更清楚一点,我第一次点击按钮时,网页会卡住,但是当你关闭它并再次点击后,页面就可以正常加载了。
由于我使用了多个按钮,我决定将它们全部放在一个 IBOutlet 集合中:
@property (nonatomic, retain) IBOutletCollection(UIButton) NSArray *btnArray;
然后我设置了一个数组来保存集合中的所有按钮,为我们的 webview 类设置一个视图控制器(我们有一个单独的 webviews 类,这在过去没有给我造成任何问题)和webview 的导航控制器,最后我使用了一个for循环将所有按钮添加到数组中。
NSMutableArray *theButtons = [[NSMutableArray alloc] init];
webVC = [[WebviewViewController alloc] init];
webNav = [[UINavigationController alloc] initWithRootViewController:webVC];
for(int i = 0; i < [btnArray count]; i++)
{
UIButton *btn = btnArray[i];
btn.tag = i;
[theButtons addObject:btn];
[theButtons[i] addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
}
我还设置了一个数组,其中包含我需要访问的所有页面的结束链接,它将附加到一个 url:
pages = @[@"exOne.php", @"exTwo.php", @"exThree.php", @"exFour.php", @"exFive.php", @"exSix.php"];
注意:以上所有代码都在-viewDidLoad中
这是处理单击按钮时发生的情况的方法:
-(void)buttonPressed : (id) sender
{
UIButton *clicked = (UIButton *) sender;
NSMutableString *url = [NSMutableString stringWithString:@"http://www.example.org/examples/"];
for(int i = 0; i < [btnArray count]; i++)
{
if(i == clicked.tag)
{
[url appendString:pages[i]];
[webVC loadURL:url];
webNav.navigationBar.translucent = NO;
webNav.navigationBar.topItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backPressed)];
[self.navigationController pushViewController:webVC animated:YES];
}
}
}
如果从方法调用中不清楚
[webVC loadURL:url];
只需将链接发送到我们的 webviewViewController 类并加载链接。
关于为什么网页仅在第二次尝试后才加载的任何想法?
【问题讨论】: