【问题标题】:How to create new view w/ webview from push of button如何通过按下按钮创建带有 webview 的新视图
【发布时间】:2025-12-19 14:55:06
【问题描述】:

更新的问题: 你好,所以我能够为此用途创建模态视图卷曲覆盖。但是我现在有一个问题,我可以单击 button1 并在 webView1 上使用 url1 拉出第二个视图,但是当我单击 button2 时,我也会在 webview 上收到 url1 的显示。下面是代码你能帮我解决我做错了什么吗?

FourthViewController.h 代码如下:

'

#import <UIKit/UIKit.h>


@interface FourthViewController : UIViewController {
    UIButton *openBlogButton, *openFbButton, *openYelpButton;
}

@property (nonatomic, retain) IBOutlet UIButton *openBlogButton, *openFbButton, *openYelpButton;


-(IBAction)openBlog:(id)sender;
-(IBAction)openFacebook:(id)sender;
-(IBAction)openYelp:(id)sender;

@end

'

FourthViewController.m 代码如下:

'

#import "FourthViewController.h"
#import "FourthBlogWebViewController.h"
#import "FourthFBWebViewController.h"


@implementation FourthViewController

@synthesize openBlogButton, openFbButton, openYelpButton; 

-(IBAction)openBlog:(id)sender {
    FourthBlogWebViewController *sampleView = [[[FourthBlogWebViewController alloc] init] autorelease];
    [sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentModalViewController:sampleView animated:YES];
}

//THIS IS WHERE I THINK THE ISSUE IS, WHEN I CLICK openFacebook it should lead to the fb URL, it seems as if I should be able to list all possible urls on the first fourthwebviewcontroller, but i was not able to be successful so I tried making a second webview controller to feed it this url but this failed...lol. thnx for any input.
-(IBAction)openFacebook:(id)sender {
    FourthFBWebViewController *sampleView = [[[FourthFBWebViewController alloc] init] autorelease];
    [sampleView setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [self presentModalViewController:sampleView animated:YES];
}

-(IBAction)openYelp {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.yelp.com"]];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
    [openBlogButton release];
    [openFbButton release];
    [openYelpButton release];
    [super dealloc];
}


@end

'

FourthBlogWevViewController.h

'

#import <UIKit/UIKit.h>


@interface FourthBlogWebViewController : UIViewController {
    UIButton *dismissViewButton;
    IBOutlet UIWebView *webViewFB;
}

@property (nonatomic, retain) IBOutlet UIButton *dismissViewButton;
@property (nonatomic, retain) UIWebView *webViewFB;

- (IBAction)dismissView:(id)sender;

@end'

FourthBlogWebViewController.m code:

'#import "FourthBlogWebViewController.h"


@implementation FourthBlogWebViewController

@synthesize dismissViewButton, webViewFB;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
    }
    return self;
}

-(IBAction)dismissView:(id)sender {
    [self dismissModalViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    NSString *urlAddress = @"http://www.facebook.com";  
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webViewFB loadRequest:requestObj];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {
    [super viewDidUnload];
}


- (void)dealloc {
    [dismissViewButton release];
    [webViewFB release];
    [super dealloc];
}


@end

我将在何处或如何添加第二个 url 并使其在单击其他按钮时进行区分。提前感谢您一直以来的帮助。


原问题: 你好,

所以第一个应用程序,我正在尝试使用按钮的一些基本功能,我在标签栏应用程序中执行此操作。在我的第四个选项卡视图中,打开一个带有 3 个按钮的视图,单击该按钮时,我试图在应用程序中加载一个网页(我能够通过 safari 在单击按钮内的按钮修饰上成功打开网页,但是不是我希望它加载的方式。所以我编辑了它的加载方式,按下按钮,网站现在在 webView 中打开。但是我想在新视图上创建这个 webview,而不是在 3按钮是,但我不清楚如何做到这一点。任何输入将不胜感激(我事先道歉,因为这似乎是一项简单的任务,但还没有弄清楚,我在询问之前做了一些搜索,但是无法找到任何显示如何在按下不同按钮以在同一 XIB 内的新视图上加载 web 视图时执行此操作的任何内容。

谢谢。

下面是我在按下按钮时加载 webview 的代码。

在第四视图控制器.h

#import <UIKit/UIKit.h>

@interface FourthViewController : UIViewController {
    IBOutlet UIWebView *webView;
}

-(IBAction)openWebsite:(id)sender;
-(IBAction)openButton2;
-(IBAction)openButton3;

@property (nonatomic, retain) UIWebView *webView;

@end

在第四视图控制器.m

#import "FourthViewController.h"

@implementation FourthViewController

@synthesize webView;

-(IBAction)openWebsite:(id)sender{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.URL.com"]]];
}

-(IBAction)openButton2:(id)sender{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.URL2.com"]]];
}

-(IBAction)openButton3:(id)sender{
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.URL3.com"]]];
}

基本上,我试图让不同的按钮推送到同一个新视图,但唯一的区别是我希望 uiwebview 根据带来新视图的按钮单击显示不同的 URL。

【问题讨论】:

  • 在你得到这个答案之前,它会帮助你知道你是否想像 UINavigationController 那样将新视图推送到屏幕上,或者如果你想将它添加为像 presentModalViewController 这样的模态覆盖:animated: 做到了。
  • 感谢您的提问,我最初的想法是按照导航控制器的方式推送视图,但是在研究了模态叠加替代方案之后,这实际上会更好。提前感谢您提供的任何帮助。
  • 所以我想出了如何进行模态叠加视图,但遇到了第二个问题。如下所列。

标签: iphone xcode ios4 uiwebview interface-builder


【解决方案1】:

正如 NWCoder 所提到的,这取决于您希望如何显示新的 web 视图。

在 UINavigationController 中显示-

  1. 将第四个选项卡设为 uinavigationcontroller,并将 3 按钮视图控制器设置为最顶层的视图控制器。
  2. 创建一个封装 UIWebView 的新 UIViewController。
  3. 当用户点击打开 webview 按钮时,创建并推送包含 uiwebview 的 uiviewcontroller

在模态视图中显示:

  1. 创建一个封装 UIWebView 的新 UIViewController。
  2. 当用户单击打开 webview 按钮时,通过调用第四个选项卡 uiviewcontroller presentModalViewController:animated 创建并推送 webview uiviewcontroller。

【讨论】:

  • 您好,所以我能够为此用途创建模态视图 curl up 覆盖。但是,我现在遇到的问题是,我可以单击 button1 并使用 webView1 上的 url1 拉出第二个视图,但是当我单击 button2 时,我也会在 webview 上收到 url1 的显示。下面是代码你能帮我解决我做错了什么吗?
  • FourthFBWebViewController有实现文件吗?我看到了 FourthBlogWevViewController 的实现。在 FourthBlogWevViewController 的 viewDidLoad 中,它正在加载 facebook url。对吗?
  • 我已经为FourthFBWebViewController 制作了一个实现文件,但我真的希望这不是问题的解决方案。我试图只提供不同的按钮推送以产生从 FourthViewController 到 FourthBlogWebViewController 的新视图,而不是必须为每个按钮制作一个全新的控制器只是为了调出一个 webview 来根据用户单击的按钮更改 url .但是如果我将 OpenFacebook IB 操作更改为在单击时使用 FourthBlogWebViewController 的问题我仍然打开另一个 URL 而不是 facebook,任何代码示例示例都有帮助
  • 您正在对 FourthBlogWevViewController:viewDidLoad 中的 FB url 进行硬编码。您只需根据单击的按钮将 FB 或博客 url 传递到 FourthBlogWevViewController。