又到了练习日 感觉最近学的东西挺多,今天我们就一起复习一下前几天学到东西:
导航控制器有两种方式生成,一种是通过拖拽,另一种是通过代码添加:
纯代码创建 :UINavigationController *nc = [[UINavigationControll alloc]initWithRootViewController:vc];
若从第二页开始使用导航控制器,首页需要先用:[self presentViewController:nc animated:YES completion:nil];方法跳转到导航控制器,再从导航控制器里跳转到其他页面,(导航控制器里是一个数组,里面存储了各个页面,可以通过调用数组中存储的页面来完成页面跳转)若要跳回首页用:[self dismissViewControllerAnimated:YES completion:nil];方法进行跳回不在控制器里的页面;
导航控制器控制的所有页面之间的跳转用以下两种方法,区别于普通页面的跳转,并且没用通过导航控制器跳转的页面都没有销毁,而通过导航控制器的跳转后会自己销毁;
跳转方法:[self.navigationController pushViewController:vc animated:YES];
返回方法:[self.navigationController popToViewController:secondVC animated:YES];
调用导航控制器里的数组:UIViewController *secondVC = self.navigationController.viewControllers[1];
创建Item: UIBarButtonItem *backItem = [[UIBarButtonItem alloc]initWithTitle:@"back"style:UIBarButtonItemStyleDone target:self action:@selector(backAction)];(可以写点击事件)
创建并初始化
创建UITextView的文件,并在.h文件中写入如下代码:
#import
@interface TextViewController : UIViewController
{
UITextView *textView;
}
@property (nonatomic, retain) UITextView *textView;
@end
在.m文件中初始化这个textview,写入代码如下:
self.textView = [[[UITextView alloc] initWithFrame:self.view.frame]autorelease]; //初始化大小并自动释放
self.textView.textColor = [UIColor blackColor];//设置textview里面的字体颜色
self.textView.font = [UIFont fontWithName:@"Arial" size:18.0];//设置字体名字和字体大小
self.textView.delegate = self;//设置它的委托方法
self.textView.backgroundColor = [UIColor whiteColor];//设置它的背景颜色
self.textView.text = @"Now is the time for all good developers tocome to serve their country.\n\nNow is the time for all good developers to cometo serve their country.";//设置它显示的内容
self.textView.returnKeyType = UIReturnKeyDefault;//返回键的类型
self.textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
self.textView.scrollEnabled = YES;//是否可以拖动
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;//自适应高度
[self.view addSubview: self.textView];//加入到整个页面中
2. UITextView退出键盘的几种方式
因为你点击UITextView会出现键盘,如果你退出键盘,有如下几种方式:
(1)如果你程序是有导航条的,可以在导航条上面加多一个Done的按钮,用来退出键盘,当然要先实UITextViewDelegate。
代码如下:
- (void)textViewDidBeginEditing:(UITextView *)textView {
UIBarButtonItem *done = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(leaveEditMode)] autorelease];
self.navigationItem.rightBarButtonItem = done;
}
- (void)textViewDidEndEditing:(UITextView *)textView {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)leaveEditMode {
[self.textView resignFirstResponder];
}
(2)如果你的textview里不用回车键,可以把回车键当做退出键盘的响应键。
代码如下:
#pragma mark - UITextView Delegate Methods
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
这样无论你是使用电脑键盘上的回车键还是使用弹出键盘里的return键都可以达到退出键盘的效果。
(3)还有你也可以自定义其他加载键盘上面用来退出,比如在弹出的键盘上面加一个view来放置退出键盘的Done按钮。
代码如下:
UIToolbar * topView = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
[topView setBarStyle:UIBarStyleBlack];
UIBarButtonItem * helloButton = [[UIBarButtonItem alloc]initWithTitle:@"Hello" style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
NSArray * buttonsArray = [NSArray arrayWithObjects:helloButton,btnSpace,doneButton,nil];
[doneButton release];
[btnSpace release];
[helloButton release];
[topView setItems:buttonsArray];
[tvTextView setInputAccessoryView:topView];
-(IBAction)dismissKeyBoard
{
[tvTextView resignFirstResponder];
}
(4)设置UITextView圆角问题
做法是在#import QuartzCore/QuartzCore.h 后,便能調用[textView.layer setCornerRadius:10]; 來把UITextView 设定圓角
(5)UITextView根据文本大小自适应高度
通过实现文本字数来确定高度,如下:
NSString * desc = @"Description it is a test font, and don't become angry for which i use to do here.Now here is a very nice party from american or not!";
CGSize size = [desc sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(240, 2000) lineBreakMode:UILineBreakModeWordWrap];
只有UILabel需要定义的numberoflines为0,即不做行数的限制。如下:
[label setNumberOfLines:0];
[label setFrame:CGRectMake(40, 135, 240, size.height+10)];
[label setText:desc];
(6)UITextView自定选择文字后的菜单
在ViewDidLoad中加入:
UIMenuItem *menuItem = [[UIMenuItem alloc]initWithTitle:@"分享到新浪微博" action:@selector(changeColor:)];
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObject:menuItem]];
[menuItem release];
当然上面那个@selector里面的changeColor方法还是自己写吧,也就是说点击了我们自定义的菜单项后会触发的方法。
然后还得在代码里加上一个方法:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if(action ==@selector(changeColor:))
{
if(textView.selectedRange.length>0)
return YES;
}
return NO;
}
今天的UITextView就讲到这里,主要讲了UITextView的初始化和开发中会遇到的一些问题和自定义等问题。谢谢大家支持哈。
posted @ 2012-01-31 19:09 SuperHappy 阅读(66) 评论(0) 编辑
UILabel的常见用法
- (void)loadView { [super loadView]; //1.UILable的大小自适应实例: UILabel *myLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 2, 2)];//设定位置与大小 [myLabel setFont:[UIFont fontWithName:@"Helvetica" size:20.0]];//格式 [myLabel setNumberOfLines:0];//行数,只有设为0才可以自适应 [myLabel setBackgroundColor:[UIColor clearColor]];//背景色 myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色 myLabel.shadowOffset = CGSizeMake(1.0,1.0);//阴影大小 NSString *text = @"abcdefghigklmnopqrstuvwxyz"; UIFont *font = [UIFont fontWithName:@"Helvetica" size:20.0]; CGSize size = [text sizeWithFont:font constrainedToSize:CGSizeMake(175.0f, 2000.0f) lineBreakMode:UILineBreakModeWordWrap]; CGRect rect=myLabel.frame; rect.size=size; [myLabel setFrame:rect]; [myLabel setText:text]; myLabel.shadowColor = [UIColor darkGrayColor];//阴影颜色 myLabel.shadowOffset = CGSizeMake(2.0,2.0);//阴影大小 [self.view addSubview:myLabel]; [myLabel release]; //2.UILable的基本用法获取自馒头MAN百度空间,感谢馒头MAN //空间地址:http://hi.baidu.com/bunsman/blog/item/95777b0ebacf05fe36d122e2.html
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 40.0, 200.0, 30.0)]; UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 80.0, 200.0, 50.0)]; UILabel *label3 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 140.0, 200.0, 50.0)]; UILabel *label4 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 200.0, 200.0, 50.0)]; UILabel *label5 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 260.0, 200.0, 50.0)]; UILabel *label6 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 320.0, 200.0, 50.0)]; UILabel *label7 = [[UILabel alloc]initWithFrame:CGRectMake(50.0, 380.0, 200.0, 50.0)]; //设置显示文字 label1.text = @"label1"; label2.text = @"label2"; label3.text = @"label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--label3--11个"; label4.text =@"label4--label4--label4--label4--4个"; label5.text = @"label5--label5--label5--label5--label5--label5--6个"; label6.text = @"label6"; label7.text = @"label7"; //设置字体:粗体,正常的是 SystemFontOfSize label1.font = [UIFont boldSystemFontOfSize:20]; //设置文字颜色label1.textColor = [UIColor orangeColor]; label2.textColor = [UIColor purpleColor]; //设置背景颜色 label1.backgroundColor = [UIColor clearColor]; label2.backgroundColor = [UIColor colorWithRed:0.5f green:30/255.0f blue:0.3f alpha:0.5f]; //设置文字位置 label1.textAlignment = UITextAlignmentRight; label2.textAlignment = UITextAlignmentCenter; //设置字体大小适应label宽度 label4.adjustsFontSizeToFitWidth = YES; //设置label的行数 label5.numberOfLines =2; //设置高亮 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; [self.view addSubview:label1]; [self.view addSubview:label2]; [self.view addSubview:label3]; [self.view addSubview:label4]; [self.view addSubview:label5]; [self.view addSubview:label6]; [self.view addSubview:label7]; [label1 release]; [label2 release]; [label3 release]; [label4 release]; [label5 release]; [label6 release]; [label7 release]; }
posted @ 2012-01-31 18:50 SuperHappy 阅读(212) 评论(0) 编辑
窗口,视图,视图控制器和UIKit基础
1、窗口:UIWindow
iPhone的规则是一个窗口,多个视图,窗口是你在app显示出来你看到的最底层,他是固定不变的,基本上可以不怎么理会,但要知道每层是怎样的架构。
2、视图:UIView
UIView是用户构建界面的基础,所有的控件都是在这个页面上画出来的,你可以把它当成是一个画布,你可以通过UIView增加控件,并利用控件和用户进行交互和传递数据。
窗口和视图是最基本的类,创建任何类型的用户界面都要用到。窗口表示屏幕上的一个几何区域,而视图类则用其自身的功能画出不同的控件,如导航栏,按钮都是附着视图类之上的,而一个视图则链接到一个窗口。
3、视图控制器:UIViewController
视图控制器UIViewController,你可以把他当成是对你要用到视图UIView进行管理和控制,你可以在这个UIViewController控制你要显示的是哪个具体的UIView。另外,视图控制器还增添了额外的功能,比如内建的旋转屏幕,转场动画以及对触摸等事件的支持。
4、 UIKit简介
(1)显示数据的视图
UITextView:将文本段落呈现给用户,并允许用户使用键盘输入自己的文本。
UILabel:实现短的只读文本,可以通过设置视图属性为标签选择颜色,字体和字号等。
UIImageView:可以通过UIImage加载图片赋给UIImageView,加载后你可以指定显示的位置和大小。
UIWebView:可以提供显示HTML.PDF等其他高级的Web内容。包括xls,word等文档等。
MKMapView:可以通过MKMapView向应用嵌入地图。很热门的LBS应用就是基于这个来做的。还可以结合MKAnnotationView和MKPinAnnotationView类自定义注释信息注释地图。
UIScrollView:一般用来呈现比正常的程序窗口大的一些内容。可以通过水平和竖直滚动来查看全部的内容,并且支持缩放功能。
(2) 做出选择的视图
UIAlertView:通过警告视图让用户选择或者向用户显示文本。
UIActionSheet:类似UIAlertView,但当选项比较多的时候可以操作表单,它提供从屏幕底部向上滚动的菜单。
(3)其他
UIBuuton:主要是我们平常触摸的按钮,触发时可以调用我们想要执行的方法。
UISegmentControl:选择按钮,可以设置多个选择项,触发相应的项调用不同的方法。
UISwitch:开关按钮,可以选择开或者关。
UISlideer:滑动按钮,常用在控制音量等。
UITextField:显示文本段,显示所给的文本。
UITableView:表格视图,可以定义你要的表格视图,表格头和表格行都可以自定义,自定义的一个表格如下图:
UIPickerView:选择条,一般用于日期的选择。
UISearchBar:搜索条,一般用于查找的功能。
UIToolBar:工具栏:一般用于主页面的框架。
UIActivityIndicatorView:进度条,一般用于显示下载进度。
UIProgressView:进度条,一般用于显示下载的进度条。
今天就简单的介绍了一下IOS应用开发常用的一些控件,还有基础的UIView,UIWindow和UIViewControl之间的关系,这些是基础,直接影响到以后开发的能力,接下来我将分开讲这些控件。
对于Button的圆角显示,利用layer实现
示例:
对button1的边框进行设置
#import //不要忘记添加库文件
CALayer *layer1=[button1 layer];
//是否设置边框以及是否可见
[layer1 setMasksToBounds:YES];
//设置边框圆角的弧度
[layer1 setCornerRadius:15.0];
//设置边框线的宽
[layer1 setBorderWidth:2.0];
//设置边框线的颜色
[layer1 setBorderColor:[[UIColorgrayColor] CGColor]];
posted @ 2012-01-31 15:52 SuperHappy 阅读(48) 评论(0) 编辑
NSNotificationCenter
新建一个继承于UIViewControll的类,并在.m中添加如下代码
-(void)doSomeThing:(NSNotification *)aNote { NSDictionary *dict = [aNote object]; NSLog(@"%@",dict); } - (void)loadView { [super loadView]; NSString *myString = @"some Value"; NSDictionary *myDict = [[NSDictionary alloc]initWithObjectsAndKeys:myString,@"first", nil]; [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomeThing:) name:@"notification" object:nil]; [[NSNotificationCenter defaultCenter]postNotificationName:@"notification" object:myDict]; }
设置通知:
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(doSomeThing:) name:@"notification" object:nil];
addObserver 这个是观察者,就是说 在什么地方接收通知;
selector 这个是收到通知后,调用何种方法;
name: 这个是通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object: 对象,当设定为nil时,默认为所有名称为以上消息名称的(notification)都接收不管发送者是谁。
发送通知:
[[NSNotificationCenter defaultCenter]postNotificationName:@"notification" object:myDict];
postNotificationName:通知的名字,也是通知的唯一标示,编译器就通过这个找到通知的。
object:对象,当设定为nil时,默认为所有名称为以上消息名称的(notification)都发送。
小例子仅仅是在一个类下的通知,实际来说通知更多的应用于不同的类之间传递信息。
posted @ 2012-01-31 15:38 SuperHappy 阅读(57) 评论(0) 编辑
简单的大头针地图标示练习(无xib)
用4.2创建的空模班,添加的UIViewController类,取名为mapView.(按正规命名方式应该为MapView,大家见谅)。
以下为添加的代码
mapView.h
[objc] view plain
copy
?
- #import
- #import
- #import
- #import
- @interface
mapView : UIViewController
- {
- CLLocationManager
*locManager;
- MKMapView
*map;
- CLLocation
*bestLocation;
- }
- @property
(
retain
)
CLLocationManager
*locManager;
- @property
(
retain
)
CLLocation
*bestLocation;
- @end
mapView.m
//
//
mapView.m
//
Map
//
//
Created by SuperHappy on 12-1-31.
//
Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import
"mapView.h"#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]@implementation mapView@synthesize locManager;@synthesize bestLocation; - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Location manager error :%@",[error description]); } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { if (!self.bestLocation) { self.bestLocation = newLocation; } else if(newLocation.horizontalAccuracy < bestLocation.horizontalAccuracy) self.bestLocation = newLocation; map.region = MKCoordinateRegionMake(self.bestLocation.coordinate, MKCoordinateSpanMake(0.1f, 0.1f)); map.showsUserLocation = YES; map.zoomEnabled = NO; } - (void)findme { // disable right button self.navigationItem.rightBarButtonItem = nil; // Search for the best locationself.bestLocation = nil; self.locManager.delegate = self; [self.locManager startUpdatingLocation];} - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if(self) { // Custom initialization } return self; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.}#pragma mark - View lifecycle// Implement loadView to create a view hierarchy programmatically, without using a nib.- (void)loadView { [super loadView]; map = [[MKMapView alloc]initWithFrame:CGRectMake(10, 10, 300, 300)]; [self.view addSubview:map]; [map release]; self.navigationController.navigationBar.tintColor = [UIColor redColor]; self.locManager = [[[CLLocationManager alloc] init] autorelease]; if(!self.locManager.locationServicesEnabled) { NSLog(@"User has opted out of location services"); return; } else { // User generally allows location calls self.locManager.desiredAccuracy = kCLLocationAccuracyBest; self.navigationItem.rightBarButtonItem = BARBUTTON(@"Find Me", @selector(findme)); } }- (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil;} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { //Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); }@end
posted @ 2012-01-31 14:13 SuperHappy 阅读(26) 评论(0) 编辑
Object-c 总结之NSDate
NSDate的常见用法总结
// 当前时间创建NSDate
NSDate *myDate = [NSDate date];
NSLog(@"myDate = %@",myDate);
//从现在开始的24小时
NSTimeInterval secondsPerDay = 24*60*60;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSLog(@"myDate = %@",tomorrow);
//根据已有日期创建日期
NSTimeInterval secondsPerDay1 = 24*60*60;
NSDate *now = [NSDate date];
NSDate *yesterDay = [now addTimeInterval:-secondsPerDay1];
NSLog(@"yesterDay = %@",yesterDay);
//比较日期
BOOL sameDate = [now isEqualToDate:yesterDay];
NSLog(@"sameDate = %lu",sameDate);
//获取较早的日期
NSDate *earlierDate = [yesterDay earlierDate:now];
NSLog(@"earlierDate = %@",earlierDate);
//较晚的日期
NSDate *laterDate = [yesterDay laterDate:now];
NSLog(@"laterDate = %@",laterDate);
//两个日期之间相隔多少秒
NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];
NSLog(@"secondsBetweenDates= %lf",secondsBetweenDates);
//通过NSCALENDAR类来创建日期
NSDateComponents *comp = [[NSDateComponentsalloc]init];
[comp setMonth:06];
[comp setDay:01];
[comp setYear:2001];
NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *myDate1 = [myCal dateFromComponents:comp];
NSLog(@"myDate1 = %@",myDate1);
//从已有日期获取日期
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *comp1 = [myCal components:units fromDate:now];
NSInteger month = [comp1 month];
NSInteger year = [comp1 year];
NSInteger day = [comp1 day];
//NSDateFormatter实现日期的输出
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
//或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString *string = [formatter stringFromDate:now];
NSLog(@"string = %@",string);
NSLog(@"formater = %@",formatter);
posted @ 2012-01-31 13:57 SuperHappy 阅读(117) 评论(0) 编辑
Object-c 总结之NSDictionary
NSDictionary的常见用法总结
NSArray *array1 = [NSArray arrayWithObjects:@"iphone",@"ipod",nil];
NSArray *array2 = [NSArray arrayWithObjects:@"mac",@"imac",@"mac pro",nil];
//类方法初始化自动释放
NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:array1,@"mobile",array2,@"computers",nil];//注意用nil结束
NSLog(@"myDictionary = %@",myDictionary);
int dictSize = [myDictionary count];
//访问字典中的值
NSArray *mobile = [myDictionary objectForKey:@"mobile"];
//从一个对象获取键
NSArray *keys = [myDictionary allKeysForObject:array1];
//获取字典中所有值得一个数组
NSArray *values = [myDictionary allValues];
//快速枚举
for(id key in myDictionary)
{
NSLog(@"key: %@,value: %@",key,[myDictionary objectForKey:key]);
}
//如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArray或NSDictionary)可以保存到文件中
NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"dict.txt"];
BOOL success = [myDictionary writeToFile:filePath atomically:YES];
//用文件填充
NSDictionary *myDict2 =[NSDictionary dictionaryWithContentsOfFile:filePath];
//可变字典
NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:array1,@"mobile",array2,@"computer", nil];
NSString *string4 = @"stringTV";
//修改对象
[dictMutable setObject:string4 forKey:@"media"];
//删除对象
[dictMutable removeObjectForKey:@"mobile"];
//删除多个对象
NSArray *keyArray =[NSArray arrayWithObjects:@"mobile",@"computer", nil];
[dictMutable removeObjectForKey:keyArray];
//删除所有对象
[dictMutable removeAllObjects];
posted @ 2012-01-31 13:54 SuperHappy 阅读(217) 评论(0) 编辑
Object-c 总结之NSArray
NSArray的基本应用总结
NSString *string1 = @"two";
NSString *string2 = @"one";
//创建数组
NSArray *array1 = [NSArray arrayWithObjects:string1,string2, nil];
NSArray *array2 = [NSArray arrayWithArray:array1];
//创建数组只包含已有数组的一部分
NSRange range = NSMakeRange(0, 2);
NSArray *subArray = [array1 subarrayWithRange:range];
//array的长度
int arrayLength = [array1 count];
//访问数组中特定位置的一个对象
NSString *string = [array1 objectAtIndex:0];
//是否包含指定对象
BOOL isInArray = [array1 containsObject:string1];
//对象在数组中的位置
int index = [array1 indexOfObject:string1];
//遍历一个数组中的值
for(NSString *obj in array1)
{
NSLog(@"%@",obj);
}
//反向遍历一个数组的值
for(NSString *objfan in [array1 reverseObjectEnumerator])
{
NSLog(@"%@",objfan);
}
//对字符串的重新排序(有点问题)
//[array1 sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
//NSLog(@"array1 = %@",array1);
//创建动态数组
NSMutableArray *myarray = [[NSMutableArray alloc]initWithObjects:string1,string2, nil];
NSString *string3 = @"Three";
//向动态数组中添加
[myarray addObject:string3];
[myarray addObject:string2];
[myarray insertObject:string3 atIndex:0];
[myarray insertObject:string2 atIndex:1];
//替换
[myarray replaceObjectAtIndex:1withObject:string3];
//删除
[myarray removeObject:string3];
//删除特定位置对象
[myarray removeObjectAtIndex:0];
//删除几个对象
[myarray removeObjectsInRange:range];
//删除所有对象
[myarray removeAllObjects];
posted @ 2012-01-31 13:52 SuperHappy 阅读(206) 评论(0) 编辑
Object-c 总结之NSString
参照《iphone SDK 3开发快速上手》编写,特此声明
//创建一个NSString
NSString *myString1 = @"some string";
NSLog(@"%@",myString1);
//格式化创建
NSString *myString2 = [NSString stringWithFormat:@"%@",myString1];
NSLog(@"%@",myString2);
//返回数字的值
NSString *myString3 = @"1234abc";
double doubleString = [myString3 floatValue];//获得123
NSLog(@"%f",doubleString);
int intString = [myString3 intValue];//获得123
NSLog(@"%d",intString);
NSUInteger stringLenth = [myString3 length];
NSLog(@"%lu",stringLenth);
//比较两个字符串,若相同返回TURE
BOOL areEqual =
[myString1 isEqualToString:myString2];
NSLog(@"areEqual = %d",areEqual);
//区分大小写的比较
BOOL areEqual1 = ([myString1 caseInsensitiveCompare:myString2] == NSOrderedSame);
NSLog(@"areEqual1 = %d",areEqual1);
//转换大小写
NSString *myString4 = @"asdfg";
NSString *upper = [myString4 uppercaseString];
NSString *lower = [upper lowercaseString];
NSLog(@"upper = %@",upper);
NSLog(@"lower = %@",lower);
//截去字符串(以空格为例)
NSString *myString5 = @" one two three ";
NSString *trimmed1 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceAndNewlineCharacterSet]];
//whitespaceAndNewlineCharacterSet去除前后的空格和换行符
NSString *trimmed2 = [myString5 stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
//whitespaceCharacterSet 去除前后的空格,实际效果来看只实现了去除首字母前面的空格
NSLog(@"myString5 = %@",myString5);
NSLog(@"trimmed1 = %@",trimmed1);
NSLog(@"trimmed2 = %@",trimmed2);
//predicate 断言断定,使基于,下面的方法实现删除空格
NSPredicate *noE = [NSPredicatepredicateWithFormat:@"SELF!=''"];
NSArray *part = [myString5 componentsSeparatedByCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]];
NSArray *file = [part filteredArrayUsingPredicate:noE];
NSString *trimmed3 = [file componentsJoinedByString:@""];
NSLog(@"trimmed3 = %@",trimmed3);
//有已知字符串创建子字符串
NSString * number = @"abx cdefghi gklmn";
//substringToIndex获取字符串的前三个
//substringFromIndex从第三个开始截取到最后
NSString *myString6 = [number substringToIndex:3];
NSLog(@"myString6 = %@",myString6);
NSRange range = NSMakeRange(4, 5);
//用边界截取起始位置为第四个,长度为5个长度
NSString *aString = [number substringWithRange:range];
NSLog(@"aString = %@",aString);
//创建成数组
NSArray *arr = [number componentsSeparatedByString:@" "];
NSLog(@"arr = %@",arr);
//替换子字符串
NSString *myString7 = [number stringByReplacingOccurrencesOfString:@"efg"withString:@"aaa"];
NSLog(@"myString7 = %@",myString7);
//查找子字符串,这段代码返回(4,3);
NSRange found = [number rangeOfString:@"cde"];
NSLog(@"found.location = %lu",found.location);
NSLog(@"%lu",found.length);
//判读那是否包含
BOOL found1 = ([number rangeOfString:@"cde"].location !=NSNotFound);
NSLog(@"found1 = %lu",found1);
//组合字符串
NSString *myString9 = [myString1 stringByAppendingString:myString7];
NSLog(@"myString9 = %@,myString1 = %@,myString7 = %@",myString9,myString1,myString7);
NSString *myString10 = [myString1 stringByAppendingFormat:myString7];
NSLog(@"myString10 = %@,myString1 = %@,myString7 = %@",myString10,myString1,myString7);
//将文件内容写入到字符串中
NSString *fileContents = [NSString stringWithContentsOfFile:@"123.text"];
//获取文件扩展名
NSString *filename = @"11111.txt";
NSString *fileExtension = [filename pathExtension];
NSLog(@"fileExtension = %@",fileExtension);
//将URL内容写入字符串,有点问题
NSURL *url = [NSURLURLWithString:@"http://www.baidu.com
"];
NSLog(@"url = %@",url);
NSString *pageContents = [NSString stringWithContentsOfURL:url];
//NSLog(@"pageContents = %@",pageContents);
posted @ 2012-01-31 13:50 SuperHappy 阅读(223) 评论(0) 编辑
iphone内存管理的一些整理
内存管理个人总结
无论编写任何程序,都需要确保能够有效和高效地管理资源。程序内存就是这些资源中的一种。在Objective-C程序中,必须确保所创建的对象,在不再需要它们的时候被销毁。
注意:iOS不提供垃圾回收机制。
一般规则:
只能释放或自动释放自己所拥有的对象
一般以alloc ,new, copy创建的对象都具有所有权,或者如果向一个对象发送了一条retain消息,也会获得该对象的所有权。
此时需要用release进行释放或者调用自动释放池autorelease;自动释放池的调用是系统来完成的。
需要将接收到的对象存储为某个实例变量的属性,您必须保留或复制该对象。
释放实例:
UIView *view 1= [[UIView alloc]init];
Self.view = view1;
[view1 release];
不需要释放的例子:
UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
由于调用的类方法没有使用alloc,new,copy等所以它自动释放,不需要手动释放。
调用自动释放池的例子:
-(NSArray *)rearray
{
NSArray *array = [[NSArray alloc]initWithObjects:@”1”,@”2”,nil];
return [array autorelease];
}
这个方法既没有破坏内存规则,又避免了接收值的使用者不知道何时销毁对象的现象。如果直接return array会造成内存泄露,如果先释放在返回的话会返回无效对象([array release];return array;)这也是错误的。当然如果直接调用类方法就可以这样做:
-(NSArray *)rearray
{
NSArray *array = [NSArray arrayWithObjects:@”1”,@”2”,nil];
return array;
}
在 Objective-C 程序中,对象会被创建和销毁。为了确保应用程序不会使用不必要的内存,对象应该在不需要它们的时候被销毁。当然,在需要对象时保证它们不被销毁也很重要。
任何对象都可能拥有一个或多个所有者。只要一个对象至少还拥有一个所有者,它就会继续存在。如果一个对象没有所有者,则运行时系统会自动销毁它。
可以使用retain来获得一个对象的所有权。
保留计数(retainCount)
在调用retain方法后通过引用计数—通常被称为“保留计数”—实现的。每个对象都有一个保留计数。
创建一个对象时,该对象的保留计数为1。
向一个对象发送retain消息时,该对象的保留计数加1。
向一个对象发送release消息时,该对象的保留计数减1。
向一个对象发送autorelease消息时,该对象的保留计数会在将来的某个阶段减1。
如果一个对象的保留计数被减为0,该对象就会被回收.(直接调用dealloc进行释放)。
当显式地查询对象的保留计数是多少。由于添加了保护机制,当保留计数被减为0时,控制台输出仍然为1因此会造成误导。
共享对象的有效性
Cocoa的所有权策略规定,被接收的对象通常应该在整个调用方法的作用域内保持有效。此外,还可以返回从当前作用域接收到的对象,而不必担心它被释放。对象的getter方法返回一个缓存的实例变量或者一个计算值,这对您的应用程序来说无关紧要。重要的是,对象会在您需要它的这段期间保持有效。
这一规则偶尔也有一些例外情况,主要可以总结为以下两类。
当对象从一个基本的集合类中被删除的时候。
heisenObject = [array objectAtIndex:n];
[array removeObjectAtIndex:n];
// heisenObject could now be invalid.
当对象从一个基本的集合类中被删除时,它会收到一条release(不是autorelease)消息。如果该集合是这个被删除对象的唯一所有者,则被删除的对象(例子中的heisenObject)将被立即回收。
当一个“父对象”被回收的时候。
学习ios 重要还是要理清楚思路 在做或者看老师代码的时候 自己多想想为什么 不要自己看着就抄 另外还是要推荐一下 蓝懿IOS这个培训机构 和刘国斌老师刘国斌老师还是很有名气的,听朋友说刘老师成立了蓝懿iOS,,老师讲课方式很独特,能够尽量让每个人都能弄明白,有的比较难懂的地方,如果有的地方还是不懂得话,老师会换个其它方法再讲解,这对于我们这些学习iOS的同学是非常好的,多种方式的讲解会理解得更全面,这个必须得给个赞,嘻嘻,还有就是这里的学习环境很好,很安静,可以很安心的学习,安静的环境是学习的基础,小班讲课,每个班20几个学生,学习氛围非常好,每天都学到9点多才离开教室,练习的时间很充裕,而且如果在练习的过程中有什么困难,随时可以向老师求助,不像其它机构,通过视频教学,有的甚至学完之后都看不到讲师本人,问点问题都不方便,这就是蓝懿与其它机构的区别,相信在刘国斌老师的细心指导下,每个蓝懿学员都能找到满意的工作,加油!
写博客第二十八天;
QQ:565803433