iOS开发中的典型代码-持续更新中~~~

1. 更改类名
选中-》右键-》refactor-》rename
2.创建视窗
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor cleanColor];
[self.window makeKeyAndVisible];

self.window.rootViewController = [[ViewController alloc] init];

3.常用属性
1)UIActionSheet
 
初始化:UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@“..” delegate:self cancelButtonTitle:@“..” destructiveButtonTitle:@“..” otherButtonTitles:@“..”, @“..”, nil];
显示:[shee showInView:self.view];

2)UIAlertView
初始化:UIAlertView *calView = [[UIAlertView alloc] initWithTitle:@“..” message:@“..” delegate:self cancelButtonTitle:@“..” otherButtonTitles:@“..”, @“..”, nil];
显示:[calView show];
常用代理方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;//buttonIndex为点击的按钮index

3)UIWebView //常用操作在其代理方法中实现
self.myweb.scalesPageToFit = YES;//fit to the page
[self.myweb loadRequest:request];//load the NSURLRequest

4)UITextField
[self.textfiled setKeyboardType:UIKeyboardTypeURL];//set the keyboard style
[self.textfield setClearButtonMode:YES];set the clear mark
textField.placeholder = @“placeholder”;
textField.borderStyle = UITextBorderStyleLine;
textField.secureTextEntry = YES;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.delegate = self;//textField中最常用的是其里面的代理
autocapitalizationType, keyboardType, borderStyle, returType, clearButtonMode, placeholder, secureTextEntry, textAlignment, textColor, font, text

UITextField委托方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//将要开始输入时调用
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//将要输入结束时调用
- (BOOL)textFieldShouldClear:(UITextField *)textField;//清除文字按钮点击事件
- (BOOL)textFieldShouldReturn:(UITextField *)textField;//键盘上的return按钮



5)NSURL
NSURL *url = [NSURL URLWithString: urlString];

6)NSURLRequest
NSURLRequest *request = [NSURLRequest requestWithURL: url];

7)UIActivityIndicatorView 旋转的菊花
UIActivityIndicatorView *activity = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

8)UISegmentedControl 
NSArray *array = @[@“..”, @“..”, @“..”];
UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:array];
segment.frame =..;
segment..selectedSegmentIndex = ..;
[segment addTarget:self action:SEL forControlEvents:..];

9)UITextView 
UITextView *textView = [[UITextView alloc] initWithFrame:..];
textView.text = @“abc\ndef\n….”;//使用\n换行

10)UISlider
minimumValue, maximumValue, value…

11)UIToolBar
UIToolbar *barTest = [[UIToolBar alloc] initWithFrame:…];
NSArray *items = [[NSArray alloc] initWithObjects:item1, item2, item3,nil];
barTest.items = items;

UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:.. style:.. target:self action:SEL)];

12)文件操作
NSString *context = @“..”;
NSString *localPath = [NSTemporaryDirectory() stringByAppendingString:@text.txt”];
NSError *error = nil;
写入:[context writeToFile:localPath atomically:YES encoding:NSUTF8StringEncoding error:&error];
读出: NSString *readString = [[NSString alloc] initWithContentOfFile:localPath encoding:NSUTF8StringEncoding error:&error];
输入读出对象必须是一致的,如果是NSDictionary,则读出的也是NSDictionary;

2)获取子视图等操作
获取子视图:NSArray *subviews = self.view.subviews;

for(UIView *view in subviews)
{
view.backgroundColor = …//对子视图(view)进行相关操作
}

获取父视图:UIView *view = self.view1.superview;

获取中心点:self.view.center = CGPointMake(..);

父视图通过tag查找对应的子视图
UIView *viewSub = [self.view viewWithTag:..];
[viewSub removeFromSuperview];//从父视图中移除

3)动画效果
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:5];
..//动画效果
[UIView commitAnimations];

 对transform属性的操作
 CGAffineTransformScale(..); CGAffineTransformTranslate(..); CGAffineTransformRotate(..);

4)对按钮不同状态的图片进行变换
[iButton setImage:.. forState:UIControlStateNormal];
[iButton setImage:.. forState:UIControlStateSelected];
[iButton addTarget:self action:@selector(changeState:) forControlEvents:UIControlEventTouchUpInside];

- (void)changeState:(UIButton *)button
{
button.selected = !button.selected;// 一定要对selected状态进行修改,否则不会是选中状态
}

5)定时器的使用(NSTimer)
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(showView) userInfo:nil repeats:YES];//auto start

