如何分配每个IBAction(在第二个ViewController中)以在TableView中的特定选择下执行?

问题描述 投票:1回答:1

免责声明:这是新问题,将尽我所能描述问题。

“应用程序截图”

我正在创建一个由TableView组成的应用程序,该应用程序的第1周到第8周。当用户做出选择时,该应用程序会进入一个ViewController,每个选择看起来都一样。但是,此第二个视图控制器中的按钮应将用户发送到特定于所选星期的URL。目前,该应用程序在选定的每周显示相同的链接。我基本上每个星期都需要有一组与该周相对应的唯一URL。

已附加我的代码,我还尝试附加一张该应用程序应执行的照片图。

import UIKit

struct WeekInfo {
    let description: String
    let url: URL
}

class WeekTableViewController: UITableViewController {

    var week = [
    WeekInfo(description: "Week 0:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 1:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 2:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 3:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 4: ", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 5:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 6:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 7:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Week 8:", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Final Prep", url: URL(string: "https://somewhere.com")!),
    WeekInfo(description: "Final Project", url: URL(string: "https://somewhere.com")!)

    ]


    override func viewDidLoad() {
        super.viewDidLoad()

        // Uncomment the following line to preserve selection between presentations
         self.clearsSelectionOnViewWillAppear = false

        // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
        // self.navigationItem.rightBarButtonItem = self.editButtonItem
    }




    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return week.count
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedWeek = week[indexPath.row]
        performSegue(withIdentifier:"moveToWeekDetail", sender:selectedWeek)
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()
        cell.textLabel?.text = week[indexPath.row].description
        return cell

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?){
        if let weekViewController = segue.destination as? WeekDetailViewController{
            // Pass the week's info onto the detail view controller
            if let selectedWeek = sender as? WeekInfo {
                weekViewController.title = selectedWeek.description
                weekViewController.week = selectedWeek
            }
        }
    }

下一个视图控制器:

import UIKit

class WeekDetailViewController: UIViewController {

    var week: WeekInfo!


    @IBOutlet weak var lessonCompleteBtn: UIButton!
    @IBOutlet weak var Button1: UIButton!
    @IBOutlet weak var Button2: UIButton!
    @IBOutlet weak var Button3: UIButton!
    @IBOutlet weak var Button4: UIButton!


    override func viewDidLoad() {
        super.viewDidLoad()


        lessonCompleteBtn.backgroundColor = UIColor.gray
        lessonCompleteBtn.layer.cornerRadius = 20

    }
    @IBAction func lessonBtnTapped(_ sender: AnyObject) {
        sender.setTitle("Complete!", for: [])
        if lessonCompleteBtn.backgroundColor == UIColor.gray{
            lessonCompleteBtn.backgroundColor = UIColor(red: 17/255, green: 107/255, blue: 0/255, alpha: 1.0)
        }
        else if lessonCompleteBtn.backgroundColor == UIColor(red: 17/255, green: 107/255, blue: 0/255, alpha: 1.0){
            sender.setTitle("Mark Complete", for: [])
            lessonCompleteBtn.backgroundColor = UIColor.gray
        }
    }

    @IBAction func Button1(_ sender: Any) {
        UIApplication.shared.open(URL(string: "https://resourelink1.com")! as URL, options: [:], completionHandler: nil)
    }
    @IBAction func Button2(_ sender: Any) {
        UIApplication.shared.open(URL(string: "https://resourcelink2.com")! as URL, options: [:], completionHandler: nil)
    }
    @IBAction func Button3(_ sender: Any) {
        UIApplication.shared.open(URL(string: "https://resourcelink3.com")! as URL, options: [:], completionHandler: nil)
    }
    @IBAction func Button4(_ sender: Any) {
        UIApplication.shared.open(URL(string: "https://resourcelink4.com")! as URL, options: [:], completionHandler: nil)
    }
}
ios swift uitableview
1个回答
-1
投票

然后将其设为类似数组

struct WeekInfo {
  let description: String
  let urls: [URL]
}


var week = [
WeekInfo(description: "Week 0:", urls:[ URL(string: "https://somewhere1.com")!, URL(string: "https://somewhere2.com")!]),
.......]

并且在第二个vc中使用它,如week.urls[0] //根据需要设置索引

© www.soinside.com 2019 - 2024. All rights reserved.