Post

Replies

Boosts

Views

Activity

Reply to Use of local variable ".." before is declaration!!
@RatKill In the following code: import UIKit class RegisterPageViewController: UIViewController { @IBOutlet weak var repeatPasswordTextField: UITextField! @IBOutlet weak var userPassportTextField: UITextField! @IBOutlet weak var userEmailTextField: UITextField! override func viewDidLoad() { super.viewDidLoad() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } @IBAction func registerButtoTapped(sender: AnyObject) { let userEmail = userEmailTextField.text let userPassword = userPassportTextField.text let userRepeatPassword = repeatPasswordTextField.text if userEmail.isEmpty || userPassword.isEmpty || userRepeatPassword.isEmpty { displayMyAlertMessage("all fields are requirede"); // ⚠ return; } if(userPassword != userRepeatPassword) { displayMyAlertMessage("Password do not match"). // ⚠ return; } NSUserDefaults.standardUserDefaults().setObject(userEmail, forKey: "user Email") NSUserDefaults.standardUserDefaults().setObject(userPassword, forKey: "userPassword") NSUserDefaults.standardUserDefaults().synchronize(); var myAlert = UIAlertController(title: "Alert", message: "Registration is sucesfull. Thank you!", preferredStyle: UIAlertControllerStyle.Alert); let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){ action in self.dismissViewControllerAnimated(true, completion: nil) } func displayMyAlertMessage(userMessage:String) { var myAlert = UIAlertController(title: "Alert", message: userMessage, preferredStyle: UIAlertControllerStyle.Alert); let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil); myAlert.addAction(okAction) self.presentViewController(myAlert, animated: true, completion: nil); } } As the func is defined inside another func, it must be defined before use. But if the func displayMyAlertMessage were defined at the top level of the class, it could be declared after use. That's due how the compiler processes the code. Interesting discussion here: https://forums.swift.org/t/improve-nested-functions-visibility-or-order-dependency/33935/8 But anyway, correcting the error is so easy that I advise not to bother too much. That's the limits of the compiler which is already very smart. Note: in the previous code, the label for argument was missing in the func use.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to UIButton embedded in view which is then embedded in UIstackview not working
I tested and ran into the same problem. In the stack, I added: the button in the view : does not react to touch a button directly in stack: it reacts to touch. I have tried many of solutions proposed here, but yet to no avail: https://stackoverflow.com/questions/39167337/unresponsive-uibutton-in-subview-added-to-uistackview I looked at the view hierarchy and there is no view above the button… Maybe the problem is in the size constraints for all elements, but I was not yet able to find out. I'll continue to search. EDIT It finally worked, when setting size constraints on the view ! That is apparently due to the way StackView handles it own layout. Even though the view appears correct in IB (but in view debugger it is effectively zero size, but the button is there !), internally in Stack, it was considered zero size. Here the configuration: StackView Axis Vertical Alignement Leading Distribution Fill View: scale to Fill User Interaction Enabled Constraints : set position and size UIView Mode Scale to Fill User interaction enabled Constraints : set size THE CAUSE OF THE PROBLEM ! DOES NOT WORK WITHOUT THIS Button User interaction enabled of course constraints : set position Thanks to report here and close the thread if it works. Good luck.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to I get the error "Unexpectedly found nil while implicitly unwrapping an Optional value" when calling a protocol method
The instance of StoreViewController that you use in storeView let storeView = StoreViewController() is not the one that was loaded from the storyboard when you loaded the controller. It is a new instance. And so there is no link between the button you tap and storeView. In addition, your code is really painful to read: class should start with UpperCase you should avoid giving the same name to the protocol and a func of the protocol. Just to illustrate what I mean: protocol LabelMaker { func makeLabel(value: String) } class StoreViewController: UIViewController { var labelDelegate: LabelMaker! @IBAction func viewersTapped(_ sender: UIButton) { labelDelegate.makeLabel(value: sender.currentTitle!) } } class LiveViewController: UIViewController { let storeView = StoreViewController() // To be changed as OOPer explained @IBOutlet var onlineViewers: UILabel! override func viewDidLoad() { super.viewDidLoad() storeView.labelDelegate = self } } extension LiveViewController: LabelMaker { func makeLabel(value: String) { onlineViewers.text = value }
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Perform Segue before view is presented
Do you ever show VC2 ? If not, why do you need passing through it ? To initialize some values ? To load some data ? If that's it, I think you have the best design. The code you have in VC2 should be in a model and called directly from VC3. However, if you really need to do so, don't segue from VC1 to VC2 but instantiate VC2 then call perform segue. let storyboard = UIStoryboard(name: "Main", bundle: nil) let vc2 = storyboard.instantiateViewController(withIdentifier: "ViewController2") as! ViewController2 vc2.loadViewIfNeeded() See details here: https://stackoverflow.com/questions/25745854/how-to-initiate-a-viewcontroller-without-presenting-it
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to How can I get access to the timer and change the color individually?
Your message seems to lack the code part… You cannot change the color of hours and minutes independently, as there is a single subview. If you do this         for view in countDownTimer.subviews {             view.setValue(UIColor.red, forKeyPath: "textColor")         } both will turn red, as there is a single subview. You should use UIPickerView to customise more. Read here: https://stackoverflow.com/questions/37388633/how-to-customise-the-uidatepicker-components-in-swift
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to How to prevent an iPhone-only app to run on iPad at Landscape mode?
In Xcode, where you select Portrait only, you need an extra step. Select iPad only (deselect iPhone) You will see 4 directions authorized keep only Portrait Then select iPhone and iPad again. I tested in Simulator it makes it work (and did not work before). Found more details here: https://stackoverflow.com/questions/30429276/locking-orientation-does-not-work-on-ipad-ios-8-swift-xcode-6-2 That would be worth a bug report. Don't forget to close the thread if that works.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Streaming response with URLSession
Not sure to understand exactly what you want, but looks like dispatchGroup could be useful there. I found those tutorials well done about it: https ://www.raywenderlich. com/5370-grand-central-dispatch-tutorial-for-swift-4-part-1-2  https ://www.raywenderlich. com/5371-grand-central-dispatch-tutorial-for-swift-4-part-2-2 Hope that helps. If so, don't forget to close the thread.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to Read ARP table on macOS (Swift)?
Could this provide some help ? https ://www.xspdf. com/resolution/54077675.html I'm not sure it is possible to get this address from IP address (seems not to be possible anymore on iOS). How to get the MAC address of an iOS/iPhone programmatically?, In iOS versions previous to 7.0 getting the MAC address of device was possible. But with new iOS version it has been disabled for the apps to access MAC address In swift we can use. UIDevice.current.identifierForVendor When you request the Device MAC address in iOS 7 and above you will always get the same response: 02:00:00:00:00:00, this has been made by Apple for privacy concerns. In iOS 7 and later, if you ask for the MAC address of an iOS device, the system returns the value 02:00:00:00:00:00.
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to ignoreSafeArea on some devices
What do you want precisely ? know if there is a notch ? => You could check the top notch: https://stackoverflow.com/questions/46192280/detect-if-the-device-is-iphone-x Need to know the exact model: => Hope this could help https://stackoverflow.com/questions/26028918/how-to-determine-the-current-iphone-device-model
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’21
Reply to find the index item in an array
You could also use map: 				let boolArray = array.map {$0 is Medic} 				let index = boolArray.firstIndex(of: true) ?? -1 				print("Medic at", index) Or, in a more condensed form (here to find Sniper) 				let sniperIndex = array.map {$0 is Sniper}.firstIndex(of: true) ?? -1 				print("Sniper at", sniperIndex)
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to find the index item in an array
I did the following small demo to show: class Player { 		var name: String = "Player" } class Medic : Player { 		override init() { 				super.init() 				name = "Medic" 		} } class AssaultSoldier : Player { 		override init() { 				super.init() 				name = "AssaultSoldier" 		} } class Sniper : Player { 		override init() { 				super.init() 				name = "Sniper" 		} } then     var array = [Medic(), AssaultSoldier(), Sniper()]    let thereIsMedic = array.filter {$0 is Medic }.count > 0 	 print(thereIsMedic) And to get the position of Medic: 				for (pos, item) in array.enumerated() { 						if item is Medic { 								print("position of Medic is", pos) 								break 						} 				} If that's what you're looking for, don't forget to close the thread
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21
Reply to Adding stacks to a stack view programmatically
Here is a small example. Created a LabelStepperView class. I add label and stepper here. class LabelStepperView: UIView { 		var textForLabel : String? 		var label: UILabel! 		var stepper: UIStepper! 		var viewFrame: CGRect? 		var aleatColor : UIColor = .white 		required init(coder aDecoder: NSCoder) { 				 				let wholeFrame = viewFrame ?? .zero 				super.init(coder: aDecoder)! 				self.label = UILabel(frame: wholeFrame) 				self.addSubview(label) // Add label 				 				self.stepper = UIStepper(frame: wholeFrame) // Just to have something ; will be done in draw 				stepper.maximumValue = 4.0 				let action = UIAction(title: "Stepper") { (action) in 						// Change color randomly 						self.backgroundColor = self.aleatColor 				} 				self.stepper.addAction(action, for: .valueChanged) 				self.addSubview(stepper) // add stepper 		} 		override func draw(_ rect: CGRect) { 				// Drawing code 				 				let redAleat = CGFloat.random(in: 0.5 ... 1.0) 				let greenAleat = CGFloat.random(in: 0.5 ... 1.0) 				let blueAleat = CGFloat.random(in: 0.5 ... 1.0) 				let alphaAleat = CGFloat.random(in: 0.5 ... 1.0) 				self.aleatColor = UIColor(red: redAleat, green: greenAleat, blue: blueAleat, alpha: alphaAleat) 				let wholeFrame = self.bounds 				var smallFrame = wholeFrame 				var stepperFrame = wholeFrame 				 				smallFrame.origin.y = wholeFrame.midY - 10 				smallFrame.size.width = wholeFrame.width / 2 				smallFrame.size.height = 20 				label.frame = smallFrame 				self.label.text = stepper.value > 0 ? "Now \(self.stepper.value)" : "Hello" 				stepperFrame.origin.x = wholeFrame.width / 2 + 4 				stepperFrame.origin.y = label.frame.origin.y 				stepper.frame = stepperFrame 		} } In the VC, I create a UIView of that class (I did it in IB for simplicity, but you can do it by code). class SomeViewController: UIViewController { 		@IBOutlet weak var labelStepper: LabelStepperView! 		 		override func viewDidLoad() { 				 				super.viewDidLoad() 				labelStepper.viewFrame = labelStepper.frame 				labelStepper.textForLabel = "From Gear" 				self.view.setNeedsDisplay() 		} } In your case, you should add several LabelStepperView to your stack feedStack.addArrangedSubview(labelStepperView)
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’21