IOS - Write to UITextField from another class in the IOS Application - ios

I have method in ViewController:
(void) writeInViewController:(NSString *)var {
NSLog(#"Var: %#", var);
}
I send value to this method(writeInViewController) from another class it's receive the value and print it in the Debug Area but I can't write to the User Interface (textfield.text = var)
I need to add text to the UITextField from this method
Thanks

If i am not wrong your value getting null when you are assign to UITextField, do one thing create one instance variable or property then assign that value to variable then assign to UITextField it will work.

Related

How to safely assign yourself to your own delegate without ending in retain cycle?

I have a class SomeTextFieldClass, that is a subclass of UITextField class. I use it for view customization on UITextField view and I would also like to use it to handle restrictions on what the user is typing , e.g. user is allowed to type in only numbers with 2 decimal places etc. For that purpose I use the delegate of UITextFieldDelegate textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool . When I assingn self.delegate = self during initialization of my SomeTextFieldClass , I can make the functionality work. However I have a strong feeling that it creates an retain cycle, and I would like to avoid that. Is there some other way how to do it? What is some best practice to use delegate methods on the class itself? Basically I just want to access the UITextField delegate methods (e.g. shouldChangeCharactersIn) on UITextField child class itself. Thanks
Delegates are 'weak' in nature. Hence the retain count does not increase by 1 when assigning the delegate. Therefore, there is no retain cycle created.
Check this out: https://medium.com/macoclock/delegate-retain-cycle-in-swift-4d9c813d0544
I believe you should setup the delegate as "textfield.delegate = self" in the view controller. Then, you should handle it like;
extension UIViewController: SomeTextFieldClassDelegate {
// protocol functions are here
}
in SomeTextFieldClass, delegate should be defined as "weak";
weak var delegate: SomeTextFieldClassDelegate?
If you provide more code, you can get better help. But it looks like there is something wrong in this code; "self.delegate = self" . I guess that you assign textfield to its own delegate.

Issue with showing the name in a row on a textfield in the next viewcontroller

My issue is this...when I click on a row of my tableview, I have to go to another screen. In that screen, there is a textfield where the value in the row(in previous screen) has to be assigned to a textfield..for that I am doing this..
editTextField.text = modelArray[index!]
But it shows this error…’Cannot assign value of type 'NSManagedObject' to type 'String?'’
Also modelArray is of type NSManagedObject.
What should be done in this case...?
It's a classic type mismatch. You have NSManagedObject but the label expects String
Two options:
If you are using NSManagedObject subclass and modelArray is declared as an array of that type
let item = modelArray[index!]
editTextField.text = item.categoryName
If you don't use NSManagedObject subclass
let item = modelArray[index!]
editTextField.text = item.value(forKey:"categoryName") as? String
In any case why is index an optional?. An optional index type is nonsense because it doesn't prevent an out-of-range exception.
’Cannot assign value of type 'NSManagedObject' to type 'String?'’
This show's you are trying to use coredata value to show in the textfiled. You can do these things
Pass the value from tableview didselect method to controller and show that in the textfield (the value in the cell definitely be a string)
Create Textfield globally in your nextviewcontroller and change its value in tableview did select delegate method

Getting a property from parent class

I've added a view controller as child like this:
UIViewController *sendViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"send"];
[self addChildViewController:sendViewController];
In the sendViewController I got this property:
#property (strong, nonatomic) IBOutlet StringInputTableViewCell *nameCell;
and in that class I can do something like self.nameCell.textField.text = #"test"; to set the textField to test
But how can I get this text thats filled in the textField in the parent class?
You are mixing model and view, thus breaking the MVC design pattern.
You should not try to read what is the content of a UI element. Rather you should have all the data (i.e. model) and the view (i.e. the UI, such as text fields) managed by a controller.
There are (easy) ways to get to this information, but I strongly advise you not to go down that road!
Basic inheritance between the parent and child class should allow you to pass the property forward.
You'll need to create a child object of the class say obj. Then to get the text value of the field you'll use (in the parent class)
id obj = [[ChildClassName alloc] init];
NSString *myChildsText = obj.nameCell.textField.text; // will get the value #"test" as assigned in the childclass.
Or of course, you can create a getter and setter in the Child Class for your #property. For example ::
- (IBOutlet)nameCell {
// returns the value
}
- (IBOutlet)setNameCell :(StringInputTableViewCell)newValue {
//set the value to the #synth value here…
}
then you can call the child objects getters/setters as below ::
NSString *text = [obj nameCell]; //etc etc
You can use 4 approaches in here
a) keep reference to you childviewcontroller -> get the value directly
b) use delegate method ( best for observing changes in your textfield )
c) send NSNotification -> this comes with overhead, well at certain points it can help you a lot
d) KVO

Pass Value as Parameter from View Controller to Method of Entity Class

I have a ViewController with a UITextField. The user enters a string of characters into the textField, clicks a done button and then this value should be passed to a method in the entity class. I know I need to pass this value as a parameter but I'm getting stuck.
I've tried to pass this value by adding a parameter to the end of the clickDone IBAction for the done button with no luck. Thank you for even the smallest bit of help or point into the right direction.
- (IBAction)doneClicked:(NSString*) name {
...
}
I've tried creating an ivar for ViewController in my entity class h file but Xcode asks "Unknown type name 'ViewController'; did you mean UIViewController?
#interface EntityClass : NSManagedObject{
ViewController* refVC;
UITextField* textBox;
}
and then use
self.name = self.refVC.textBox.text;
in my method in entity class m file to return the NSString self.name
Don't pass the ViewController to the Entity Object. Instead, update the entity object's property in your doneClicked action.
- (IBAction)doneClicked:(NSString *)name {
myObject.name = name;
}
I figured it out. This is my final code. It works. Help is no longer needed.
Thanks!
- (IBAction)doneClicked:(NSString*) name {
...
superClass.name = headingText.text; //headingText is UITextField on ViewController
[superClass doSomething];
...
}

Problem setting property value on Custom Control Template Part

I have a custom control template that contains a Slider control.
I name that as a part in the class that implements the custom control:
[TemplatePart(Name = MapZoomSliderName, Type = typeof(Slider))]
In the OnApplyTemplate() override, I get the Slider:
MapZoomSlider = (Slider) GetTemplateChild("MapZoomSlider");
if (null != MapZoomSlider)
{
MapZoomSlider.ValueChanged +=new RoutedPropertyChangedEventHandler<double>(MapZoomSlider_ValueChanged);
MapZoomSlider.Value = InitSliderValue; // crash
_lastSliderValue = MapZoomSlider.Value;
}
When I try to set the Slider's Value property, the app crashes with "Object reference not set to an instance of an object."
Getting the slider's value works as expected.
What do I need to do to set the Slider's value at run time?
Thanks for any tips...
What is "InitSliderValue"? Maybe its the wrong value type? (Must be a double) Also, zero or negative may not be a valid value.
It appears the problem was in setting the ValueChanged handler before changing the Value property. The ValueChanged handler tries to manipulate other parts of app, parts that might not be ready yet.
If I set the value, then add the handler, it works as desired.
MapZoomSlider.Value = InitSliderValue; // all good
MapZoomSlider.ValueChanged +=new RoutedPropertyChangedEventHandler<double>(MapZoomSlider_ValueChanged);

Resources