ios - 使用单个 ViewController 上的按钮打开不同的网页

标签 ios swift xcode button webview

我知道如何在我的应用程序中实现 webview,现在我只想使用一个 ViewController 来打开我网站的不同网页,如下所示:

第一个 View Controller 有按钮:button1、button2 和 button3。

然后在第二个 View Controller 中:

  • 如果点击了 button1:mysite.com
  • 如果点击按钮 2:mysite.com/page
  • 如果点击了 button3:mySite.com/Page2

到目前为止,我已经为每个类别创建了一个 viewcontroler,所以我有很多类别,这更令人沮丧。

我想要这样的总和: enter image description here

最佳答案

enter image description here是的,您可以通过单击不同的按钮在同一 WebView 中打开多个链接,例如

 myWebView = UIWebView(frame: CGRectMake(0,0,   self.view.frame.size.width,self.view.frame.size.height-100))

        myWebView.delegate = self
        self.view.addSubview(myWebView)

    let Button1 = UIButton(frame: CGRectMake(10,self.view.frame.size.height-50,50,50))
         Button1.setTitle("Link 1", forState: .Normal)

        Button1.setTitleColor(UIColor.redColor(), forState: .Normal)
        Button1.addTarget(self, action: #selector(button1Action), forControlEvents: .TouchUpInside)

         self.view.addSubview(Button1)


        let Button2 = UIButton(frame: CGRectMake(150,self.view.frame.size.height-50,50,50))
         Button2.setTitle("Link 1", forState: .Normal)
        Button2.setTitleColor(UIColor.redColor(), forState: .Normal)

        Button2.addTarget(self, action: #selector(button2Action), forControlEvents: .TouchUpInside)

        self.view.addSubview(Button2)

         let Button3 = UIButton(frame: CGRectMake(250,self.view.frame.size.height-50,50,50))
         Button3.setTitle("Link 1", forState: .Normal)
        Button3.setTitleColor(UIColor.redColor(), forState: .Normal)

        Button3.addTarget(self, action: #selector(button3Action), forControlEvents: .TouchUpInside)

        self.view.addSubview(Button3)
     }

     func button1Action()
    {
        let myUrl = NSURL(string: "https://www.google.co.in")

        let request = NSMutableURLRequest(URL: myUrl!)
       myWebView.loadRequest(request)

    }

     func button2Action()
    {

         let myUrl = NSURL(string: "http://stackoverflow.com/questions/tagged/ios")
        let request = NSMutableURLRequest(URL: myUrl!)
        myWebView.loadRequest(request)

    }

    func button3Action()
    {
        let myUrl = NSURL(string: "http://stackoverflow.com/questions/39916960/open-diffrent-webpage-with-button-on-single-viewcontroller")
        let request = NSMutableURLRequest(URL: myUrl!)
        myWebView.loadRequest(request)

    }

并且不要忘记在 info.plist 应用程序传输安全设置中设置允许任意加载 true enter image description here

关于ios - 使用单个 ViewController 上的按钮打开不同的网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39916960/

相关文章:

swift - ObjectMapper toJSON() 快速

c++ - 我是否必须在 Xcode iOS 项目中释放 C 分配的内存?

c++ - Xcode 5.1 针对 C++98 编译,避免 C++11 特性

iphone - 在 UITextView 中的单词末尾添加点

ios - 为什么 View Controller 的头文件中需要一个 Action 方法声明?

ios - UISlider 自定义缩略图图像大小调整问题

ios - 在 Objective-c 中访问二维 NSArray 中的值

objective-c - 在 Storyboard 中创建独立 View 以编程方式使用

swift - 如何创建模态动画,其中向上滑动屏幕上半部分,向下滑动屏幕下半部分

ios UITableview 单个单元格上的多个选择选项?