rmaddy 的意思是你写的解释和代码显示的不一样。我知道您说您有两个视图控制器,但您只显示了一个的代码。您实际上是在创建一个视图,该视图直接位于另一个视图之上并且具有相同的尺寸。您将永远不会以这种方式看到下面的 Web 视图。如果你做这样的事情,你应该看到两个 webview,一个在另一个之上。这会告诉你那里确实有两个,但一个只是隐藏在另一个后面:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) IBOutlet UIWebView *webView2;
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height/2)];
NSString *url=@"http://test.bithumor.co/test26.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
webview.scrollView.bounces = NO;
[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];
// Do any additional setup after loading the view, typically from a nib.
UIWebView *webview2=[[UIWebView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height/2, self.view.frame.size.width,self.view.frame.size.height/2)];
NSString *url2=@"http://google.com";
NSURL *nsurl2=[NSURL URLWithString:url2];
NSURLRequest *nsrequest2=[NSURLRequest requestWithURL:nsurl2];
[webview2 loadRequest:nsrequest2];
webview2.scrollView.bounces = NO;
[self.view addSubview:webview2];
[self.view bringSubviewToFront:webview2];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];
编辑 1:
如果你真的想要两个不同的视图控制器,你需要这样做:
文件 1:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=@"http://test.bithumor.co/test26.php";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
webview.scrollView.bounces = NO;
[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];
文件 2:
#import "ViewController2.h"
@interface ViewController2 ()
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController2
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSString *url=@"http://www.google.com";
NSURL *nsurl=[NSURL URLWithString:url];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
webview.scrollView.bounces = NO;
[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];
如果您使用的是故事板或 nib,那么您可能希望将每个视图控制器设置为不同的类。一个将设置为 ViewController,另一个将设置为 ViewController2
编辑 2:
要在一个文件中执行此操作,您可以执行以下操作:
ViewController.h:
...
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *urlString;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@end
@implementation ViewController
@synthesize scrollView;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
draw1 = 0;
scrollView.frame = CGRectMake(0, 300, 480, 55);
[scrollView setContentSize:CGSizeMake(480, 55)];
openMenu.frame = CGRectMake(220, 270, 60, 30);
UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
NSURL *nsurl=[NSURL URLWithString:self.urlString];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
webview.scrollView.bounces = NO;
[self.view addSubview:webview];
[self.view bringSubviewToFront:webview];
[self.view bringSubviewToFront: openMenu];
[self.view bringSubviewToFront: scrollView];
您展示 Web 视图控制器的文件:
...
ViewController *webViewController = [[ViewController alloc] init];
webViewController.urlString = @"http://www.google.com";
[self presentViewController:webViewController animated:YES completion:nil];
或者如果它是通过 segue 呈现的,你可以在 prepareForSegue 方法中设置 urlString 属性
...
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"yourSegueIDHere"]) {
ViewController *webViewController = (ViewController *)segue.destinationViewController;
webViewController.urlString = @"http://www.google.com";
}
}
...