1.一个Controller是一个对象,属性是这样定义的:
在view.h头文件里:
@property (copy,nonatomic)NSString *userName;
在controller.m里:
@synthesize userName ;
2.事件是这样定义的
- (IBAction)changeGreeting:(id)sender {
self.userName=self.textField.text;
NSString *nameString =self.userName;
if([nameStringlength]==0){
nameString =@"world";
}
NSString *greeting = [[NSStringalloc]initWithFormat:@"Hello,%@!",nameString];
self.label.text=greeting;
}
产生事件的操作,是按住ctrl键,把对象拖到controller.m里
3.建立对象的变量(outlet),操作是按住ctrl键,然后用鼠标把它拖到view.h头文件里,产生类似这样的代码:
@property (weak, nonatomic) IBOutletUITextField *textField;
4.IOS有三种布局方式
- storyboard
- xib
继承自UIViewController
- 手写代码
也是继承自UIViewController
5.IOS类定义
Student.h
//只是用来声明
#import <Foundation/Foundation.h>
@interface Student : NSObject{
//成员变量定义在这里
int _age;
int _no;
}
- (void)setAge:(int)newAge; //一个冒号对应一个参数,方法名是setAge:
- (int)age; //动态方法
- (void)setNo:(int)no;
- (int)no;
-(id)initWithAge:(int) age andNo:(int)no;
@end
Student.m
#import "Student.h"
@implementation Student
-(int)age{
return _age;
}
-(void)setAge:(int)newAge{
_age=newAge;
}
-(void)setNo:(int)no{
_no=no;
}
-(int)no{
return _no;
}
-(id)initWithAge:(int)age andNo:(int)no{
if(self=[super init]){
_age=age;
_no=no;
}
return self;
}
- (NSString *)description{//redefine %@
return [NSString stringWithFormat:@"Age=%d,No=%d",_age,_no];
}
@end
main.m
#import <Foundation/Foundation.h>
#import "Student.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
//Student *stu=[Student alloc];
//stu =[stu init];
//Student *stu=[[Student alloc] init];
Student *stu=[[Student alloc] initWithAge:1 andNo:2];
//[stu setAge:100];
stu.age=10;
//int age=[stu age];
NSLog(@"%@",stu);
//[stu release];
}
return 0;
}
6.一些常用语法:
- @autorelesepool{} //自动释放内存池
- printf("My age is %d and my name is %s",1,"abc"); %o 八进制
- NSLog(@"Hello,");
- #import <Foundation/Foundation.h>
- staticNSString *const TitleKey=@"TITLE"; 定义常量
读取plist
NSString *path=[[NSBundle mainBundle] pathForResource:@"Products" ofType:@"plist"];
NSArray *array=[NSArray arrayWithContentsOfFile:path];
- 可变数组的定义与初始化
NSMutableArray *_productList;
_productList = [NSMutableArray arrayWithCapacity:array.count];
- 数组的遍历级可变数组增加元素:
[array enumerateObjectsUsingBlock:^(NSDictionary *dict,NSUInteger idx,BOOL *stop){
Product *product =[[Product alloc]init];
product.title=dict[TitleKey];
product.imageName=dict[ImageNameKey];
product.flag=[dict[FlagKey] integerValue];
[_productList addObject:product];
}];
- toolbar
[self.navigationController setToolbarHidden:NO animated:YES];
//设置toolbarHidden背景颜色
[self.navigationController.toolbar setBarTintColor:[UIColor redColor]];
//设置toolbarHidden样式,黑色,黑色透明等等,但貌似都是半透明效果
[self.navigationController.toolbar setBarStyle:UIBarStyleBlack];
//设置toolbarHidden背景图片,forToolbarPosition是位置状态是放在什么地方时显示设置它的位置,UIBarMetricsDefault是状态设置在竖屏还是横屏时显示
[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"navigationBar.png"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
//设置位置
self.navigationController.toolbar.frame=CGRectMake(0, 0, 375, 44);
float y=self.navigationController.toolbar.frame.origin.y + self.navigationController.toolbar.frame.size.height;
[self.tableView setFrame:CGRectMake(0,y,self.view.frame.size.width,self.view.frame.size.height-y)];
//重点是设置上面的按钮这些
//和设置navigationBarItem类似
//先设置一个UIBarButtonItem,然后组成数组,然后把这个数组赋值给self.toolbarItems
UIBarButtonItem *btn1=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *btn2=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:nil];
UIBarButtonItem *btn3=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:nil];
UIBarButtonItem *btn4=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
NSArray *arr1=[[NSArray alloc]initWithObjects:btn4,btn1,btn4,btn2,btn4,btn3,btn4, nil];
self.toolbarItems=arr1;
self.view.backgroundColor=[UIColor purpleColor];
- 背景颜色
- self.tableView.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.95 alpha:0.0];
- 弹出提醒
UIAlertView * alter = [[UIAlertView alloc] initWithTitle:@"标题" message:@"内容" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
- UIToolbar加间距
UIBarButtonItem *itemButtonEmpty = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[mycustomButtons addObjectsFromArray:[NSArray arrayWithObjects:myButton1,itemButtonEmpty,myButton2,itemButtonEmpty,myButton3,itemButtonEmpty,myButton4,itemButtonEmpty,myButton5,itemButtonEmpty,myButton6,itemButtonEmpty,nil]];
- ViewController跳转
7.一些ViewController相关的操作
跳转
<pre name="code" class="objc">
ViewController * content=[[ViewController alloc]init];
[self.navigationController pushViewController:content animated:true];
根据StoryboardId找到Controller
ViewController *content=[self.storyboard instantiateViewControllerWithIdentifier:@"viewControllerName"];
8.CGRect操作
CGRect frame= [[UIScreen mainScreen] bounds]; //返回APP区域
CGRect _frame=[[UIScreen mainScreen] applicationFrame]; //除去工具条的App区域
9.常量定义
首先,创建Constants.h
和Constants.m
文件用来存放我们的常量。 在Constants.h
中,指定常量名字,将常量声明为一个指向NSString
对象的指针:
1 | // Constants.h |
最后,在Constants.m
中通过赋值定义常量:
1 | // Constants.m |
字符串比较可以这样进行:
(@"myString" == MyConstant
)而不是字符串比较([@"myString" isEqualToString:MyConstant]
)
如有c或者c++混合的话使用FOUNDATION_EXPORT 来代替 extern