zoukankan      html  css  js  c++  java
  • iphone 自定义UISwitch

    链接地址:http://www.cnblogs.com/mainPage/archive/2010/10/28/1863472.html

    修改UISwitch的标题,实现自定义UISwitch方法有两种: 

    1.使用类别扩展UISwitch。 

    如下:

     下面是UISwitch.h文件:

    #import <UIKit/UIKit.h>

    @interface UISwitch (tagged)

    + (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2;

    @property (nonatomicreadonly) UILabel *label1;

    @property (nonatomicreadonly) UILabel *label2;

    @end

    UISwitch.m文件:

    #import "UISwitch-Extended.h"

    #define TAG_OFFSET 900

    @implementation UISwitch (tagged)

    - (void) spelunkAndTag: (UIView *) aView withCount:(int *) count

    {

     for (UIView *subview in [aView subviews])

     {

     if ([subview isKindOfClass:[UILabel class]])

     {

     *count += 1;

     [subview setTag:(TAG_OFFSET + *count)];

     }

     else 

     [self spelunkAndTag:subview withCount:count];

     }

    }

    - (UILabel *) label1 

     return (UILabel *) [self viewWithTag:TAG_OFFSET + 1]; 

    }

    - (UILabel *) label2 

    return (UILabel *) [self viewWithTag:TAG_OFFSET + 2]; 

    }

    + (UISwitch *) switchWithLeftText: (NSString *) tag1 andRight: (NSString *) tag2

    {

     UISwitch *switchView = [[UISwitch allocinitWithFrame:CGRectZero];

    int labelCount = 0;

    [switchView spelunkAndTag:switchView withCount:&labelCount];

    if (labelCount == 2)

    {

    [switchView.label1 setText:tag1];

    [switchView.label2 setText:tag2];

    }

    return [switchView autorelease];

    }

    @end

    2.还有一种方法,这种方法比较简单,但比较难懂,我不甚理解。


     

     

    UISwitch *isFooOrBar=[[UISwitch allocinit];


    ((UILabel *)[[[[[[isFooOrBar subviewslastObjectsubviewsobjectAtIndex:2subviews]objectAtIndex:0]).text = @"Foo";

    ((UILabel *)[[[[[[isFooOrBar subviewslastObjectsubviewsobjectAtIndex:2subviews]objectAtIndex:1]).text = @"Bar";

  • 相关阅读:
    commands.getstatusoutput和subprocess.call结果不一致
    win10 企业版 2015 长期服务激活
    pycharm设置护眼模式
    实战--滚动菜单
    javascript中this的用法
    jQuery之remove与empty的区别
    clone方法案例实践
    jQuery内部插入与外部插入
    jQuery文档处理
    2020/02/11星期二复习
  • 原文地址:https://www.cnblogs.com/xingchen/p/2240225.html
Copyright © 2011-2022 走看看