UITextView in UiCollectionView - ios

I really need your help here. I have a UITextView in a UiCollectionViewCell. The problem i am facing here is when i select the cell to edit it , neither the textview methods nor the collectionview methods are called. For ex. neither the didSelectItemAtIndexPath nor the textViewDidBeginEditing methods are invoked.
As a result, I am not able to capture the edits made by the user the cell and save it .
Any inputs would be highly appreciated.
Thanks

I had faced similar problem in custom cell what i did was I had added following code to capture the events
[textField addObserver:self forKeyPath:#"contentSize" options:(NSKeyValueObservingOptionNew) context:NULL];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
UITextView *tv = object;
//Your code using tv
}

Related

iOS 9: KVO no longer working

I have an observer on a UITextView to detect if its content size is changing:
[_textView addObserver:self forKeyPath:#"contentSize" options:NSKeyValueObservingOptionNew context:NULL];
This code always worked to call the following function, where I do resizing of the UITextView:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
However, in iOS 9, this function is never getting called. What changed in iOS 9 and how do I fix this?
If you do a search for UIKit and KVO Compliance you will see that everyone says you can't rely on it. See this question and this question.
I don't know what changed, but I think you should just subclass UITextView and overload setContentSize: if you want to know when it changes.

KVO notifications with textView.text

I need notifications with textView.text, but the do not work.
My code:
Add a KVO:
[textView addObserver:self
forKeyPath:#"text"
options:NSKeyValueObservingOptionNew
context:nil];
Observing method
-(void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
I am trying change contentSize. KVO is working...
https://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/KVO.html
Note: Although the classes of the UIKit framework generally do not support KVO, you can still implement it in the custom objects of your application, including custom views.
There have another way to help you simply.
Conform UITextViewDelegate protocol methods.
Use method - (void)textViewDidChange:(UITextView *)textView; to get textView.text changed.

Listen UITextField setText

I have a UITextField which is updated from a different module (I'm passing my UITextField to the above mentioned module to fill it). I need a way to identify when text value of the UITextField is changed inside the module which the UITextField originally is. UITextField Delegate methods or UIControlEvents will not work as the text is fill programmatically.
Put the following somewhere, like -viewDidLoad:
[textField addObserver:self forKeyPath:#"text" options:NSKeyValueObservingOptionNew context:nil];
And then add a -observeValueForKeyPath like:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (object == textField)
{
// Do whatever with your text field here
}
}
This will only be called when the text is set programmatically though, not when the user is typing in the field.
Or just subclass UITextField and pass your subclass in. You can override setText: as needed
I don't quite understand what you mean by a "different module", but you can try subscribing to UITextFieldTextDidChangeNotification.
Another way to get notified is by using Key-Value Observing. I don't know if it works for the text property, though.

Call animation method when UItextField.enabled changes

I've got a set of UITextFields which are enabled or disabled based on an if function (basically only allows 2 to be filled in, then the others are disabled).
I've got 2 methods to fade the background of the UItextFieldFade in and Fade out.
I want the animation to run every time the enabled property of the UITextField changes but don't really know how to track the actual change.
Any help would be great.
Thanks
Add an observer to the text field:
[_textField addObserver: self forKeyPath: #"enabled" options: NSKeyValueObservingOptionNew context:NULL];
You should implement this method:
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *) context;
Where you handle the value change of the "enabled" property.
Documentation: Key value observing.

Observing custom cell

I am trying to add observer (KVO) to observe my custom cell. Once the cell is selected I should receive a notification of the event.
My code:
[colMain addObserver:self forKeyPath:#"colMain" options:0 context:NULL];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (keyPath == #"colMain") {
NSLog(#"cell Selected");
[self performSelector:#selector(deleteCell) withObject:nil];
}
}
colMain stands for collectionView. I am not quite sure how to do it cause I don't have customCell as a property otherwise it does not compile.
Any ideas?
Why not just set a delegate on your collection view and then implement one of these two methods?
[– collectionView:shouldSelectItemAtIndexPath:]
[– collectionView:didSelectItemAtIndexPath:]

Resources