ios控件学习 代码实现

1.创建一个button,并触发一个alert事件

- (void)viewDidLoad
{
[super viewDidLoad];

b1button=[UIButton buttonWithType:1];
b1button.frame=CGRectMake(0, 0, 100, 50);//位置 大小
[b1button setTitle:@"button1" forState:UIControlStateNormal];//名称
b1button.backgroundColor=[UIColor brownColor];//背景颜色
[b1button addTarget:self action:@selector(buttonpressed) forControlEvents:UIControlEventTouchUpInside];//触发事件
[self.view addSubview:b1button];//button添加入view
}

-(IBAction)buttonpressed
{
UIAlertView * alert1=[[UIAlertView alloc] initWithTitle:@"myView" message:@"hahah" delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil];
[alert1 addButtonWithTitle:@"确定2"];
[alert1 show];
}

2. NavigationBar:创建一个navigationBar,并在上面添加2个按钮。

首先创建一个navigationbar,再在上面加入navigationItem,然后在navigationItem上加入2个barButtonItem。

- (void)viewDidLoad
{
[super viewDidLoad];


UINavigationBar * navigationbar=[[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];//创建导航栏
UINavigationItem * navigationitem=[[UINavigationItem alloc] initWithTitle:@"hello"];//创建导航栏集合

[navigationbar pushNavigationItem:navigationitem animated:NO];//把navigationitem合放入导航栏

UIBarButtonItem * leftbutton=[[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStylePlain target:self action:@selector(clickleftbutton)];//左按钮
UIBarButtonItem * rightbutton=[[UIBarButtonItem alloc] initWithTitle:@"right" style:UIBarButtonItemStyleDone target:self action:@selector(clickrightbutton)];//右按钮

[navigationitem setLeftBarButtonItem:leftbutton];//把按钮加入navigationitem
[navigationitem setRightBarButtonItem:rightbutton];

[self.view addSubview:navigationbar];
// Do any additional setup after loading the view, typically from a nib.
}


-(void) showMessage:(NSString *)str
{
UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:@"hello" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
[alertview show];
}


-(void) clickleftbutton
{
[self showMessage:@"按了左边按钮"];
}

-(void) clickrightbutton
{
[self showMessage:@"按了右边按钮"];
}





3.创建一个tableView,并写入几条记录。

先创建一个single view application。

在viewController.h中添加:

@interface ViewController : UIViewController
<UITableViewDataSource,UITableViewDelegate>//添加 协议
{
NSMutableArray * titleList;//添加 记录
UITableView * tableview1;//添加 tableView
}
@property (retain,nonatomic) NSMutableArray * titleList;//添加
@property (retain,nonatomic) UITableView * tableview1;//添加

@end

在viewController.m中添加:

@synthesize titleList;
@synthesize tableview1;

修改viewDidLoad方法:

- (void)viewDidLoad
{
[super viewDidLoad];

self.tableview1=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain];//创建tableView
tableview1.delegate=self;//连接委托
tableview1.dataSource=self;//连接数据源
[self.view addSubview:self.tableview1];
self.titleList =[NSMutableArray arrayWithObjects:@"a1",@"a2",@"a3", nil];//记录
}

实现UITableViewDataSource协议中的2个方法:

#pragma mark - tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.titleList.count;//返回行数
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//此方法每一行创建的时候都要调用一次
static NSString * sectionID=@"sectionID_1";//sectionID 当前只有一个section
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:sectionID];

if (cell==nil)
{
cell=[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionID];
}
NSInteger row=[indexPath row];//获取当前行
cell.textLabel.text=[titleList objectAtIndex:row];//为当前行赋值
return cell;
}




4.创建label

//创建label视图  
label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 30)];
//设置显示内容
label.text = @"雨松MOMO的程序世界";
//设置背景颜色
label.backgroundColor = [UIColor blueColor];
//设置文字颜色
label.textColor = [UIColor whiteColor];
//设置显示位置居中
label.textAlignment = UITextAlignmentCenter;
//设置字体大小
label.font = [UIFont fontWithName:[[UIFont familyNames] objectAtIndex:10] size:20];
//设置字体大小适应label宽度
label4.adjustsFontSizeToFitWidth = YES;

//设置label的行数

label5.numberOfLines = 2; // =0时 行数无限制 自动换行


//设置高亮

label6.highlighted = YES;

label6.highlightedTextColor = [UIColor orangeColor];

//设置阴影

label7.shadowColor = [UIColor redColor];

label7.shadowOffset = CGSizeMake(1.0,1.0);

//设置是否能与用户进行交互

label7.userInteractionEnabled = YES;



//设置label中的文字是否可变,默认值是YES

label3.enabled = NO;

//设置文字过长时的显示格式

label3.lineBreakMode = UILineBreakModeMiddleTruncation;//截去中间

// typedef enum {

// UILineBreakModeWordWrap = 0,

// UILineBreakModeCharacterWrap,

// UILineBreakModeClip,//截去多余部分

// UILineBreakModeHeadTruncation,//截去头部

// UILineBreakModeTailTruncation,//截去尾部

// UILineBreakModeMiddleTruncation,//截去中间

// } UILineBreakMode;


//如果adjustsFontSizeToFitWidth属性设置为YES,这个属性就来控制文本基线的行为

label4.baselineAdjustment = UIBaselineAdjustmentNone;

// typedef enum {

// UIBaselineAdjustmentAlignBaselines,

// UIBaselineAdjustmentAlignCenters,

// UIBaselineAdjustmentNone,

// } UIBaselineAdjustment;



http://blog.csdn.net/xys289187120/article/details/6831917  Objective-C语法之第一个iPhone应用程序的那些事儿

http://hi.baidu.com/bunsman/blog/item/95777b0ebacf05fe36d122e2.html [iPhone开发之控件的使用]UILabel的各种属性与方法的使用



5.创建imageview

    imageview=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 48*7)];
[imageview setImage:[UIImage imageNamed:@"1.png"]];
[self.view addSubview:imageview];



原文地址:https://www.cnblogs.com/phoenix13suns/p/2341623.html