refresh controller freeze after changing tap tab bar quickly - swift

I use refresh controller. It works well when I tap the tab bar item after the refresh animation completely end. But if I tap the bar item very quickly before the animation done, it will be freeze. I try to use refreshControl.endRefreshing() in viewDidApper. I try to use refreshControl.endRefreshing() in viewWillDisappear, too. This bug still happen. Below are my code and the snapshot.
snapshot: https://imgur.com/a/UNvQbA8
demo video: https://youtu.be/cZMurwiwfjI
let refreshControl: UIRefreshControl = UIRefreshControl()
#IBOutlet weak var myScrollView: UIScrollView!
#IBOutlet weak var noDataView: UIView!
#IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib.init(nibName: "MessageListCell", bundle: nil), forCellReuseIdentifier: "MessageListCell")
tableView.delegate = self
tableView.dataSource = self
refreshControl.addTarget(self, action: #selector(refresh), for: UIControl.Event.valueChanged)
self.tableView.isHidden = true
self.myScrollView.isHidden = false
}
I put refreshControl.endRefreshing() here. It seems no work.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
self.refreshControl.endRefreshing()
if !oneReload {
getList()
oneReload = true
}
}
I also try to put refreshControl.endRefreshing() before adding the subview. It still no worked.
private func getList(){
if self.viewData.count > 0{
self.tableView.addSubview(self.refreshControl)
self.tableView.isHidden = false
self.myScrollView.isHidden = true
self.tableView.reloadData()
}else{
self.myScrollView.addSubview(self.refreshControl)
self.myScrollView.isHidden = false
self.tableView.isHidden = true
}
}
#objc func refresh(){
refreshControl.endRefreshing()
self.getList()
}
I think this will happen because the view controller been freeze before changing the tabbar selected view. Next time I back to the view.It show with the freeze animation. Can I solve this issue? Please help. Thank you.

Try to put your refresh method on the main thread.
#objc func refresh(){
DispatchQueue.async.main {
refreshControl.endRefreshing()
self.getList()
}
}

Related

How to navigate from one View Controller to the other?

I want to navigate from one View Controller to another.
let vc = SecondViewController()
I have tried until now :
vc.modalPresentationController = .fullScreen
self.present(vc, animated: true) //self refers to the main view controller
Im trying to open a new ViewController when the users manages to register or to log in.I am new to software developing, and I want to ask, is this the best method to navigate from one ViewController to another, im asking because as I can see the mainViewController is not deinit(). I have found other similar questions and tried the answers, the problem is with the:
self.navigationController?.pushViewController
it doesn't work because I don't have any storyboard.
The question is it is right to navigate as explained above?
Thanks,
Typically when you are doing login you would use neither push or present. There are multiple ways of handling this, but the easiest is to embed in some parent (root) VC. Here is an example:
class ViewController: UIViewController {
private var embeddedViewController: UIViewController! {
didSet {
// https://developer.apple.com/documentation/uikit/view_controllers/creating_a_custom_container_view_controller
// Add the view controller to the container.
addChild(embeddedViewController)
view.addSubview(embeddedViewController.view)
// Create and activate the constraints for the child’s view.
embeddedViewController.view.translatesAutoresizingMaskIntoConstraints = false
embeddedViewController.view.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
embeddedViewController.view.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
embeddedViewController.view.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
embeddedViewController.view.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
// Notify the child view controller that the move is complete.
embeddedViewController.didMove(toParent: self)
}
}
override func viewDidLoad() {
super.viewDidLoad()
let loginVC = LoginViewController()
loginVC.delegate = self
embeddedViewController = loginVC
}
}
extension ViewController: LoginDelegate {
func didLogin() {
embeddedViewController = MainViewController()
}
}
protocol LoginDelegate: AnyObject {
func didLogin()
}
class LoginViewController: UIViewController {
private lazy var loginButton: UIButton = {
let button = UIButton()
button.setTitle("Login", for: .normal)
button.addTarget(self, action: #selector(didTapLoginButton), for: .touchUpInside)
return button
}()
weak var delegate: LoginDelegate?
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(loginButton)
view.backgroundColor = .red
loginButton.translatesAutoresizingMaskIntoConstraints = false
loginButton.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
loginButton.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
#objc private func didTapLoginButton() {
delegate?.didLogin()
}
}
class MainViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .blue
}
}

Cannot Form Weak Reference To Instance Of Class - Swift Error

