您的位置:首页 > 移动开发 > IOS开发

ios基础笔记(一)

2015-06-01 10:29 393 查看
1.设置按钮文字的尺寸为按钮自己的尺寸
button.size=[button.currentTitlesizeWithFont:button.titleLabel.font];
button.backgroundColor=[UIColorredColor];

2.常用尺寸
/**
44:cell的默认高度、导航栏的可见高度
49:UITabBar的默认高度
64:从窗口顶部到导航栏底部
20:状态栏高度
320:竖屏情况下的屏幕宽度
480:竖屏情况下的3.5inch的屏幕高度
568:竖屏情况下的4.0inch的屏幕高度
*/

3.textview改变通知
#warning不要设置自己的代理为自己本身
//监听内部文字改变
//self.delegate=self;

/**
监听控件的事件:
1.delegate
2.-(void)addTarget:(id)targetaction:(SEL)actionforControlEvents:(UIControlEvents)controlEvents;
3.通知
*/

//当用户通过键盘修改了self的文字,self就会自动发出一个UITextViewTextDidChangeNotification通知
//一旦发出上面的通知,就会调用self的textDidChange方法
[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(textDidChange)name:UITextViewTextDidChangeNotificationobject:self];

4.copy策略,重写setter方法,
-(void)setPlacehoder:(NSString*)placehoder
{
#warning如果是copy策略,setter最好这么写
_placehoder=[placehodercopy];

//设置文字
self.placehoderLabel.text=placehoder;

//重新计算子控件的fame,
[selfsetNeedsLayout];//重新布局子控件
}

4.UiTabbarbug修复
-(void)tabBarController:(UITabBarController*)tabBarControllerdidSelectViewController:(UINavigationController*)viewController
{
UIViewController*vc=[viewController.viewControllersfirstObject];
if([vcisKindOfClass:[HMHomeViewControllerclass]]){
if(self.lastSelectedViewContoller==vc){
[self.homerefresh:YES];
}else{
[self.homerefresh:NO];
}
}

self.lastSelectedViewContoller=vc;
}
self.lastSelectedViewContoller=vc;
/**
其实在这里隐藏着一个问题,如果如图所示你的4个TabBar都是指向4个NavigationController,那么没有问题,运行OK。
但如果你的4个TabBar有任何一个指向的不是NavigationController,那么程序就会crash。因为非NavigationController不能够响应popToRootViewControllerAnimated:方法。
*/

if([viewControllerisKindOfClass:[UINavigationControllerclass]]){
[(UINavigationController*)viewControllerpopToRootViewControllerAnimated:YES];
}

5.//自定义Log


#ifdefDEBUG

#defineWCLog(...)NSLog(@"%s%d\n%@\n\n",__func__,__LINE__,[NSStringstringWithFormat:__VA_ARGS__])

#else
#defineWCLog(...)
#endif



6.懒加载

#pragmamark懒加载

//-(NSMutableArray*)imageLinkURL

//{

//if(_imageLinkURL==nil){

//_imageLinkURL=[NSMutableArrayarray];

//}

//return_imageLinkURL;

//}

如果不使用懒加载,数组初始化方式如下,

NSMutableArray*imageArray=[NSMutableArrayarray];

for(AdImageModel*modelinimageModels){

[imageArrayaddObject:model.imageUrl];

}

self.imageLinkURL=imageArray;

不可变数组可以不初始化,可变数组必须初始化

7.block中循环引用

__weaktypeof(clearCache)weakClearCache=clearCache;

__weaktypeof(self)weakVc=self;

clearCache.operation=^{

[MBProgressHUDshowMessage:@"正在清除缓存...."];

//清除缓存

NSFileManager*mgr=[NSFileManagerdefaultManager];

[mgrremoveItemAtPath:imageCachePatherror:nil];

//设置subtitle

weakClearCache.subtitle=nil;

//刷新表格

[weakVc.tableViewreloadData];

[MBProgressHUDhideHUD];

};
8.用搜索子控制器中寻找父控制器


-(UIViewController*)viewController{
for(UIView*next=[selfsuperview];next;next=next.superview){
UIResponder*nextResponder=[nextnextResponder];
if([nextResponderisKindOfClass:[UIViewControllerclass]]){
return(UIViewController*)nextResponder;
}
}
returnnil;
}






@interfaceUIView(Extend)
-(UIViewController*)viewController;
@end

@implementationUIView(Extend)

-(UIViewController*)viewController{
for(UIView*next=[selfsuperview];next;next=next.superview){
UIResponder*nextResponder=[nextnextResponder];
if([nextResponderisKindOfClass:[UIViewControllerclass]]){
return(UIViewController*)nextResponder;
}
}
returnnil;
}
@end

使用示例:
-(void)buttonPress{
ElectronFrom*electronForm=[[ElectronFromalloc]init];
[[selfviewController].navigationControllerpushViewController:electronFormanimated:YES];
[electronFormrelease];
}


ViewCode


  


                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: