Home > Mobile >  how to access function properly?
how to access function properly?

Time:08-10

here i want to use this function which i have mention in custom subclass of uiview

this is messageviewcontroller file

import UIKit

    class MessageViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

        var MessageData : NSMutableArray!
        @IBOutlet weak var tableviee: UITableView!
        override func viewDidLoad() {
            super.viewDidLoad()
            
            MessageData = NSMutableArray()
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Silvestor Young", profileMessage: "Hi, I want to purchase your service, can you please provide it to me", Messagtime: "10 min ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Welma Simpson", profileMessage: "How much is it cost to repeate it again", Messagtime: "22 min ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Robert Doe", profileMessage: "It was really nice and calm", Messagtime: "1h ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Steve Adams", profileMessage: "I need an extra piece of this can you provide it to me.", Messagtime: "1 h ago"))

            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "Alizia Smith", profileMessage: "ok noted thanks", Messagtime: "3 h ago"))
            
            MessageData.add(getMessageListData(image: "ProfileImageRound", profileName: "JW Doe", profileMessage: "Thank you for your inquiry om this can you provide details for your so i can contact you", Messagtime: "3 h ago"))
            
            tableviee.register(UINib(nibName: "MessageMainCell", bundle: nil), forCellReuseIdentifier: "MessageMainCell")
            let messageBar = MessageBar.instanceFromNib() as! MessageBar
            self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: messageBar)
        }
        
        func getMessageListData(image: String, profileName: String, profileMessage: String, Messagtime: String) -> NSMutableDictionary{
            let data = NSMutableDictionary()
            data["image"] = image
            data["profileName"] = profileName
            data["profileMessage"] = profileMessage
            data["Messagetime"] = Messagtime
            return data
        }
        
        func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
            return MessageData.count
        }
        
        func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
            return 100
        }
        
        func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
            let cell = tableView.dequeueReusableCell(withIdentifier: "MessageMainCell", for: indexPath ) as! MessageMainCell
            if let setData = MessageData.object(at: indexPath.row) as? NSDictionary{
                cell.setListdata(data: setData)
            }
            return cell
        }
        
        func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
            tableView.deselectRow(at: indexPath, animated: true)
            let stroryBoard = UIStoryboard(name: "Main", bundle: nil)
            let rootVc = stroryBoard.instantiateViewController(withIdentifier: "MessageDetailViewController") as! MessageDetailViewController
            let backImage = UIImage(named: "backArrowWhite")
            self.navigationController?.navigationBar.backIndicatorImage = backImage
            self.navigationController?.navigationBar.backIndicatorTransitionMaskImage = backImage
            self.navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: UIBarButtonItem.Style.plain, target: nil, action: nil)
            self.navigationController?.navigationBar.tintColor = .black
            self.navigationController?.pushViewController(rootVc, animated: true)
        }
    }

import UIKit

class MessageDetailbar: UIView {

@IBOutlet weak var time: UILabel!
@IBOutlet weak var name: UILabel!
@IBOutlet weak var imageView: UIImageView!



class func instanceFromNib() -> UIView {
    return UINib(nibName: "MessageDetailbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
}

func setBarData(data: NSDictionary){
    if let image = data["image"] as? String{
        imageView.image = UIImage(named: image)
    }

    if let Name = data["profileName"] as? String{
        name.text = Name
    }

    if let Time = data["Messagetime"] as? String{
        time.text = Time
    }
}

}

but i am not able to use it how can use it proper way

i want to use custom navigation bar when on of the cell is selected [this] cell2 to [this] navigation bar 3

CodePudding user response:

You don't show your MessageDetailViewController or how you're passing the data from the selected index path, but assuming you have something like this:

class MessageDetailViewController: UIViewController {

public var theData: NSDictionary!

you'll be passing the data in didSelectRowAt like this:

    let stroryBoard = UIStoryboard(name: "Main", bundle: nil)
    let rootVc = stroryBoard.instantiateViewController(withIdentifier: "MessageDetailViewController") as! MessageDetailViewController
    
    // unwrap optional
    guard let setData = MessageData.object(at: indexPath.row) as? NSDictionary else { return }
    
    // pass the data to the Detail controller
    rootVc.theData = setData
    
    self.navigationController?.pushViewController(rootVc, animated: true)

Then, in viewDidLoad() in MessageDetailViewController:

class MessageDetailViewController: UIViewController {

    public var theData: NSDictionary!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // whatever else you're doing to congfigure
        //  this controller
        
        // get an instance of MessageDetailbar
        let messageDetailBar = MessageDetailbar.instanceFromNib() as! MessageDetailbar
        // set the data
        messageDetailBar.setBarData(data: theData)
        // set it as the navigation bar's titleView
        self.navigationItem.titleView = messageDetailBar

    }

}

As a side note, override intrinsicContentSize om your MessageDetailbar class so it will fill the available horizontal space:

class MessageDetailbar: UIView {
    
    @IBOutlet weak var time: UILabel!
    @IBOutlet weak var name: UILabel!
    @IBOutlet weak var imageView: UIImageView!
    
    class func instanceFromNib() -> UIView {
        return UINib(nibName: "MessageDetailbar", bundle: nil).instantiate(withOwner: nil, options: nil)[0] as! UIView
    }
    
    func setBarData(data: NSDictionary){
        if let image = data["image"] as? String{
            imageView.image = UIImage(named: image)
        }
        
        if let Name = data["profileName"] as? String{
            name.text = Name
        }
        
        if let Time = data["Messagetime"] as? String{
            time.text = Time
        }
    }

    // add this
    override var intrinsicContentSize: CGSize {
        return UIView.layoutFittingExpandedSize
    }

}

CodePudding user response:

I'm not sure if I understand it right, you are trying to reach to custom view that is somehow used as a navigationBar. You want to update that custom view according to cell selected in tableview.

There are couple of ways to do that but you can't directly access that setBarData function if you don't have any reference in your viewController.

Can you show us where you use that MessageDetailbar in your code?

  • Related