เปิดเว็บเพจอื่นด้วยปุ่มบน ViewController เดียว

ฉันรู้วิธีใช้งาน webview ในแอปของฉัน และตอนนี้ฉันต้องการใช้ ViewController เพียงอันเดียวเพื่อเปิดหน้าเว็บต่างๆ ในเว็บไซต์ของฉัน เช่นนี้

วิวคอนโทรลเลอร์ตัวแรกมีปุ่ม: ปุ่ม 1, ปุ่ม 2 และปุ่ม 3

จากนั้นในตัวควบคุมวิวที่สอง:

  • หากคลิกปุ่ม 1: mysite.com
  • ถ้าคลิกปุ่ม 2: mysite.com/page
  • ถ้าคลิกปุ่ม 3: mySite.com/Page2

จนถึงตอนนี้ ฉันได้สร้าง viewcontroler หนึ่งอันต่อหมวดหมู่ ดังนั้นฉันจึงมีหลายประเภทและมันน่าหงุดหงิดกว่ามาก

ฉันต้องการสิ่งนี้: ป้อนคำอธิบายรูปภาพที่นี่


person Ninder Chand    schedule 07.10.2016    source แหล่งที่มา
comment
คุณสามารถเพิ่มโค้ดและภาพหน้าจอได้หรือไม่?   -  person nishith Singh    schedule 07.10.2016


คำตอบ (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 เพื่ออนุญาตการโหลดตามอำเภอใจจริงป้อนคำอธิบายรูปภาพที่นี่

person mukul    schedule 07.10.2016
comment
ฉันจะตั้งชื่อปุ่มของฉันได้อย่างไร? ตามมาเหรอ? - person Ninder Chand; 07.10.2016
comment
ฉันใช้ Swift .. CGRectMake ไม่พร้อมใช้งานใน Swift - person Ninder Chand; 07.10.2016