Label is not updating iphone - iphone

I have singleton class in which I declare an int and a NSTimer so both are globally accessible. I decrease int value using timer and display on label which declare at A class.
What happened is that it decreases and keeps updating int value on label but when user moves back to A class and then jump into it doesn't update value.
I have logged the int and the text value of label and both are working fine but does no update onto view.
Please help me and indicate me where I am doing wrong. If anyone does not understand I'll update question with code.

Related

UITextField always results in nil

I'm trying to get the information of a UITextField and then reuse that data from the UITextField with swift 2.0 and Xcode 7. But every time I run the app, the app crashes and says:
fatal error: unexpectedly found nil while unwrapping an Optional value
even though there is something in the textfield.
Someone knows how to solve this problem?
This is how my code looks:
#IBOutlet weak var entryAmount: UITextField! //TextField declaration
#IBAction func saveNewButtonTapped(sender: UIBarButtonItem) {
//update overall amount
let amount: String? = self.entryAmount.text! //Throws the error at this point
self.viewController.currentValue += amount
}
In another function, I wrote the same thing ("let amount: String? = entryAmount.text!") and when I printed "amount", it showed the right result. But as soon as I tried to use it further, "amount" was always set to nil, even tough it always printed the right result in the beginning.
I encounter the same thing with DatePicker, Switches and updating Button texts.
Someone knows something to help me? - Thanks
Btw I'm pretty new to swift and Xcode.
Update: Could it be because the TextField is in a ContainerView?
Update Two: I'm pretty much sure the problem is because of the ContainerView. I tried a TextField that isn't in a containerView and didn't get the error, it worked perfectly fine.
Please check in your Interface Builder that the UITextField is properly linked to your "entryAmount" IBOutlet. It might be the case that they are not properly connected and that's why the value is nil at runtime.
I could be wrong but what it seems like from your question is you're having trouble with scoping. Any variables you declare in a function are scoped only to that function.
Declaring amount in saveNewButtonTapped means amount is only set in that function. Using amount in another function will result in amount being nil. If you want to use variables between functions you need to declare them outside of a function.
In the example I have the var runningAmount. In the example, if I press the button that calls pressAnotherButton, I get the same fatal error you get because I'm unwrapping self.runningAmount before it's been set. But if I press the button that calls saveNewButtonTapped, even with nothing in the textfield, then press the button that calls pressAnotherButton, I get the output in the photo.

Start editing a TreeTableCell from a code request

I have a TreeTableView that is working great. I have implemented a context menu that appears when the "TreeTableCell" is clicked. In my context menu, I have a MenuItem called "edit" that, when selected, I want to cause the current TreeTableCell to start editing. Unfortunately, I am unable to figure out how to correctly start editing the cell.
My initial thinking was that since the context menu callback is in the scope of my TreeTableCell instance, I would call the "startEdit" method of my cell. This appears to work ... however, when the commitEdit occurs, I get a NullPointerException deep within the internals of JavaFX. This makes me think that explicitly calling startEdit() is the wrong thinking and that there should be a correct way to cause a specific cell in the table to begin its edit cycle.
I was able to find the answer after lower level study. I had assumed that I could call startEdit() against my TreeTableCell however that is not the correct way to flag a table cell as entering is editing mode.
Instead both TableView and TreeTableView expose an edit() method that take two parameters:
The row number of the table indexed from 0
The TableColumn of the table
The combination of row and column provide a unique location of a cell in the table. Calling edit() places the cell in editing mode.
Here is the JavaDoc for TableView edit method:
JavaDoc for TableView edit

Boolean variable that evaluates whether or not the current view has disappeared?

I am using an NSTimer that runs down from 15 seconds in a multiple choice quiz app. The app has two possible outcomes. The time may run down to zero, and the incorrectAnswer view is shown, in which case the Timer is invalidated/stopped and the static integer for its time is reset back to 15 for the next question.
However, the other possible outcome is that an answer is selected before time runs down, in which case I use the method viewWillDisappear to trigger further action. My problem is that I cannot reset the static integer for time from this method, because it is declared in the method above. I tried declaring it in the .h file, but there are problems because the integer is static.
I want to know if there is a boolean expression that evaluates if the current view has disappeared, because this way I can keep everything in the same method and be able to reset the static integer for time.
First, if your timer variable local to the class, it doesn't have to be declared as static. Secondly, you have two opportunities to address your problem from the viewController: viewWillDisappear and viewDidDisappear. Lastly, if you need to know if a view property has changed, you can do it via a KVO method. The problem you'll run into is if the view has truly disappeared, the view object may be toast by the time you reference it. Without more info, there is no way to tell what problem you're truly having.
Also, once you invalidate a timer, you can no longer use it. You must create a new timer.

Issue with UIPickerView's NumberOfRowsInComponent properly registering the int 16 value I am returning from core data

I have a Book data entity that has as an attribute an int 16 type value called numBooks. Due to the way everything is setup, I am actually grabbing the number I need inside titleForRow, and saving the value to a global variable of type NSNumber.
In titleForRow, I am setting the global in this way:
global = book.numBooks;
If I NSLog my global variable after it is set thus, it returns to the consol the proper number value (50 depending). But, if I were to just print book.numBooks to the console, the value is quite different, 100499556.
Now, the problem is that when I take my global variable back to numberOfRowsInComponent, to use as the count of rows for a given component, there is seemingly no end to the number. I can scroll and scroll and it obviously isnt working right.
Does the uiPickerView numberOfRowsInComponent only want an int? Is it basically thinking that the value I am passing it is that 100499556?
Please help, thanks!
Try
[book.numBooks intValue];
You are probably seeing a pointer value for the NSNumber* book.numBooks.

Why can't I store a float value - it's always zero!

I have a view controller that is created by the app delegate - it's the first one shown in the app.
In its interface I declare
float lengthOfTime;
I also set it as a property:
#property (nonatomic) float lengthOfTime;
And in it's implemetation:
#synthesize lengthOfTime;
In the class viewDidLoad method, I set the value:
self.lengthOfTime = 3.0f;
However, after this, the value is always zero.
No errors, no compile warnings, nothing. Just zero.
The class is instantiated, it is showing in the view, so I'm pretty sure it's not a nil reference.
I've searched all over Google and can't figure it out.
What's going on?!?
:(
The property should be atomic I think. Just declare it as:
#property float lengthOfTime;
Updating primitives is an atomic operation.
I'm not sure if that will solve your problem or not.
The "non-atomic" keyword is for protection from multi-threading issues where one thread gets interrupted in the middle of changing it. You can only interrupt an operation if it takes more than one instruction to perform. Updating a single floating-point value is a one-instruction operation, and therefore cannot be interrupted.
It is possible that your view is reading the value before -viewDidLoad is called. I would try setting the variable in a method that is called before the view is initialized such as -awakeFromNib.
I fixed it and don't know how I did it. That's the worst. Now I don't know how to solve it if it happens again.
I thought it wasn't being initialized, but I removed the line and it still works.
I think I'm even more frustrated than before.