Basic use of Rxswift controls

Source: Internet
Author: User
Tags bind

1, if we want to implement, click on the view can let the text box in the view stop editing, we can do this in the storyboard to drag a gesture out, and then in referencing Outlets collection to go and view to the association.


And then we can write it directly in the code.

The first implementation of
TapGestureRecognizer.rx.event
            . Subscribe (onNext: {[unowned Self] _ in
                self.view.endEditing ( true)
            }). Disposed (by:disposebag)

//The second implementation method
TapGestureRecognizer.rx.event.bind {[unowned self] _ in           
self.view.endEditing (True)
}
. Disposed (By:disposebag)
2. Bind the label with the TextField

TextField.rx.text
            . Bind (To:textFieldLabel.rx.text)
            . Disposed (by:disposebag)

3. Calculate the number of words in the text box

TextView.rx.text.asDriver ()
            . Map {
                "Character count: \ ($0!. Count) "
            }
            . Drive (TextViewLabel.rx.text)
            . Disposed (by:disposebag)
//We can of course, Add a orempty and you don't have to unpack it
. TextView.rx.text.orEmpty.asDriver ()
. Map {
"count: \ ($0.count ) "
}
. Drive (TextViewLabel.rx.text)
. Disposed (by:disposebag)

4, the button can be clicked and some of the actions we need to be bound together

Button.rx.tap
            . bind {
                self.buttonlabel.text! + = "click"
                self.view.endEditing (True)
                //This will be animated.
               Uiview.animate (withduration:0.3) {
                    self.view.layoutIfNeeded ()
                }
            }
            . Disposed (by:disposebag)
        

5, Uistepper value is bound to the label above

Stepper.rx.value
            . Map {String ($)}
            . Bind (To:stepperLabel.rx.text)
            . Disposed (by:disposebag)

6, binding Segmentedcontrol Selectindex, where the value is actually selectindex, and then skip (1) Is the setting of the skip response Segmentedcontrollabel The first step of setting the text to 1

SegmentedControl.rx.value
            . Skip (1)
            . bind {[unowned self] in
                self.segmentedControlLabel.text = "Selected \ ($) "
                
                uiview.animate (withduration:0.3) {
                    self.view.layoutIfNeeded ()
                }
            }
            . Disposed (by: Disposebag)
Effect Diagram

7. Change the value of the progress bar according to the change of UISlider

Slider.rx.value
            . Bind (to:progressView.rx.progress)
            . Disposed (by:disposebag)

8, using the switch is to control the network indicator of the hidden and display and rotation or do not turn

  Switch.rx.value
            . Map {!$0}
            . Bind (To:activityIndicator.rx.isHidden)
            . Disposed (by:disposebag)
  Switch.rx.value.asDriver ()
. Drive (activityIndicator.rx.isAnimating)
. Disposed (By:disposeb Ag

9, the date selector inside the date given to the label

DatePicker.rx.date
            . map {[unowned self] in
                "Date:" + self.dateFormatter.string (from: $)
            }
            . Bind ( To:datePickerLabel.rx.text)
            . Disposed (by:disposebag)
As well as the date formatter we set, NSDateFormatter comes with several formats, so the next thing we do is to set up these own formats.

Lazy var dateformatter:dateformatter = {Let
        formatter = Dateformatter ()
        Formatter.datestyle =. Medium
        Formatter.timestyle =. Short
        return Formatter
    } ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.