假设我有一个带有 3 个按钮的应用程序,每个按钮打开 3 个不同的 UIWebView View Controller 。我不想为每个按钮使用 3 个单独的 View Controller ,而是希望拥有 1 个 UIWebView View Controller ,并且根据按下的按钮,这就是 UIWebView 上将显示的内容。
这只是我正在谈论的一个例子
第一个ViewController
按钮 1 打开雅虎
按钮 2 打开 Google
按钮 3 打开 Bing
第二 View Controller
如果按下按钮1,则在 UIWebView 上显示 Yahoo 如果按下按钮2,则在 UIWebView 上显示 google 如果按下button3,则在UIWebView上显示bing
我是怎么想到这个的?
最佳答案
在 iOS 中,每个对象都包含自己的 标签
如果您有兴趣使用标签或其他选项。
假设您的 button1.tag=10、button2.tag=20 和 button3.tag=30
在.h文件中设置全局字符串
NSString *activecheck;
// assign the single method for all buttons in touchup inside method
- (IBAction)button_GetDeals:(UIButton*)sender {
switch (sender.tag)
{
case 10:
activecheck=@"1";
break;
case 20:
activecheck=@"2";
break;
case 30:
activecheck=@"3";
break;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"secondviewcontrolleridentidiername"]) {
secondViewController *destViewController = segue.destinationViewController;
destViewController. buttontype = activecheck;
}
//这是你的第二个 View Controller
@interface secondViewController : UIViewController
@property (nonatomic, strong) NSString *buttontype;
@property (strong, nonatomic) IBOutlet UIWebView *webview;
@end
在你的 .m 文件中 viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
if ([buttontype isEqualtoString:@"1"])
NSString *strURL = @"http://www.google.com";
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webiew loadRequest:urlRequest];
}
就像遵循 bing 和 yahoo 的另外两个条件......
关于ios - 如何在一个 UIViewController 下的 Xcode 上显示多个 Web View 链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29247679/