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]);
}
//给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]);
}
相关文章推荐
- CodeForces 12C Fruits
- CodeForces 373B Making Sequences is Fun
- Java基础知识强化09:String、StringBuffer和StringBuilder使用
- UISlider
- USACO-Section 2.1 Sorting a Three-Valued Sequence (贪心)
- leetcode[187]Repeated DNA Sequences
- 分享一个递归无限级拼接Json的方法---ExtJs的TreePanel和TreeGrid均适用(Ef,Lambda,Linq,IQueryable,List)
- UITabBarController学习笔记
- 多校10 1007 CRB and Queries
- jQuer基础
- Wow! Such Sequence!(线段树4893)
- Wow! Such Sequence!(线段树4893)
- UILabel属性
- [LeetCode] Closest Binary Search Tree Value II
- iOS阶段学习第33天笔记(自定义标签栏(UITabBar)介绍)
- Memo 安装 Mirantis Fuel - OpenStack安装和管理软件
- C#基础------可空类型 StringBuilder
- queue
- IOS基础UI之(一)简单的qq登录
- UITableViewCell 自适应高度