Where do I call the Subclass? (reselectable UISegmentControl Swift 3) - swift

I have a subclass for a UISegmentedControl which is called SegControl in a Cocoa Touch Class.
class SegControl: UISegmentedControl {
override var = "test"
}
And now I have a UISegmentControl in my ViewController what do I have to do that this conforms to my custom class?
EDIT:
Full code for a Reselectable UISegmentControl for Swift 3
class ReselectableSegmentedControl: UISegmentedControl {
#IBInspectable var allowReselection: Bool = true
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
let previousSelectedSegmentIndex = self.selectedSegmentIndex
super.touchesEnded(touches, with: event)
if allowReselection && previousSelectedSegmentIndex == self.selectedSegmentIndex {
if let touch = touches.first {
let touchLocation = touch.location(in: self)
if bounds.contains(touchLocation) {
self.sendActions(for: .valueChanged)
}
}
}
}
}
UISegmentControl:
var savedSegment: Int = -1
#IBAction func segmentChanged(_ sender: ReselectableSegmentedControl) {
if (sender.selectedSegmentIndex == savedSegment) {
sender.selectedSegmentIndex = UISegmentedControlNoSegment
savedSegment = UISegmentedControlNoSegment
}
else {
savedSegment = sender.selectedSegmentIndex
}

Your question isn't entirely clear but to start you need to replace any references to UISegmentedControl with SegControl. This includes updating any references in code as well as changing the class name of the object in any storyboard or xib.
You are also going to need to add the proper init methods to your subclass so you can create instances of it.
And of course you need to added whatever additional code that makes having this subclass useful.

add a default UISegmentControl in your layout builder
then click on it and in the side menu under identity inspector -> class
write your class name SegControl

Related

Swift 5 - Mac OS - NSTrackingArea overlapping views

Currently I have a little issue when it comes to buttons(NSButton) which have a tracking area and views(NSView overlay) above these buttons, this is my setup:
Custom button:
class AppButton: NSButton {
override func updateTrackingAreas() {
super.updateTrackingAreas()
let area = NSTrackingArea(
rect: self.bounds,
options: [.mouseEnteredAndExited, .activeAlways],
owner: self,
userInfo: nil
)
self.addTrackingArea(area)
}
override func mouseEntered(with event: NSEvent) {
NSCursor.pointingHand.set()
}
override func mouseExited(with event: NSEvent) {
NSCursor.arrow.set()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
An instance of this button class is used i a very basic NSView.
When I hover over the button, the cursor changes correctly.
When I click the button a new overlay(NSView) is opened above the button...
This is where the problem starts:
When I hover over the overlay where my button is placed, the cursor still changes...
I did not know that a NSTrackingArea is going through all views..
How can i solve this issue?
Can I set any property on the overlay(NSView) to somehow disable the NSTrackingArea on the button?
Thanks!!
You can subclass NSView and add local monitoring for events. Check if the event has occurred over the view and if true return nil. This will avoid propagating the events being monitored. If the event is outside the view frame you can propagate it normally returning the event monitored.
class CustomView: NSView {
override func viewWillMove(toSuperview newSuperview: NSView?) {
super.viewWillMove(toSuperview: newSuperview)
wantsLayer = true
layer?.backgroundColor = NSColor.windowBackgroundColor.cgColor
layer?.borderWidth = 1
NSEvent.addLocalMonitorForEvents(matching: [.mouseEntered, .mouseExited, .leftMouseDown]) { event in
if self.frame.contains(event.locationInWindow) {
// if cursor is over the view just return nil to do not propagate the events
return nil
}
return event
}
}
}
If you are trying to stop mouse events from a viewController, add this code in viewDidLoad
NSEvent.addLocalMonitorForEvents(matching: [.mouseEntered, .mouseExited, .leftMouseDown]) { event in
if self.view.frame.contains(event.locationInWindow) {
return nil
}
return event
}

How to detect changes in a UIImageView and change a Boolean when this happens

I'm using UIPresentationController to prevent the user from accidentally closing a UIViewController presented modally if the user has made any changes. Everything works as it should when it comes to UITextFields since I detect the changes with .editingChanged. A sample code is shown below. I have an UIImageView where the user can change to provide a profile photo. I can enable the save button (rightBarButtonItem) once the user has uploaded an image using didFinishPickingMediaWithInfo in UIImagePickerController but that would not prevent the UIViewController from closing accidentally. Ideally, I would like to change the value of the hasChanges var but it is a get-only property.
var hasChanges: Bool {
guard let customer = customer else { return false }
if
firstNameTextField.text!.isNotEmpty && firstNameTextField.text != customer.firstName
// additional textfields etc…
{
return true
}
return false
}
override func viewWillLayoutSubviews() {
// If our model has unsaved changes, prevent pull to dismiss and enable the save button
let hasChanges = self.hasChanges
isModalInPresentation = hasChanges
saveButton.isEnabled = hasChanges
}
#objc func cancel(_ sender: Any) {
if hasChanges {
// The user tapped Cancel with unsaved changes
// Confirm that they really mean to cancel
confirmCancel(showingSave: false)
} else {
// No unsaved changes, so dismiss immediately
sendDidCancel()
}
}
#objc func textFieldDidChange(_ textField: UITextField) {
if hasChanges {
navigationItem.rightBarButtonItem?.isEnabled = true
} else {
navigationItem.rightBarButtonItem?.isEnabled = false
}
}
func setupTextFieldDelegates() {
let textFields = [all the textfields are included here]
for textField in textFields {
textField.addTarget(self, action: #selector(textFieldDidChange(_:)), for: .editingChanged)
}
}
To achieve this, you have to create a custom UIImageView, where you have to create a delegate variable and a protocol with a method and need to override the image variable.
when override the image variable you have to call the delegation method inside the didSet method of variable.
class CustomImageView: UIImageView{
override var image: UIImage?{
didSet{
delegate?.didChangeImage()
}
}
var delegate: ImageViewDelegate?
}
protocol ImageViewDelegate {
func didChangeImage()
}
Next in your ViewController set the delegate.
class ImageViewController: UIViewController {
#IBOutlet weak var imageView: CustomimageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.delegate = self
}
}
If you use the outlet from the storyboard, make sure to provide the custom class name to outlet. Otherwise it will not work.

Link in an Editable NSTextView (like Apple's Notes)

I have an NSTextView that is initially set as read-only like this:
taskDescription.isEditable = false
I set a click gesture recognizer in viewDidLoad() on it like this so that when the user clicks the field, it gets set as editable.
override func viewDidLoad() {
super.viewDidLoad()
let clickGesture = NSClickGestureRecognizer(target: self, action: #selector(setDescriptionEditState))
clickGesture.numberOfClicksRequired = 1
taskDescription.addGestureRecognizer(clickGesture)
}
And here is the function called on click:
#objc func setDescriptionEditState(){
//Make it editable
taskDescription.isEditable = true
//Don't show automatic link detect while editing (I want plain text)
taskDescription.checkTextInDocument(nil)
//Put the cursor in the NSTextView
taskDescription.window?.makeFirstResponder(taskDescription)
}
Then when I load an NSAttributedString into it with a link in it, it shows up fine:
Last of all, I implement this delegate method for NSTextView:
func textView(_ textView: NSTextView, clickedOnLink link: Any, at charIndex: Int) -> Bool {
print(link) //<-- Never called
}
What's happening is the clickGesture is getting called instead of the link click. I know this because if I manually set the NSTextView to isEditable = false then the link works fine.
How can I allow the link click to happen while still allowing the user to click on the NSTextView to switch it into edit mode?
--- Update ---
I set the content of this NSTextView with an NSAttributedString. When logged to the console, it looks like this:
I want {
NSColor = "...";
NSFont = "...";
}a big link{
NSColor = "...";
NSFont = "...";
NSLink = "google.com";
} here.{
NSColor = "...";
NSFont = "...";
}
So the link is specified with an NSLink.
The key is to override the function "touchesBegan" and implement your own logic.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first
guard let location = touch?.location(in: self.view) else { return }
if !popupContainer.frame.contains(location) {
goodTouch = true
} else {
goodTouch = false
}
}
The variable "goodTouch" is established as a class property. You can then check the status of this variable at the beginning of the functions in question and then route your application appropriately from there. (This was specifically adapted from an iOS project I wrote, but should be the same for Cocoa). Make sure if you implement this, you reset the "goodTouch" variable to false after inspecting it to be ready for the next round of touches.
EDIT
I think this is a more appropriate response to your specific example, but I didn't want to delete the code from above:
override func mouseDown(with event: NSEvent) {
let textField = NSTextField() //this is for demo only...reference your actual object
let location = event.locationInWindow
let cgPoint = CGPoint(x: location.x, y: location.y)
if textField.bounds.contains(cgPoint) {
//fire the link here, over ride the click gesture recognizer
} else {
//change the editable style of the text field
}
}

IOS Swift Protocol is not working or hitting breakpoint

