I have UIViewController and I created my custom class for its view. By the way, I redefined method "canBecomeFirstResponder" in that class so it always returns true. I want my view to become first responder when the viewDidAppear method is called. But after I go to another controller and come back my program crashes. Can not understand why does this happens. Here is some code:
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
self.firstResponder = self.view.isFirstResponder()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if(self.firstResponder){
self.view!.becomeFirstResponder()
}
}
This link helped me a lot. https://stackoverflow.com/a/21906038/4092466 I should not have named the property "firstResponder". When I renamed it everything worked fine.
Related
1)videovc.swift
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.createTimerView()
}
2)MyvideosVC.swift
#objc func btnBackTapping(_ sender: UIButton) {
self.navigationController?.popViewController(animated: true)
}
on backtapping i want to hide createTimerView() function when viewDidAppear method call again, if Anyone can help that would be greatful Thakyou in Advance.
Move it in viewDidLoad and it will be called only on once per creation
override func viewDidLoad() {
super.viewDidLoad()
self.createTimerView()
}
So, There is concept called ViewController Lifecycle.
ViewDidLoad is method which gets called only once in the life cycle of ViewControlller
I would highly recommend you to go through this article:
https://medium.com/good-morning-swift/ios-view-controller-life-cycle-2a0f02e74ff5
For some reason, my viewWill/DidAppear methods aren't being called. Here is the way I'm presenting my view controller:
let profileViewVM = ProfileViewViewModel()
let profileViewController = ProfileViewController(viewModel: profileViewVM)
self.navigationController?.pushViewController(profileViewController, animated: true)
override func viewDidLoad() {
super.viewDidLoad() // <---- breakpoint set here gets hit
// do stuff
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) // <---- breakpoint set here does NOT get hit
// do stuff
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated) // <---- breakpoint set here does NOT get hit
// do stuff
}
Every other question I've seen with this issue has been something to do with not setting up a child controller correctly, not calling the super methods, or calling the wrong super message. This isn't a child controller - it's being presented by a navigationController, and as you can see I'm calling the correct super methods. I'd appreciate any input on why these aren't being hit, I'm stumped
I tried to use
func viewDidAppear(animated: Bool) {
self.myTableName.reloadData()
}
but it didn't work or reload when I went back to the page in the simulator. What am I doing wrong?
You need to add _ as first parameter label in Swift 3, so change your method with below one and add override prefix before it also call the super.viewDidAppear(animated) .
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.myTableName.reloadData()
}
I want to change button image whenever UISideMenuNavigationController Appear Or Disappear.
This is the class that has a button.
class MenuViewController: UIViewController {
#IBOutlet var btnMenu: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
}
This is the other class that i want to insert code.
open class UISideMenuNavigationController: UINavigationController {
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// insert some code here but from MenuViewController class
}
override open func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
// insert some code here but from MenuViewController class
}
}
I don't want to change UISideMenuNavigationController class because it is framework from pods.
I'm using framework side menu from https://github.com/jonkykong/SideMenu
I need to change button image whenever Side Menu Appear Or Disappear. I can't find the way from ReadMe Side Menu. That's why I think need to insert the code in ViewDidAppear and ViewDidDisappear Method from Side Menu Class but don't want to break the class.
You simply need to subclass UISideMenuNavigationController and override the viewDidAppear & viewDidDisappear methods to invoke a delegate.
protocol MyUISideMenuDelegate {
func menuDidAppear(_ menu:MyUISideMenuNavigationController) -> Void
func menuDidDisappear(_ menu:MyUISideMenuNavigationController) -> Void
}
open class MyUISideMenuNavigationController: UISideMenuNavigationController {
var menuDelegate: MyUISideMenuDelegate?
override open func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.menuDelegate?.menuDidAppear(self)
}
override open func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
self.menuDelegate?.menuDidDisappear(self)
}
}
Then have you view controller with the button implement the protocol and set itself as the delegate.
You could also have your menu subclass send NSNotification and have any other objects that are interested subscribe to those. This way you completely decouple the menu and the other classes.
Your MenuViewController class could have a function that changes the image on the button. Eg, func changeButtonMenuImage().
Your 'UISideMenuNavigationController' (or a subclass of it) could have some sort of connection to the MenuViewController either as a property, instance variable or an IBOutlet. Eg, #IBOutlet var menuController: MenuViewController
Then your viewDidAppear and viewDidDisappear can call it's function. Eg, menuController.changeButtonMenuImage()
I got my table view on top of a map view. I want to show to user that the tableView is scrollable. Here is my previos thread: Swift - How to attach bar to the top of table view?
I tried using
self.tableView.flashScrollIndicators()
in my ViewDidLoad() method but this doesn't change anything, the table view looks exactly the same as before. I read suggestion that it might be caused by reloading tableView and filling it with data, whilst created tableView is empty. Nonetheless I tried pasting the flashScrollIndicators() method in other project where table is created with cells immediately - again no significant difference.
Am I doing something wrong or using the method in wrong place?
If anyone is still fighting with this problem here is my working solution - works on iOS 11. In my case flashScrollIndicators() did not work on first invocation of viewDidAppear(animated:). Invoking flashScrollIndicators() with a delay will do the trick.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
DispatchQueue.main.asyncAfter(deadline: (.now() + .milliseconds(500))) {
self.collectionView.flashScrollIndicators()
}
}
You're using the method in the wrong place. In viewDidLoad, the view has just finished loading. It hasn't yet been displayed. A safe alternative would be to move the call into your view controller's viewDidAppear: method to make sure that you don't attempt to flash the scroll indicator until the view is already on screen.
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
self.tableView.flashScrollIndicators()
}
Swift 4
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.tableView.flashScrollIndicators()
}