Tableview not reloading after button click - ios

I am working in swift. I have a protocol defined that has a function called reload. I have a home class which is a tableviewcontroller where I extend the protocol and implement the reload function. I have another class where there is a button. On this click of the button I set the delegate and the reload function is called. Until this step it works fine. Now in the reload function i want to refresh the home page, so i tried
tableview.reload()
this is not working and the app is crashing, then i tried calling
viewDidLoad()
this is also not refreshing the page. I dont want to use NSNotification
what am i doing wrong?
Can anybody please help
Thanks in advance

Are you sure your UITableView data source delegate methods are working properly? You should look up viewDidLoad() and viewWillLoad(), etc... in the UIViewController reference page (Apple doc). If you're serious about app development I recommend at the very least making the time to spend 15 - 30 minutes each looking at the UIViewController and UIView pages to familiarize yourself with them, since you'll be using them all the time. You should know basically how they work. Also check out CALayers in UIViews. Then you'll be better established to write really nice apps.

you are setting a delegate on button touchup inside event, please check whether you are setting a delegate to your Home class .

I assume your mean tableview.reloadData() which is the correct method to call for reloading your tableview.
You shall not call viewDidLoad by yourself.

Related

iOS How do I tell when a user is interacting with the app?

Goal:
I need to be able to tell when a user is interacting with an iPhone app.
After 5 minutes, I will auto logout the user, but I don't want to do that if the user is interacting with the app.
I have read about shouldReceiveTouch:, but that it will be called if a user touches something. I could put login in there to reset the 5 minute timer.
How do I make it so that if any view on any screen is touched, the 5 minute timer starts over?
Is it possible to do this without implementing shouldReceiveTouch: everywhere?
Any help is appreciated.
There are at least 3 approaches:
(1) The best one I think is suggested in this post. Basically you would override -sendEvent: on UIApplication. But someone mentions this might not work anymore since iOS 7 - YMMV.
(2) Another, less clean solution would be to use method swizzling. You could replace a method on UIView for your own implementation, e.g. -touchesEnded:withEvent: or -hitTest:withEvent:.
(3) If your app doesn't use any 3rd party view controller, you could create a custom base class of UIViewController and make all your view controllers (except login view controller and anything shown before) inherit from this base class. This view controller would have a gesture recognizer or perhaps override a method like -touchesEnded:withEvent: or -hitTest:withEvent:.
For all approaches you would probably use a singleton to keep track of the last touched moment. Perhaps the singleton would use a countdown timer. The singleton could have a multicast delegate or use NSNotificationCenter to inform other view controllers that the timer has run out.
P.S.: I should note that you should consider method swizzling carefully. If some other project or library swizzles the same method, you will never know for sure which implementation is used for the swizzled method.

Call method in UIViewController from didSelectRowAtIndexPath in NSObject

I have a UIViewController(A) that uses an NSObject with delegate methods to display a UITableView programmatically. This works great.
But then when I select a row I want to load a different UIViewController(B). I have tried to use a NSNotification on UIViewController(A) that is called in the didSelectRowAtIndexPath. However this takes about 20 seconds to load the UIViewController(B).
NOTE: I know there is nothing wrong with UIViewController(B) in terms of loading.
I have checked that the displaying of UIViewController(B) is on the main thread. Which it is.
I am therefore thinking that there may be an alternative method to display UIViewController(B). The alternative method I am thinking is to write a method in UIViewController(A) to present UIViewController(B). But how can I call this from the NSObject didSelectRowAtIndexPath method.
Managed to get it to work. So for anyone who is having the same troubles. My answer came from https://github.com/burczyk/KBContactsSelection

Swift IOS app won't start after idle for over 1 day

