I have a question about xcode7.x Swift. (1. What is container view? 2. Can selecting a cell in table view affect the disconnected table?)

Asked 2 years ago, Updated 2 years ago, 27 views

Hello, I am a student who is currently making an ios application for the first time.

Currently, the screen I want to make is divided into three parts and displayed at once. What I'm curious about is that 'Split View Controller' is not that meaningful unless it's an iPad. When I turned the simulator, I couldn't see the screen split apart except for 6s plus... So the way I found it was Container View, and I wanted to see that the screen was divided into separate sections. It was good up to that point, but since it's my first time using it, I wonder if it's right to use it or if I use it properly and efficiently. Of course, I saw it once on Apple's official site, but I don't understand it well.

Second, as the cell of the table view on the left is selected among the three divided screens, I would like to know how to call the pre-created view in the right container view.

ios swift

2022-09-22 21:16

1 Answers

1 - What is Container View?

A view that allows you to configure reusable views that can be shared by all controllers. Before the container view, I used reusable views through UIView, but I couldn't see them separately on the storyboard, so it's a complementary view.

2 - It's a little complicated, but you can call up the view.

In the parent view, determine the container view by the name of the container view through prepareForSegue. Importantly, prepareForSegue is called before container view is called, and the class of container view is called. If it's a container view that requires an initial value, you can set the initial value here.

import UIKit

class ViewController: UIViewController, VcTableDelegate {

    var vc_button : Vc_button? = nil
    var vc_table : Vc_table? = nil

    override func viewDidLoad() {
        super.viewDidLoad()
        // // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // // Dispose of any resources that can be recreated.
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
    {

        //Called here and then moved to the class connected to each container view to be called viewDidLoad()
        if (segue.identifier == "buttonview") {
            vc_button = segue.destinationViewController as? Vc_button
            //vc_button?.delegate = self
            vc_button?.str_title = "Hello World"

        } } else if (segue.identifier == "tableView") {
            vc_table = segue.destinationViewController as? Vc_table
            vc_table?.delegate = self
        }
    }

    // pass the value through the delegate
    func func_cellSelect(str: String) {
        self.vc_button!.func_recevieTableCell(str)
    }
}

Vc_Button Class

import UIKit

class Vc_button: UIViewController {

    var str_title = ""

    @IBOutlet var lb_title: UILabel!
    @IBOutlet var iv_suji: UIImageView!
    @IBOutlet var view_back: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        lb_title.text = str_title
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    //Change UI with received values
    func func_recevieTableCell(str : String) {

        switch str {
        case "Replace Label":
            lb_title.text = str
        case "Show pictures":
            iv_suji.image = UIImage(named: "a2.jpg")
        case "Replace View Background":
            view_back.backgroundColor = UIColor.yellowColor()
        default:
            break
        }
    }
}

After setting the table view in the Vc_table class, you can change the value to the container view next to you through the DELEGATE as the cell selects it.

import UIKit

protocol VcTableDelegate : class {
    func func_cellSelect(str : String)
}

class Vc_table: UIViewController, UITableViewDelegate, UITableViewDataSource {

    weak var delegate:VcTableDelegate?

    @IBOutlet var tb_table: UITableView!
    let arr_list: [String] = ["Replace Label", "Show Picture", "Replace View Background"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // // Do any additional setup after loading the view.
        tb_table.delegate = self
        tb_table.dataSource = self
        tb_table.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // // Dispose of any resources that can be recreated.
    }


    //MARK: table view delegates, data sources
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return arr_list.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath)
        cell.textLabel?.text = arr_list[indexPath.row]
        return cell

    }

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        //Call method to parent view with delegate
        delegate?.func_cellSelect(arr_list[indexPath.row])
        tableView.deselectRowAtIndexPath(indexPath, animated: true)
    }

    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 50;
    }

}

Download the full code through [Link][1] and check it out. (This is a dropbox link.)

Also, please refer to the basic setting for container view here.

Please let me know in the comments if you have any questions or if you can't. I'll check and answer as far as I know.

And if there is anything wrong, please feel free to request editing. :)

[1]: https://www.dropbox.com/s/nsh43f1b61u5z2c/contaierview.zip


2022-09-22 21:16

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.