I am getting the error:
objc[12772]: Cannot form weak reference to instance (0x137d65240) of class appName.ViewController. It is possible that this object was over-released, or is in the process of deallocation.
I find this strange because this happened only after I have added a button click to bring the user to this UIViewController through instantiation. After running the app again, the error goes away so this occurs only when the user is segued into this UIViewController from a button.
Does anyone have any idea as to what causes this issue?
#IBOutlet weak var time: UILabel!
#IBOutlet private weak var startPause: UIButton! {
didSet {
startPause.setBackgroundColor(.green, for: .normal)
startPause.setBackgroundColor(.yellow, for: .selected)
startPause.setTitle("PAUSE".uppercased(), for: .selected)
}
}
private lazy var stopwatch = Stopwatch(timeUpdated: { [weak self] timeInterval in // SIGNAL SIGABRT
guard let strongSelf = self else { return }
strongSelf.time.text = strongSelf.timeString(from: timeInterval)
})
deinit {
stopwatch.stop()
}
#IBAction func toggle(_ sendler: UIButton) {
sendler.isSelected = !sendler.isSelected
stopwatch.toggle()
}
#IBAction func reset(_ sendler: UIButton) {
stopwatch.stop()
startPause.isSelected = false
}
private func timeString(from timeInterval: TimeInterval) -> String {
let seconds = Int(timeInterval.truncatingRemainder(dividingBy: 60))
let minutes = Int(timeInterval.truncatingRemainder(dividingBy: 60 * 60) / 60)
let hours = Int(timeInterval / 3600)
return String(format: "%.2d:%.2d:%.2d", hours, minutes, seconds)
}
Code To Present View Controller:
class TutorialViewController: UIViewController {
#IBOutlet weak var doneTut: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func doneTut(_ sender: Any) {
let homeViewController = self.storyboard?.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController
self.view.window?.rootViewController = homeViewController
self.view.window?.makeKeyAndVisible()
}
}
Ok, I think the way you are presenting the view controller is problematic. I would look into using UINavigationController, UITabController or your own container view controller with view controller containment to handle presentations: https://www.hackingwithswift.com/example-code/uikit/how-to-use-view-controller-containment
Basically, since you are resetting the window's root view controller, your presenting view controller, TutorialViewController, might be getting deallocated as a result (if nothing else is retaining it).

How to show containerview in viewcontroller in swift

I need to show containerview in viewcontroller, so i have taken one viewcontroller and designed and in that i have taken one containerview below to the buttons view
given containerview constraints: below to the buttonview
top = buttonview.bottom 10, leading, trailing, bottom = 0
there are two buttons indicates with green line.. initially i am in the "viewproposalvc" with red colour button.. here if i click gray colour button then i need to show containerview
here the viewproposalvc code:
class ViewProposalVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var postId: Int?
#IBOutlet weak var containerView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
updateNavigationBar(with: "View Proposal")
containerView.isHidden = true
}
#IBAction func grayBtn(_ sender: Any) {
containerView.isHidden = false
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "ContainerVC") as? ContainerVC
vc.serviceId = self.viewproposalData?.result?.posted_services?.id
self.navigationController?.pushViewController(vc!, animated: true)
}
#IBAction func redBtn(_ sender: Any) {
containerView.isHidden = true
postedServicesCall()
}
}
this is containervc code:
class ContainerVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
override func viewDidLoad() {
super.viewDidLoad()
}
}
but here containervc not coming below to the buttonsview.. its coming in separate viewcontroller, why?
o/p of containervc: i dont want separate viewcontroller.. i need containervc to show below to the buttonsview of the viewproposalvc
first time i am working with container views, please do help
The reason why is because you are calling
self.navigationController?.pushViewController(vc!, animated: true)
ContainerView is just a generic UIView with an embedded segue, that is triggered from viewDidLoad of the hosted VC. If you need to do additional setup of that VC I suggest making an var on ViewProposalVC and making desired changes from there.
as in
class ViewProposalVC: UIViewController, UITableViewDelegate, UITableViewDataSource {
var postId: Int?
#IBOutlet weak var containerView: UIView!
#IBOutlet weak var secondaryController: UIViewController!
override func viewDidLoad() {
super.viewDidLoad()
updateNavigationBar(with: "View Proposal")
containerView.isHidden = true
}
#IBAction func grayBtn(_ sender: Any) {
containerView.isHidden = false
self.secondaryController.serviceId = self.viewproposalData?.result?.posted_services?.id
}

onClick Action with Label does not work [duplicate]