[timer invalidate];//stop
timer = nil;//timer失效之后,一定要将其设置为nil

6)收起键盘
UITextField *view = (UITextField *)[self.view viewWithTag:1];
[view resignFirstResponder];

7)创建视图UI的xib文件
NSBundle *bundle = [NSBundle mainBundle];
NSArray *arr = [bundle loadNibNamed:@"..” owner:self options:nil];
UIView *myView = [arr objectAtIndex: 0 ];

8)UIView的常用方法
addSubview:添加子视图
insetSubview: atIndex:视图插入到指定索引位置
insetSubview: aboveSubview:视图插入制定视图之上
insetSubview: belowSubview:视图插入制定视图之下
bringSubviewToFront:把视图移动到最顶层
sendSubviewToBack:把视图移动到最底层
exchangeSubviewAtIndex: withSubviewAtIndex:两个视图对换位置
removeFromSuperview:把视图从父视图中移除

常用属性
multipleTouchEnabled:是否开启多点触摸
userInteractionEnabled:是否响应触摸事件

UIView属性的动画
frame - 改变视图的尺寸和位置
bounds - 改变视图的尺寸
center - 改变视图的位置
transform - 翻转或者缩放视图
alpha - 改变视图透明度
contentStetch - 改变视图如何拉伸

模态视图
模态视图:通过视图控制器的presentModalViewController方法弹出的视图。
一般都是用在临时窗口,如登陆窗口。
模态视图弹出时通过modalTransitionStyle属性设置不同的动画效果。
调用dismissModalViewControllerAnimated:方法关闭窗口。

ModelViewController *mc = [[ModelViewController alloc] init];
mc.modalTransitionStyle = UIModalTransitionStyleFlipStyleFlipHorizontal;
[self presentModalViewController:mc animated:YES];
 返回上个视图:[self dismissViewControllerAnimated:YES completion:nil];

导航控制器
创建子控制器:RootViewController *rootVc = [[RootViewController alloc] init];
创建导航控制器:UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:rootVc];
创建第二个子控制器:SecondViewController *secondVC = [[SecondViewController alloc] init];
导航到第二个控制器:[self.navigationController pushViewController:secondVC animated:YES];
返回上一个视图:[self.navigationController popViewControllerAnimated:YES];

返回到上一个视图:[self.navigationController 
以栈的形式对控制器进行管理,先进后出,后进先出
常用属性
@property (nonatomic,readonly,retain) UIViewController *topViewController;
@property (nonatomic,readonly, retain) UIViewController *visibleViewController;
@property (nonatomic, copy) NSArray *viewControllers;
@property (nonatomic, readonly) UINavigationBar *navigationBar;

常用方法
 - (id)initWithRootViewController: (UIViewController *)rootViewController;

- (void)pushViewController: (UIViewController *)viewController animated: (BOOL)animated;

- (UIViewContrller *)popViewControllerAnimated:(BOOL)animated;

- (NSArray *)popToViewController:(UIViewController *)viewController animate:(BOOL)animated;

- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

设置NavigationBar的属性
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor], NSFontAttributeName:[UIFont systemFontOfSize: 18]}];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
[[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@..”] forBarMetrics:UIBarMetricsDefault];

使用图片作为背景颜色的填充
nav.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@“..”]];

- (UIStatusBarStyle)preferredStatusBarStyle
{
return UIStatusBarStyleDefault;
}

放置多个BarButtonItem在同一侧:
self.navigationItem.leftBarButtonItems = @[button1, button2];

[textView becomeFirstResponder];//使其成为第一响应
textView.returnKeyType = UIReturnKeySend;

 收起键盘的两种方式
[textView resignFirstResponder];
[self.view endEditing:YES];

self.navigationController.navigationBar.translucent = YES;//导航栏设置为半透明

self.tabBarItem.badgeValue = @“New”;
设置nav上的可移动的图片:self.bgImage.center = button.center;

移除tabBar上原有的item

- (void)removeView
{
NSArray *viewArray = self.tabBar.subviews;
Class tabButtonClass = NSClassFromString(@“UITabBarButton”);
for(UIView *view in viewArray)
{
if([view isKindOfClass:[tabButtonClass class]]){
[view removeFromSuperview];
}
}
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self removeView];
}

 设置navigationController的item时,不用再前面加navigationController,即直接使用如下方式
self.navigationItem.rightBarButtonItem = rightItem;








































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值