您的位置:首页 > 产品设计 > UI/UE

UISegmentedControl的属性

2015-08-30 20:03 459 查看
    //当前已经选中的segment  [sender selectedSegmentIndex]
    //给segment上的文本,可以通过索引来找到sender titleForSegmentAtIndex:
.h文件

#import <UIKit/UIKit.h>
@interface AppDelegate :
UIResponder <UIApplicationDelegate>
{
    UILabel * la;
}
@property (strong,
nonatomic) UIWindow *window;

@end

.m文件

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow
alloc]initWithFrame:[[UIScreen
mainScreen]bounds]];
    //建立一个数组,为了能够在分段标签上分别显示
    NSArray * item = [[NSArray
alloc]initWithObjects:@"one",@"two",@"three",nil];
    //通过建立的数组来初始化segment控件
    UISegmentedControl *sc = [[UISegmentedControl
alloc]initWithItems:item];
    sc.frame=CGRectMake(50,
50,120,
37);
    sc.backgroundColor = [UIColor
whiteColor];
    //给segment控件增加事件,注意这里用到了UIControlEventValueChanged,增加事件是为了点击不同的标签,lable里有显示
    [sc addTarget:self
action:@selector(act:)
forControlEvents:UIControlEventValueChanged];
    //给segment增加图片setimage
    [sc setImage:[UIImage
imageNamed:@"bz.png"]
forSegmentAtIndex:0];
    //增加标签
    la = [[UILabel
alloc]initWithFrame:CGRectMake(180,
50, 50,
37)];
    la.backgroundColor = [UIColor
redColor];
    //写死标签的默认值
    la.text=@"two";
    //让segment默认选中第二个,和写死的lable对应
    [sc setSelectedSegmentIndex:1];
    
    [self.window
addSubview:la];
    [self.window
addSubview:sc];
    [self.window
makeKeyAndVisible];
    return
YES;
}

-(void)act:(UISegmentedControl *)sender
{
    //对于分段控件,每段都有一个索引值
    //当前已经选中的segment  [sender selectedSegmentIndex]
    //给segment上的文本,可以通过索引来找到sender titleForSegmentAtIndex:
    la.text = [sender
titleForSegmentAtIndex:[sender
selectedSegmentIndex]];
   // NSLog(@"%d", [sender selectedSegmentIndex]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: