LoginSignup
3
2

More than 5 years have passed since last update.

RubyMotionでUITextViewの状態監視

Posted at

UITextViewでのテキスト編集開始や変更を検知するためのサンプルコードとその実行結果。

1. ディレクトリ構成

$ tree app
app
├── app_delegate.rb
└── controllers
    └── sample_controller.rb

1 directory, 2 files

2. app_delegate.rbを編集する

SampleControllerをルートビューとして配置しているだけ

class AppDelegate
  def application(application, didFinishLaunchingWithOptions:launchOptions)
    @window = UIWindow.alloc.initWithFrame(UIScreen.mainScreen.bounds)
    @window.makeKeyAndVisible

    sample_ctrl = SampleController.alloc.initWithNibName(nil, bundle: nil)
    @window.rootViewController = UINavigationController.alloc.initWithRootViewController(sample_ctrl)
    true
  end
end

3. sample_controller.rbを編集する

テキストの編集開始、変更を扱う4つのメソッドを実装している。
他にもテキストの完了を監視するメソッドなどもある。

class SampleController < UIViewController

  def viewDidLoad
    super
    self.title = 'Sample'

    text_view = UITextView.new
    text_view.delegate = self
    text_view.backgroundColor = UIColor.grayColor
    text_view.frame = CGRectMake(
      0,
      0,
      self.view.bounds.size.width,
      self.view.bounds.size.height
    )

    self.view.addSubview(text_view)
  end

  #
  # テキストの編集が開始される直前に呼ばれる
  #
  def textViewShouldBeginEditing(textView)
    puts 'Before begin'
    true # ここでfalseを返すと編集が開始されない
  end

  #
  # テキストの編集を開始したタイミングで呼ばれる
  #
  def textViewDidBeginEditing(textView)
    puts 'Begin'
  end

  #
  # テキストの状態が変更されるタイミングで呼ばれる
  #
  def textViewDidChange(textView)
    puts 'Change'
  end

  #
  # カーソルの動き、選択範囲の変更が行われるタイミングで呼ばれる
  #
  def textViewDidChangeSelection(textView)
    puts 'Change Selection'
  end

end

4. 実行する

まずrakeでエミュレータを起動

$ rake
================================================================================
A new version of RubyMotion is available. Run `sudo motion update' to upgrade.
================================================================================

     Build ./build/iPhoneSimulator-7.1-Development
    Create ./build/iPhoneSimulator-7.1-Development/event.app/Info.plist
  Simulate ./build/iPhoneSimulator-7.1-Development/event.app
(main)>

画面はこうなる。
スクリーンショット 2014-09-06 18.11.38.png

この状態で画面をタップして「abc」と入力してEnterを押す。
スクリーンショット 2014-09-06 18.13.14.png

ターミナルには以下のように出力されている。
それぞれのイベントタイミングに応じてメソッドが呼び出されていることがわかる。

$ rake
================================================================================
A new version of RubyMotion is available. Run `sudo motion update' to upgrade.
================================================================================

     Build ./build/iPhoneSimulator-7.1-Development
    Create ./build/iPhoneSimulator-7.1-Development/event.app/Info.plist
  Simulate ./build/iPhoneSimulator-7.1-Development/event.app
(main)> Before begin
Begin
Change Selection
Change
Change Selection
Change
Change Selection
Change
Change Selection
Change Selection
Change
Change Selection
Change
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2