gpt4 book ai didi

iphone - 多个UIButton打开相同的 View 但内容不同

转载 作者:行者123 更新时间:2023-11-29 04:53:14 25 4
gpt4 key购买 nike

我正在寻找一种更快的方法来构建我的应用程序。因为我有一系列按钮,它们都会打开一个 UIWebView。但每个按钮都会转到不同的网站。

请问我是否可以只制作一个 xib 文件?或者我应该为每个按钮创建一个新文件?

这是我转到下一个 xib 的代码。

- (IBAction)items:(id)sender {

//toggle the correct view to be visible
Chelseapic *myView1 =[[Chelseapic alloc] initWithNibName:nil bundle:nil];
[myView1 setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:myView1 animated:YES];
}

这是在 WebView xib 中打开 URL 的代码。

- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = @"http://www.google.com" ;
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];

}

非常感谢

最佳答案

如果您想要在 Nib 中放置一组固定的按钮,请尝试为每个按钮创建一个单独的 IBAction:

// helper method
- (void)presentViewControllerForURL:(NSURL *)url
{
Chelseapic *vc = [[Chelseapic alloc] initWithNibName:nil bundle:nil];
vc.url = url;
[vc setModalTransitionStyle:UIModalTransitionStylePartialCurl];
[self presentModalViewController:vc animated:YES];
}

// Connect the first button in your nib to this action.
- (IBAction)presentGoogle:(id)sender
{
[self presentViewControllerForURL:[NSURL URLWithString:@"http://www.google.com/"]];
}

// Connect the second button in your nib to this action.
- (IBAction)presentStackOverflow:(id)sender
{
[self presentViewControllerForURL:[NSURL URLWithString:@"http://stackoverflow.com/"]];
}

// etc.

您需要为 Chelseapic 提供一个在 viewDidLoad 中使用的 url 属性,而不是在那里对 URL 进行硬编码。

关于iphone - 多个UIButton打开相同的 View 但内容不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8466536/

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