I would like to make a UILabel clickable.
I have tried this, but it doesn't work:
class DetailViewController: UIViewController {
#IBOutlet weak var tripDetails: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
...
let tap = UITapGestureRecognizer(target: self, action: Selector("tapFunction:"))
tripDetails.addGestureRecognizer(tap)
}
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
}
Have you tried to set isUserInteractionEnabled to true on the tripDetails label? This should work.
Swift 3 Update
Replace
Selector("tapFunction:")
with
#selector(DetailViewController.tapFunction)
Example:
class DetailViewController: UIViewController {
#IBOutlet weak var tripDetails: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
...
let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
tripDetails.isUserInteractionEnabled = true
tripDetails.addGestureRecognizer(tap)
}
#objc
func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
}
SWIFT 4 Update
#IBOutlet weak var tripDetails: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let tap = UITapGestureRecognizer(target: self, action: #selector(GameViewController.tapFunction))
tripDetails.isUserInteractionEnabled = true
tripDetails.addGestureRecognizer(tap)
}
#objc func tapFunction(sender:UITapGestureRecognizer) {
print("tap working")
}
Swift 5
Similar to #liorco, but need to replace #objc with #IBAction.
class DetailViewController: UIViewController {
#IBOutlet weak var tripDetails: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
...
let tap = UITapGestureRecognizer(target: self, action: #selector(DetailViewController.tapFunction))
tripDetails.isUserInteractionEnabled = true
tripDetails.addGestureRecognizer(tap)
}
#IBAction func tapFunction(sender: UITapGestureRecognizer) {
print("tap working")
}
}
This is working on Xcode 10.2.
Swift 3 Update
yourLabel.isUserInteractionEnabled = true
Good and convenient solution:
In your ViewController:
#IBOutlet weak var label: LabelButton!
override func viewDidLoad() {
super.viewDidLoad()
self.label.onClick = {
// TODO
}
}
You can place this in your ViewController or in another .swift file(e.g. CustomView.swift):
#IBDesignable class LabelButton: UILabel {
var onClick: () -> Void = {}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
onClick()
}
}
In Storyboard select Label and on right pane in "Identity Inspector" in field class select LabelButton.
Don't forget to enable in Label Attribute Inspector "User Interaction Enabled"
You need to enable the user interaction of that label.....
For e.g
yourLabel.userInteractionEnabled = true
For swift 3.0 You can also change gesture long press time duration
label.isUserInteractionEnabled = true
let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(userDragged(gesture:)))
longPress.minimumPressDuration = 0.2
label.addGestureRecognizer(longPress)
Pretty easy to overlook like I did, but don't forget to use UITapGestureRecognizer rather than UIGestureRecognizer.
Thanks researcher
Here's my solution for programmatic user interface using UIKit.
I've tried it only on Swift 5. And It worked.
Fun fact is you don't have to set isUserInteractionEnabled = true explicitly.
import UIKit
open class LabelButon: UILabel {
var onClick: () -> Void = {}
public override init(frame: CGRect) {
super.init(frame: frame)
isUserInteractionEnabled = true
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
}
public convenience init() {
self.init(frame: .zero)
}
open override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
onClick()
}
}
Uses:
override func viewDidLoad() {
super.viewDidLoad()
let label = LabelButton()
label.text = "Label"
label.onClick = {
// TODO
}
}
Don't forget to set constraints. Otherwise it won't appear on view.
On top of all of the other answers, this depends on where the label is, it might be behind some subviews. You might think you tap on the label but maybe click the top view. To solve this you can bring the label view to the front with the following line.
self.view.bringSubviewToFront(lblView)
As described in the above solution
you should enable the user interaction first and add the tap gesture
this code has been tested using
Swift4 - Xcode 9.2
yourlabel.isUserInteractionEnabled = true
yourlabel.addGestureRecognizer(UITapGestureRecognizer(){
//TODO
})

ViewController looses animated position after navigation

I want a view to appearing and disappear from the left (depending if it has some useful information for the user).
I want to view to be positioned using constraints, so I need it to be created from the storyboard.
In this code snippet, the view should be moved out of the way when to code appears.
But: When I segue to the next VC I can see that the view appears again at its original position and when going back from the VC to the initial VC it has, in fact, resumed the original position.
I played a little around with saving the "state" of the view in a variable, making it appear/disappear in the various lifecycles of the VC, but nothing really helped.
How is the best way to achieve this?
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
var boxIsVisible = false
override func viewDidLoad() {
super.viewDidLoad()
}
var originalX:CGFloat = 0.0
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
originalX = self.myView.frame.origin.x
if boxIsVisible == false {
self.myView.center.x -= 200
}
}
#IBAction func slideInAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.myView.center.x = self.originalX
self.boxIsVisible = true
})
}
#IBAction func slideOutAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.myView.center.x = -200
self.boxIsVisible = false
})
}
}
Update leading constraint instead of the view's position, because you use auto layout.
class ViewController: UIViewController {
#IBOutlet weak var myView: UIView!
var boxIsVisible = false
#IBOutlet weak var leadingConstraint: NSLayoutConstraint!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
if boxIsVisible == false {
leadingConstraint.constant = -myView.frame.width
}
}
#IBAction func slideInAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.leadingConstraint.constant = -50
self.view.layoutIfNeeded()
self.boxIsVisible = true
})
}
#IBAction func slideOutAction(sender: AnyObject) {
UIView.animateWithDuration(3.0, animations: {
self.leadingConstraint.constant = -self.myView.frame.width
self.view.layoutIfNeeded()
self.boxIsVisible = false
})
}
}