-2

I am new to macOS app writing so I'm doing something with storyboard. I took an existing app source demo I found on the web and I'm modding it. I've learned quite a bit from playing with it but I haven't found all I need. Much of the online help is for iOS apps and doesn't entirely transfer over. Others are old and obsolete.

Here's what I have so far.

I have a windowcontroller with a child viewcontroller which contains a tableview and a customised class derived from an image view. I am using this so I can customise drag and drop.

The table view in the sample has a delegate which reads from an array to populate the table. I can add, subtract, etc. text items from the array, call the tableview's reloadData() method and it populates the table. That bit works great.

What I want do to is when I drag an image onto my custom image view class is that it takes the filename of the image and adds it to the table view of the parent.

One way I suppose I could do it is with shared data but that's rather messy. A cleaner way would be for the child custom imageview to send an event to the parent and the parent handling the event. Only I do not know how to do this. I saw something using protocols but that was for iOS and wouldn't transfer properly.

I'm using macOS Montery and Xcode 14.2.

I found something on protocols which i copied, pasted and changed class names. However, some properties/methods were missing. Most online code of this nature seems to be written for iOS.

4
  • 1
    Does this answer your question? Passing data between view controllers Commented Apr 28, 2024 at 15:27
  • You need to update your question showing what you have tried and what issues you are having. The linked question certainly does answer your question. But without any details about your specific issue, it's impossible to help. Commented Apr 28, 2024 at 20:23
  • Regrettably no. although i was able to remap data types to suit macos, i wasn't trying to pass data, i was attempting to raie an event in a parent. i got what should work but doesnt as the created deligate has a value of nil. it doesn't appear to initialise. protocol ChildViewControllerDelegate { func invokeSignal(imageView: MyImageClass) } --- inwould post more of the code buy i am limited by character count. Commented Apr 28, 2024 at 20:28
  • @FrankBaron edit the question and post your code in the question. Commented Apr 29, 2024 at 9:32

1 Answer 1

-1

Not the most ideal, but my solution was to add an IBAction for my custom imageview class in my viewcontroller.

     @IBAction func ImageHandler(_ sender: MyImageClass)
        {
            if(sender.DraggedFile != "")
            {
                let thisFile:URL = URL(fileURLWithPath: sender.DraggedFile )
                filesList.append(thisFile)
                tableView.reloadData()
            }
        }

Then put a property in my custom class called DraggedFile, which is set inside the class when a drag/drop operation is completed. It works pretty well.

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need an IBAction. Use a function with the parameters you need. Something like func addImage(withURL:).
i needed the two to talk to each other. the function you mentioned would not have acheived that. The ibAction works smoothly so i'm happy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.