I have a simple app in Swift with just a few views:
A UIWebView
some TableViews
and another view with some data I download from my server
It all works well until when using the app I press the home button, leave there for a while then the iPad goes on sleep mode. A few days later I tap on the app icon and it won't start:
first tap on the icon will select the icon (goes a little darker) and deselect it a few seconds later
second tap will launch the LaunchScreen and crash a few seconds later
double tap the home button and quit the app will sometimes work
I'm just wondering if there is something I need to set on my code to handle idle/long periods of inactivity in something like viewWillDisappear or other methods?
If so I already have this in all my controllers:
override func viewWillDisappear(animated: Bool) {
timer.invalidate()
webView.removeFromSuperview()
}
Maybe I need to call super. in there too? or something else I'm missing?
You should definitely call super in your viewWillDisappear(animated:) method. See UIViewController Class Reference documentation. Also you might want to confirm why you are removing your webView from the view controller's hierarchy.
Discussion
This method is called in response to a view being removed
from a view hierarchy. This method is called before the view is
actually removed and before any animations are configured.
Subclasses can override this method and use it to commit editing
changes, resign the first responder status of the view, or perform
other relevant tasks. For example, you might use this method to revert
changes to the orientation or style of the status bar that were made
in the viewDidDisappear: method when the view was first presented. If
you override this method, you must call super at some point in your
implementation.
You probably have some null pointer exception and crash. Maybe you are calling some variable that is not set (and checked if not null).
Try disabling app funcionality (like downloading, storing and using data from server) and see where you app starts working normal again and then procede from there.
Sorry for vague answer but withouth code and maybe some log it is really hard to give specific answer.
And NO, you dont have to do anything special to handle idle/long periods of inactivity.

when to use viewDidLoad

I am new to both iOS development and programming in general. I need some clarification as to what sort of things should be declared in the viewDidLoad function of a UIViewController subclass
Thanks
In order to properly understand what viewDidLoad does, you should understand the View Controller Lifecycle. The best point to start is reading the Apple Documentation, e.g. the learning guides for developing iOS Apps: https://developer.apple.com/library/prerelease/ios/referencelibrary/GettingStarted/DevelopiOSAppsSwift/Lesson4.html
Declare elements that don't need to be refreshed or recreated when the view reloads. For instance, viewDidLoad is called only when it is created while viewDidAppear will be called every time the view is shown.
Read up on some apple docs.
Everything you write inside the viewDidLoad function will run the the View(which can be TableView, ViewController & more..) is loaded.
For example, if you got a label called 'label' and you want to set it's by the code so you type:
override func viewDidLoad() {
super.viewDidLoad()
label.text = String("any text here")
}
and then the text of the label will change when the View will load.

iOS Delegates instead of passing data through a segue

I'm trying to learn how delegates work and wrap my head around the concept. I'm finding I get some of the ideas. I understand you use it to pass data from one view controller to another, however wouldn't it work the same if I just sent data from a segue and every time the 1st view controller would appear, it would use that data?
So for example I have 2 view controllers.
1 is homeViewController and
2 is editViewController.
I have a variable titled "addressOfHome" which is a string in homeViewController(1).
In homeViewController under the method "viewDidAppear"
I also set the addressLabel = addressOfHome.
Then I just pass the data from editViewController(2) to homeViewController(1)
when the segue's destination vc is homeViewController ?
I'm terrible at explaining things so I apologize for that, but I gave it my best shot. Thanks for your time!
Delegates are mainly used to "trigger action" on an object from another one.
An object delegates a way to handle something to someone else, for example when you click on an UIAlertView button, if its delegate is set on a viewController, alertView:clickedButtonAtIndex will be executed on the VC, which can so react as it want
I'm terrible at explaining things
Haha, yes, you are !
A delegate isn't for that - a delegate is a way to over-ride the default behaviour of some feature(s) of a class, without creating your own sub-class. See this post : How does a delegate work in objective-C?
Are you trying to understand how delegates work (in which case, I don't think your example is one that requires a delegate) or are you trying to implement the functionality you describe, and think that a delegate is the way to do it? (I think you actually want a data source).

Resources