0

I have a registration screen of textFields & textViews placed on a scroll view. For moving the view above text field I used the following codes -

mark: I have connected all the delegates properly.

var activeTextField:UITextField?
viewDidLoad()
{
 NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWasShown:"), name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardWillBeHidden:"), name: UIKeyboardWillHideNotification, object: nil)
} 

 override func viewWillDisappear(animated: Bool)
    {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardDidHideNotification, object: nil)
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UIKeyboardWillHideNotification, object: nil)
    }
func textFieldDidBeginEditing(textField: UITextField)
    {
        self.activeTextField = textField 
    }
    func textFieldDidEndEditing(textField: UITextField)
    {
        self.activeTextField = nil
    }

func textViewDidBeginEditing(textView: UITextView)
    {
        textView.becomeFirstResponder()
        self.scrollView.setContentOffset(CGPointMake(0, 180), animated: true)

    }
    func textViewDidEndEditing(textView: UITextView)
    {
        self.scrollView.setContentOffset(CGPointMake(0, 0), animated: true)
        textView.resignFirstResponder()

    }

 func keyboardWasShown(notification: NSNotification)
    {
        // Step 1: Get the size of the keyboard.
        let info : NSDictionary = notification.userInfo!
        let keyboardSize = (info.objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue as CGRect!).size
        // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
        let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
        // Step 3: Scroll the target text field into view.
        var aRect: CGRect = self.view.frame
        aRect.size.height -= keyboardSize.height
        if !CGRectContainsPoint(aRect, activeTextField!.frame.origin)
        {
            let scrollPoint: CGPoint = CGPointMake(0.0, activeTextField!.frame.origin.y - (keyboardSize.height - 15))
            self.scrollView.setContentOffset(scrollPoint, animated: true)
        }
    }

    func keyboardWillBeHidden(notification: NSNotification)
    {
        let contentInsets: UIEdgeInsets = UIEdgeInsetsZero
        self.scrollView.contentInset = contentInsets
        self.scrollView.scrollIndicatorInsets = contentInsets
    }

But as soon as I click on the textView I get an error message as -

enter image description here

But there is no error related breaks in my viewController. So what might be the error?

Kamala Dash
  • 245
  • 3
  • 16
  • 1
    It means some value which is not expected to be `nil` is set to `nil`. – Tejas K Mar 02 '16 at 09:24
  • You should set a breakpoint where the exception is thrown (http://stackoverflow.com/a/17802723/727817). – alex-i Mar 02 '16 at 09:26
  • I have tried every where. Whenever I remove the notification part,it goes well but when I register for the notifications then only the problem arises. – Kamala Dash Mar 02 '16 at 09:28

0 Answers0