I created a question a few days ago and was provided with protocol on how to solve an issue of passing data back and forth . I have also looked at some tutorials and have created a protocol but it is not working or even hitting the breakpoint from what I can see it should be working. I have created a protocol for my AVPlayer so that on tap it could get a new video but like i said it's not even hitting the breakpoint this is my code...
protocol CustomAVPLayerProtocol: class {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
// When I tap the AVPlayer this print statement shows
// So I know it is coming here
}
}
}
Now This is my second class/controller
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
print("This protocol method does not execute or hit breakpoint")
self.PlayVideo(250, "url")
}
func PlayVideo(MediaHeight: Float, MediaURL: String) {
let movieURL = URL(string: MediaURL)
videoCapHeight.constant = CGFloat(MediaHeight)
streamsModel.playerView = AVPlayer(url: movieURL!)
streamsModel.MyAVPlayer.player = streamsModel.playerView
streamsModel.MyAVPlayer.videoGravity = AVLayerVideoGravity.resizeAspectFill.rawValue
streamsModel.MyAVPlayer.showsPlaybackControls = false
streamsModel.MyAVPlayer.view.frame = VideoView.bounds
VideoView.addSubview(streamsModel.MyAVPlayer.view)
self.addChildViewController(streamsModel.MyAVPlayer)
streamsModel.playerView?.isMuted = false
streamsModel.MyAVPlayer.player?.play()
}
}
As I stated before it is not even hitting the breakpoint on BookViewC.reloadTabled as suggestions would be great
As per your code these are some minor mistakes which you can correct to make it work.
1. `weak var delagate: CustomAVPLayerProtocol?`
*Make a weak delegate to save it from retaining a strong reference cycle and memory leaks.*
2. Code Snippet:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self
}
in Your Second ViewController, You need to assign your delegate to an object / view controller to make ie respond to
NOTE: In case you require, you can make a super class that conforms the your protocol class, so that your every view controller conforms it automatically, you just need to assign an delegate to class on which you want to use it.
You have the foundation set up correctly but remember that classes are (mostly) just blueprints for instances. These classes are useless until you create instances of them because it’s the instances that will do the work.
Therefore, simply pass one instance as the delegate of the other, which you can do here because you've correctly set up the protocol.
protocol CustomAVPLayerProtocol: AnyObject {
func reloadTabled(at index: Int)
}
class CustomAVPLayerC: AVPlayerViewController {
weak var delagate: CustomAVPLayerProtocol?
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.delagate?.reloadTabled(at: 1)
for touch in touches {
self.delagate?.reloadTabled(at: 1)
print("Tapped")
}
}
}
class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
func reloadTabled(at index: Int) {
PlayVideo(250, "url")
}
func PlayVideo(_ MediaHeight: Float, _ MediaURL: String) {
//
}
}
let book = BookViewC()
let layer = CustomAVPLayerC()
layer.delagate = book
Where you do this instantiation/delegation is up to you. Also, I know that a lot of people here use class to define protocols that only conform to classes, but Swift's documentation instructs us to use AnyObject.
Protocols are the most common means used by unrelated objects to communicate with each other. As per the above code, the communication did not seem to happen.
Your protocol declaration part seems alright. The problem exists in the secondViewController. I can see that you have not set the delegate to the object that's been created. Ideally, it has to be something like this:
Class BookViewC: UIViewController, CustomAVPLayerProtocol {
override func viewDidLoad() {
super.viewDidLoad()
PlayVideo(250, "url")
}
You need to the set the delegate here:
func PlayVideo {
let customPlayer = CustomAVPLayerC()
customPlayer.delegate = self //This makes the selector to respond
}

How to access var of an extension in another class?

I´m working with Sprite Kit. I want to change the button picture of the main class with the settings class. How can I make the variable in the extension (from the setting class) available for the main class?
Here is the extension:
extension ChangingDots {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches{
let locationUser = touch.location(in: self)
if atPoint(locationUser) == DCButton {
var blackdot = SKSpriteNode(imageNamed: "AppIcon") //<--var I want to use
}
}
}
}
Here is the use in the main class:
blackdot.setScale(0.65)
blackdot.position = CGPoint(x: CGFloat(randomX), y: CGFloat(randomY))
blackdot.zPosition = 1
self.addChild(blackdot)
Does anyone have a better idea of changing button pictures of one class from another?
If you want to use a variable in the main class, it needs to be created in the main class. Extensions are designed to extend functionality, this means functions and computed properties.
To find out more, see this documentation:
https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Extensions.html
Just to add on here, there are ways to "workaround" adding stored properties in an extension. Here is an article that describes how to do it: https://medium.com/#ttikitu/swift-extensions-can-add-stored-properties-92db66bce6cd
However, if it were me, I would add the property to your main class. Extensions are meant to extend behavior of a class. It doesn't seem to be the right design approach to make a main class DEPENDENT on your extension.
If you want to make it private, you can use fileprivate so your extension can access it but maintain it's "private" access. For example:
fileprivate var value: Object? = nil
Sorry but you make me realize that you can't add stored properties in extensions. You can only add computed properties. You can add your blackdot var as a computed property or declare it in your main class instead of your extension. If you want to try the computed way, use this :
extension ChangingDots {
var blackdot:Type? { // Replace Type with SKSpriteNode type returned
return SKSpriteNode(imageNamed: "AppIcon")
}
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
super.touchesBegan(touches, with: event)
for touch in touches {
let locationUser = touch.location(in: self)
if atPoint(locationUser) == DCButton {
blackdot = SKSpriteNode(imageNamed: "AppIcon") //<--var I want to use
}
}
}
}
This way your blackdot var is only gettable and NOT settable. If you want to add the possibility to set it you need to add a setter like this :
var blackdot:Type? { // Replace Type with SKSpriteNode type returned
get {
return SKSpriteNode(imageNamed: "AppIcon")
}
set(newImage) {
...
}
}