Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

RadioButton

imchristian_g edited this page Mar 20, 2018 · 1 revision

radio

Un MLRadioButton es un widget que tiene dos estados, on y off. El uso del mismo, combinado con el MLRadioButtonCollection provee una estructura de selección de items de forma excluyente.

Radio Button

Uso

Creación

Por XIB

para agregar un radio button a un xib, seguir los siguientes pasos:

  1. Agreagar una vista al xib
  2. Ponerle como custom class la clase MLRadioButton

screen shot 2016-06-13 at 10 26 35 am

Por código usando Autolayout

Para crearlo usando Autolayouts, inicializar el botón con el siguiente método:

[[MLRadioButton alloc] init];

y luego aplicarle los constraints

Observaciones

El radio button preferentemente tiene que ser cuadrado de 15x15

Jerarquía

mlradiobuttonclassdiagram

Ejemplo de uso:

  • Inicializando el radio button
MLRadioButton *radioButton = [[MLRadioButton alloc] init];
  • Seteándole el estado luego de que fue creado
[radioButton off]; // Por default el MLRadioButton se seteará en off
  • Verificando el estado
[radioButton isOff];

Radio Button Collection

Uso

Creación

Con radio buttons

Se puede crear un radio button collection con un NSArray de MLRadioButton y ésto crea el MLRadioButtonCollection con el primer item en fill y el resto de los items en clear

MLRadioButton *radioButton0 = [[MLRadioButton alloc] init];
MLRadioButton *radioButton1 = [[MLRadioButton alloc] init];
MLRadioButton *radioButton2 = [[MLRadioButton alloc] init];
    
MLRadioButtonCollection *radioButtonCollection = [MLRadioButtonCollection radioButtonCollectionWithRadioButtons:@[radioButton0, radioButton1, radioButton2]];
Con longitud

Se puede crear un radio button collection con una longitud y ésto creara internamente una lista de MLRadioButton.

[MLRadioButtonCollection radioButtonCollectionWithRadioButtonCout:4]

Luego se puede acceder a ésta lita de la siguiente manera:

[radioButtonCollection radioButtons]

Observaciones

Si el radio button se crea con 0 elementos o elementos negativos, éste lanzará una exception.

Interfáz

/**
 *  MLRadioButtonCollection manage the exclusiveness of an array of MLRadioButtons
 */
@interface MLRadioButtonCollection : NSObject

/**
 *  Static initialiser that returns an instance of MLRadioButtonCollection.
 *  This method might be called only if you want to create the
 *  MLRadioButton objects, if you already have this object, whether you initialised theme in the
 *  xib file or by code, you might initialise the MLRadioButtonCollection with
 *  radioButtonCollectionWithRadioButtons: initialiser
 *
 *  @parms radioButtonCount    the amount of MLRadioButton objects that might initialise
 *  the return instance
 */
+ (instancetype)radioButtonCollectionWithRadioButtonCount:(NSUInteger)radioButtonCount;

/**
 *  Static initialiser that returns an instance of MLRadioButtonCollection.
 *  This method might be called only if you already have the
 *  MLRadioButton objects, if you dont have this object, you might initialise
 *  the MLRadioButtonCollection with radioButtonCollectionWithRadioButtonCount: initialiser
 *
 *  @parms radioButtons    An NSArray of MLRadioButtons
 */
+ (instancetype)radioButtonCollectionWithRadioButtons:(NSArray *)radioButtons;

/**
 *  Array of MLRadioButtons that will have exclusive selection
 */
- (NSArray *)radioButtons;

/**
 *  Changes the state of the MLRadioButton at the index sent by parameter and sets to off
 *  the old MLRadioButton with state in on
 *
 *  @parms index    index of selected MLRadioButton
 */
- (void)selectRadioButtonAtIndex:(NSUInteger)index;

/**
 *  Returns the index of the currently MLRadioButton with state in on
 */
- (NSUInteger)indexOfSelectedRadioButton;
Ejemplo de uso:
  • Inicializando el radio button collection
MLRadioButtonCollection *radioButtonCollection = [MLRadioButtonCollection radioButtonCollectionWithRadioButtonCout:3];
  • Seleccionando un radio button en particular
[radioButtonCollection selectRadioButtonAtIndex:2];
  • Obteniendo el índice del radio button seleccionado
NSUInteger indexOfSelectedRadioButton = [radioButtonCollection indexOfSelectedRadioButton];