Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 在iOS编程中以不同的视图加载Webview_Iphone_Ios_Browser - Fatal编程技术网

Iphone 在iOS编程中以不同的视图加载Webview

Iphone 在iOS编程中以不同的视图加载Webview,iphone,ios,browser,Iphone,Ios,Browser,我一直在为iOS编写一个Web浏览器应用程序,但偶然发现了一个问题。起初,我的搜索文本字段和web视图位于同一视图中(一切正常:)。当我将web视图放在另一个视图中时,web视图不会加载页面并保持空白。因此,问题是web视图不会加载到不同的视图中(如果文本字段和web视图位于同一视图中,则会起作用) 我的代码: #import "ViewController.h" #import <QuartzCore/QuartzCore.h> @interface ViewController

我一直在为iOS编写一个Web浏览器应用程序,但偶然发现了一个问题。起初,我的搜索文本字段和web视图位于同一视图中(一切正常:)。当我将web视图放在另一个视图中时,web视图不会加载页面并保持空白。因此,问题是web视图不会加载到不同的视图中(如果文本字段和web视图位于同一视图中,则会起作用)

我的代码:

#import "ViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface ViewController ()

@end

@implementation ViewController

@synthesize searchButton;




- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib

}


-(IBAction)SearchButton:(id)sender {
    NSString *query = [searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://www.google.com/search?q=%@", query]];
                                       NSURLRequest *request = [NSURLRequest requestWithURL:url];
                                       [webView loadRequest:request];

    ViewController *WebView = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];
    [self presentViewController:WebView animated:YES completion:nil];
  }


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
#导入“ViewController.h”
#进口
@界面视图控制器()
@结束
@实现视图控制器
@综合搜索按钮;
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置
}
-(iAction)搜索按钮:(id)发件人{
NSString*query=[searchField.text stringByReplacingOccurrencesOfString:@''和字符串:@“+”];
NSURL*url=[NSURL URLWithString:[NSString stringWithFormat:@]http://www.google.com/search?q=%@“,查询]];
NSURLRequest*request=[nsurlRequestRequestWithURL:url];
[webView加载请求:请求];
ViewController*WebView=[self.storyboard实例化eviewcontrollerwhiteIdentifier:@“WebView”];
[self-presentViewController:WebView动画:是完成:无];
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
[自视图编辑:是];
}
-(无效)未收到记忆警告
{
[超级记忆警告];
//处置所有可以重新创建的资源。
}
@结束

SearchButton方法引用当前类中的webView,但它是即将显示的新ViewController(您称之为webView),并且应该包含UIWebView。因此,呈现的视图控制器上的UIWebView永远无法访问loadRequest

创建UIViewController的子类以包含web视图。(称之为MyWebViewController)它应该有一个名为urlString的属性。确保将当前在序列图像板中绘制的视图控制器的类更改为MyWebViewController

SearchButton方法应如下所示:

// renamed to follow convention
- (IBAction)pressedSearchButton:(id)sender {

    NSString *query = [searchField.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    NSString *urlString = [NSString stringWithFormat:@"http://www.google.com/search?q=%@", query];

    // remember to change the view controller class in storyboard
    MyWebViewController *webViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"WebView"];

    // urlString is a public property on MyWebViewController
    webViewController.urlString = urlString;
    [self presentViewController:webViewController animated:YES completion:nil];
}
新类可以从URL字符串中形成请求

// MyWebViewController.h
@property (nonatomic, strong) NSString *urlString;
@property (nonatomic, weak) IBOutlet UIWebView *webView;  // be sure to attach in storyboard

// MyWebViewController.m

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    NSURL *url = [NSURL URLWithString:self.urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webView loadRequest:request];
}

你能解释一下为什么不能吗?只要重读这篇文章,我很确定我理解这个问题,而且这种解决方法是正确的。