I'm stuck in this situation where I cannot link any UITextView delegate methods to my code, because they just don't show up in IB. That's all I've got :
I guess every delegate method should appear here (editingDidEnd, etc.) ?
UITextField delegates work fine though.
I also tried to create a #IBAction func textViewDidEndEditing(textView: UITextView) {} function manually, but I can't drag the UITextView to that function either.
Does someone know what is going on ?
Related
I am trying to emulate Apple's weather app animation where when you scroll, the top header collapses and remains as a condensed sticky cell. I am attempting to do this with all of the UI being done programmatically and without storyboards.
The two ways I've thought of implementing this each have an issue I have not been able to solve.
My initial attempt was having a containerVC contain a UIView (as a header) and a UICollectionViewController. The issue is that The containerVC cannot access the UICollectionViews scrollViewDidScroll() which I would use to calculate and adjsut the size of the UIView. I could make the containerVC the collectionViews delegate but I wanted to avoid that to keep my logic separated. I also tried using Key Value observers but I could not figure out how to make it work.
My second attempt was to use a UICollectionReusableView as a header cell, that way there is no container view, just a single collectionViewController. The issue here is I can't figure out how to dynamically resize the headercell. The header size is currently being returned from referenceSizeForHeaderInSection and I have been unable to find another way of updating this.
Is there a better way to be going about this? Or an easier solution to the issues described that I haven't tried yet?
You can implement your own delegates:
protocol ParentDelegate: class {
func childDidScroll()
}
extension ParentViewController: ParentDelegate {
func childDidScroll() {
print("My child controller did scroll")
}
}
Create a delegate variable in the class that you want to call it:
weak var delegate: ParentDelegate?
Set it to your parent ViewController:
delegate = parentVC
then inside your child scroll method you call it:
delegate?.childDidScroll()
I'm getting really confused about calling methods on a dynamically added subview. I know I'm making a newbie mistake but I just can't figure it out.
My hierarchy looks something like this:
TableView
- TableViewCell
- CustomContainer (UIVIew subclass)
- Widget (UIView subclass)
- myButton
The Widget is added to the CustomContainer via view.addSubview(widget)
I have an #IBAction on myButton that I want to call myFunc declared in the Widget class (below the #IBAction):
#IBAction func myButtonTapped(sender: AnyObject) {
myFunc()
}
...
func myFunc(){
print("myButton was just tapped")
}
When I tap the button I get an error in the console that I can't resolve: [myApp.CustomContainer myButtonTapped:]: unrecognized selector sent to instance
My objective is to have all the methods that relate to the Widget contained in the Widget class or somewhere similar that makes this logical and easy to move around.
I've tried lots of different things but to be honest I'm now just casting around in the dark. If anyone can suggest an approach I'd be very grateful.
Okay, after some quick investigation and a little demo: Add you #IBAction function to your custom UITableViewCell subclass.
When adding the the Widget with the UIButton to the CustomContainer, make sure it's selector is YourCustomUITableViewCellClass.myButtonTapped. Another step you might also want to consider is to refactor your code and maybe not use such a deep hierarchy.
I am new to iOS development and am reading the swift tutorial from apple.
It seems to me that the code below is always needed so that when a user hits return in a text field, it resigns the first responder.
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
I understand that swift provides the flexibility for programmers to control this behavior, but in what scenarios of a real app do I not need these two lines? It seems to me that I always need them so why doesn't swift automatically do it?
It would be very wrong for Cocoa to assume that tapping Return should automatically dismiss the keyboard. It is rightly left up to you. The user's expectation is probably that this will dismiss the keyboard. And you, too, probably would rather not have the keyboard hanging around forever. Well, resignFirstResponder dismisses the keyboard. Still, suppose there is something wrong with the text. When the user taps Return, you might check the text and not dismiss the keyboard. You might put up an alert instead, warning that there's a problem. You wouldn't want to be overridden by some automatic behavior of the framework!
By the way, there are other ways to cause tapping Return to dismiss the keyboard other than implementing this method. In apps that use those other ways, this method might not appear at all.
That's a default.
However, if you prefer the "automatic enable return", you can now in Swift 2.0.
You still need to make your class conform to UITextFieldDelegate, and you still need to configure the delegate in Interface Builder.
Make a protocol extension:
extension UITextFieldDelegate {
func textFieldShouldReturn(textField: UITextField) -> Bool {
NSLog("default behavior")
textField.resignFirstResponder()
return true
}
}
No, that's the default behavior for all your App...
You can still override the method in class.
I have created a custom subclass of UITextField, CustomTextView.
I created a
private var tapGesture = UITapGestureRecognizer()
in the customTextView class
In the initInView, I have the following code
tapGesture.addTarget(self, action: "tapTextField:")
self.addGestureRecognizer(tapGestureRecognizer)
CustomTextView implements UIGestureRecognizerDelgate
CustomTextView has a private func named tapTextField
Now when I use iOS simulator and click on the text field, the function tapTextField never gets called.
What am I missing here? I saw similar posts but none of them answer my question. I could not comment on those as I don't have reputation yet. So asking as a new question.
PS: Does this have to do with firstResponder being set? This is someone else's code I am working on, so I might have missed something. Let me know what I should look for
Related stack overflow questions:
Add UITapGestureRecognizer to UITextView without blocking textView touches
UITextview: Place focus on becomefirstresponder
I don't know swift, but try changing this "tapTextField:" to this "tapTextField" and make sure you don't have any arguments/parameters/whatever swift calls them in your "tapTextField" function.
Also, it looks like
self.addGestureRecognizer(tapGestureRecognizer) should be self.addGestureRecognizer(tapGesture)
You can try few things to debug the problem.
1> Make sure there is no mistake of tapTextField and tapTextField: i.e. you are adding selector with argument but you have implemented same method without any argument.
2> Make sure no any other transparent view obscuring your custom uitextfield.
3> print po yourTextFieldName in the xcode console to see whether actually your textfield has any gesture recognizer added in it or not.
Hope this helps
I've got a view controller xib file with several views in it. I'm building a wizard-type interface. I'm just doing a simple fade between the views, and I'm already using a navigation controller for the main interface. I'd prefer not to use one for this wizard. Anyway, in the views, each panel has at least a button, some sort of input field (usually a UITextField) and some helper text hard coded in a UILabel.
The problem is that not all the UITextField objects are calling the textFieldDidChange method in the delegate (File's Owner - .m file associated with the xib), but all the UITextField objects ARE calling the textFieldDidBeginEditing method.
Makes no sense to me. I feel like I must be missing something simple in how I set up the screens, but I'll be darned if I can figure it out. Each of the screens all look identical in the property sheets (on the right hand side of Xcode), and everything is wired up correctly in the File's Owner property sheet, both in IBOutlet and IBActions.
Here are some shots of what's going on...
Ideas? Thanks.
Here are links to the screen caps of the vital parts.
(being a new member is making it hard to add all the info I need with screen caps!)
As far as I now, there is no delegate method with the header textFieldDidChange. You have created a method of your own, which is depending on a NSNotification. Make sure all the UITextFields are send the right notification.
There is no such method on a UITextFieldDelegate
You may have confused textViewDidChange, which is a delegate method for a UITextView, but itis passed the UITextView that generated the event, not an NSNotification.
Seems like you want textField:shouldChangeCharactersInRange:replacementString: instead.
This is resolved. I'm a knucklehead. :-)
I was attaching my own notifier/observer and hadn't done so for the last few UITextField objects. Sorry to bother y'all.