OC语言的学习

UI常用控件

//

//  Created by lanou on 16/7/10.

//  Copyright © 2016年pingguo. All rights reserved.

//

#import"ViewController.h"

@interfaceViewController()

@property(nonatomic,strong)UILabel*titleLabel;

@property(nonatomic,strong)UIButton*leftBtn;

@property(nonatomic,strong)UIButton*rightBtn;

@property(nonatomic,strong)UIImageView*myImageView;

@property(nonatomic,strong)NSArray*imageNames;

@end

@implementationViewController

- (void)viewDidLoad {

[superviewDidLoad];

self.imageNames=@[@"biaoqingdi",@"bingli",@"chiniupa",@"danteng",@"wangba"];

//创建并初始化按钮图片及文本

//表明文本的位置

self.titleLabel=[[UILabelalloc]initWithFrame:CGRectMake(160,60,150,30)];

//文本内容

self.titleLabel.text=@"biaoqingdi";

//将文本添加到视图

[self.viewaddSubview:self.titleLabel];

//表明左按钮的位置

self.leftBtn=[[UIButtonalloc]initWithFrame:CGRectMake(20,150,45,45)];

//关掉交互

self.leftBtn.userInteractionEnabled=NO;

//加载左按钮的图片

UIImage*leftImage=[UIImageimageNamed:@"left_disable"];

//左按钮的背景及按下状态

[self.leftBtnsetBackgroundImage:leftImageforState:(UIControlStateNormal)];

//将左按钮添加到视图

[self.viewaddSubview:self.leftBtn];

//表明图片的位置

self.myImageView=[[UIImageViewalloc]initWithFrame:CGRectMake(85,100,200,200)];

//加载图片

UIImage*image=[UIImageimageNamed:@"biaoqingdi"];

//设置myimageview显示的图片

self.myImageView.image=image;

//将图片添加到视图

[self.viewaddSubview:self.myImageView];

//以下同理

self.rightBtn=[[UIButtonalloc]initWithFrame:CGRectMake(305,150,45,45)];

UIImage*rightImage=[UIImageimageNamed:@"right_normal"];

[self.rightBtnsetBackgroundImage:rightImageforState:(UIControlStateNormal)];

[self.viewaddSubview:self.rightBtn];

//创立按钮监听

[self.rightBtnaddTarget:selfaction:@selector(rightBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

[self.leftBtnaddTarget:selfaction:@selector(leftBtnAction)forControlEvents:(UIControlEventTouchUpInside)];

oc基本语法

//

//  Created by lanou on 16/7/9.

//  Copyright © 2016年pingguo. All rights reserved.

//

#import

intmain(intargc,constchar* argv[]) {

@autoreleasepool{

// insert code here...

NSLog(@"Hello, World!");

  OC基本语法

//整型

NSIntegera =10;

NSLog(@"a=%ld",a);

CGFloat  b =2.3;

NSLog(@"b=%.2f",b);

BOOLflag =YES;

NSString*str=@"abcde";

NSLog(@"str=%@",str);

NSLog(@"str的长度=%ld",str.length);

if([strisEqualToString:@"abcde"]) {

NSLog(@"是的");

}

if([strhasPrefix:@"a"]) {

NSLog(@"前缀等于a");}

if([strhasSuffix:@"e"]) {

NSLog(@"后缀等于e");

}

NSString*str1=[NSStringstringWithFormat:@"%@++++",@"im"];

NSLog(@"str1=%@",str1);

}

return0;

}

oc基础语法:

//

//  Created by lanou on 16/7/9.

//  Copyright © 2016年pingguo. All rights reserved.

//

#import

intmain(intargc,constchar* argv[]) {

@autoreleasepool{

// insert code here...

NSLog(@"Hello, World!");

NSArray*array1=@[@"a",@"b",@"c",@"d"];

NSLog(@"array1=%@",array1);

NSLog(@"count=%ld",array1.count);

NSString*str = array1[0];

NSLog(@"str=%@",str);

NSMutableArray*mutableArray =[NSMutableArrayarrayWithObjects:@"1",@"2",@"3",@"4",nil];

NSLog(@"mutable Array=%@",mutableArray);

[mutableArrayaddObject:@"5"];

NSLog(@"已添加----%@",mutableArray);

[mutableArrayremoveObject:@"3"];

NSLog(@"已移除----%@",mutableArray);

NSDictionary*dict =@{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3"};

NSLog(@"dict=%@",dict);

NSString*string=[dictobjectForKey:@"key1"];

NSLog(@"string=%@",string);

NSLog(@"allkeys=%@,allvalues=%@",dict.allKeys,dict.allValues);

}

return0;

}

你可能感兴趣的:(OC语言的学习)