【问题标题】:Open different Web page with button on single ViewController使用单个 ViewController 上的按钮打开不同的网页
【发布时间】:2017-02-16 10:28:24
【问题描述】:

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

第一个视图控制器有按钮:button1、button2 和 button3。

然后在第二个视图控制器中:

  • 如果按钮 1 被点击:mysite.com
  • 如果按钮 2 被点击:mysite.com/page
  • 如果按钮 3 被点击:mySite.com/Page2

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

我想要这样的总结:

【问题讨论】:

  • 你能添加一些代码和屏幕截图吗?

标签: ios swift xcode button webview


【解决方案1】:

是的,您可以通过点击不同的按钮在同一个网页视图中打开多个链接,例如

 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 App Transport Security Settings 中设置允许任意加载 true

【讨论】:

  • 如何命名我的按钮?在继续?
  • 我正在使用 swift .. "CGRectMake" 在 Swift 中不可用
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
  • 1970-01-01
  • 2019-09-22
相关资源
最近更新 更多