gpt4 book ai didi

ios - 在 Objective C 中通过按钮触摸传递 URL

转载 作者:行者123 更新时间:2023-11-29 12:42:01 25 4
gpt4 key购买 nike

我正在为我的学校构建一个基本应用程序,但无法弄清楚如何根据按下的按钮更改要显示的 URL。我有四个按钮,目前它们都链接到临时 URL,但是当我运行我的应用程序时,webview 显示为空白,我的 NSLog 为 URL 返回 null。我究竟做错了什么?这是我的代码:

TWViewController.h:

#import <UIKit/UIKit.h>

@interface TWViewController : UIViewController

- (IBAction)gradesPressed:(id)sender;
- (IBAction)facPressed:(id)sender;
- (IBAction)newsPressed:(id)sender;
- (IBAction)sportsPressed:(id)sender;

@property (strong, nonatomic) NSURL *url;

@end

TWViewController.m:

#import "TWViewController.h"
#import "TWWebViewController.h"

@interface TWViewController ()

@end

@implementation TWViewController

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

- (IBAction)gradesPressed:(id)sender{
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:@"http://google.com"];
}

- (IBAction)facPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:@"http://yahoo.com"];
}

- (IBAction)newsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:@"http://facebook.com"];
}

- (IBAction)sportsPressed:(id)sender {
TWWebViewController *webView = [[TWWebViewController alloc] init];
webView.website = [NSURL URLWithString:@"http://apple.com"];
}


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

//- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//
// TWWebViewController *wbc = (TWWebViewController *)segue.destinationViewController;
//}

@end

TWWebViewController.h:

#import <UIKit/UIKit.h>

@interface TWWebViewController : UIViewController
{
UIWebView *webView;
NSString *website;
}
@property (strong, nonatomic) IBOutlet UIWebView *webView;
@property (strong, nonatomic) NSURL *website;
@end

TWWebViewController.m:

#import "TWWebViewController.h"
#import "TWWebViewController.h"

@interface TWWebViewController ()

@end

@implementation TWWebViewController

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



- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:self.website];
[self.webView loadRequest:urlRequest];
NSLog(@"%@", website);
// Do any additional setup after loading the view.
}

我是 iOS 开发的新手,所以我不太确定我是否在正确的轨道上。任何帮助将不胜感激,谢谢!

最佳答案

你需要在初始化后推送你的 Controller :此外,你的 NSString 不是 NSURL 对象,所以在你的 TWWebViewController 中也这样做:NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:self.website]] ;

TWWebViewController.m:

- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:
[NSURL URLWithString:self.website]];
[self.webView loadRequest:urlRequest];
}

TWViewController.m:

- (IBAction)sportsPressed:(id)sender 
{
TWWebViewController *webView = [self.storyboard
instantiateViewControllerWithIdentifier:@"WebView"];
webView.website = @"http://apple.com";

[self.navigationController pushViewController:webView animated:YES];
}

另外不要忘记在您的 Storyboard文件中执行此操作:

enter image description here

关于ios - 在 Objective C 中通过按钮触摸传递 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24618900/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com