iphone开发的一些基础概念及代码

1、使用代码添加插座变:

IBOutlet UITextField *nameTextField;

2、使用代码添加动作:

-(IBAction)btnClick:(id)sender;

3、使用UIAlertView:

UIAlertView *alert = [[UIAlertView alloc]

initWithTitle:@"Hello"

message:@"This is an alert view"

delegate:self

cancelButtonTitle:@"OK"

otherButtonTitle:nil];

4、使用UIActionSheet:

UIActionSheet *action = 

[[UIActionSheet alloc]

initWithTitle:@"Title of Action Sheet"

delegate:self

cancelButtonTitle:@"OK"

destructiveButtonTitle:@"Delegate Message"

otherButtonTitle:@"Option1",@"Option2",nil];

5、为UIPageControl连接事件:

[pageControl addTarget:self

action:@selector(pageTurning:)

forControlEvents:UIControlEventValueChanged];

6、使用UIImageView:

[imageView1 setImage:

[UIImage imageNamed:@"Apple.jpeg"]];

7、为UISegmentedControl连接事件:

[segmentedControl addTarget:self

action:@selector(segmentChanged:)

forControlEvents:UIControlEventValueChanged];

8、使用UIWebView:

NSURL *url = [NSURL URLWithString:@"http://www.apple.com"];

NSURLRequest *req = [NSURequest requestWithURL:url];

[webView loadRequest:req];

9、为切换添加动画:

[UIView beginAnimations:@"flipping view"

context:nil];

[UIView setAnimationDuration:1];

[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

[UIView setAnimationTransition:UIViewAnimationTransitionCurlDown

forView:self.viewcache:YES];

[self.view addSubview:secondViewController.view];

[UIView commitAnimations];

10、处理键盘的Return键:

-(BOOL)textFieldShouldReturn:(UITextField *)textFieldView{

if(textFiedView == textField){

[textField = resignFirstResPonder ];

}

return NO;

}

11、编写代码创建Label视图:

label = [[UILabel alloc]initWithFrame:frame];

label.textAlignment = UITextAlignmentCenter;

label.font = [UIFontWithName:@"Verdana" 

size:20];

label.text = @"This is a lable";

12、编写代码创建Button视图:

frame  = CGRectMake(10,250,300,50);

button = [[UIButton buttonWithType:UIButtonTypeRoundRect] initWithFrame:frame];

[button setTitle:@"OK" forState:UIControlStateNormal];

button.backgroudColor = [UIColorclearColor];

13、连接事件与事件处理程序:

button addTarget:self

action:@selector(buttonClick:)

forControlEvents:UIControlEventTouchUpInside];

14、切换视图:

myViewController = [[MyViewController alloc]initWithNibName:@"MyView"

bundle:nil];

[self.view addSubview:myViewController.view];

15、在基于导航的应用程序中导航到另一个视图控制器:

[self.navigationController

 pushViewController:self.detailsViewController

animated:YES];

16、在表视图中使用属性列表:

使用如下代码定位属性列表

NSString *path = [[NSBundle mainBundle]

pathForResource:@"Movies"

ofType:@"plist"];

然后使用NSDictionary与NSArray对象检索存储在属性列表中的键/值对

17、加载首选项设置的值:

NSUserDefaults *defaults = [NSUserDefauts standardUserDefaults];

NSString *strLoginname = [defaults objectForKey:@"login_name"];

18、保存首选项设置的值:

NSUserDefaults *defaults = [NSUserDefauts standardUserDefaults];

[defaults setObject:loginName.text

forKey:@"login_name"];

19、获取Docunments目录的路径:

NSArray  *path = NSSearchPathForDirectoriesInDomains(

NSDocumentDirectory,

NSUserDomainMask,YES);

NSString *documentsDir = [path objectAtIndex:0];

20、获取tmp目录的路径:

-(NSString *)tempPath{

return NSTemporaryDirectory();

}

21、检查文件是否存在:

if([NSFileManager defaultManager]fileExistsAtPath:filePath]){

}

22、创建NSDictionary对象的可变副本:

NSDictionary *dict = [[NSDictionary alloc]

initWithContentsOfFile:pListPath];

NSMutableDictionary *copyOfDict = [dict mutableCopy];

23、使用NSTimer对象创建定时器:

创建一个每隔半秒调用一次onTimer方法的定时器对象

Timer = [NSTimer scheduledTimerWithTimeInterval:0.5

target:self

selector:@selector(onTimer)

userInfo:nil

repeats:YES];

24、停止NSTimer对象

[timer invalidate];

25、创建连续变化的视觉效果:

[UIView beginAnimations:@"some_text" context:nil];

[UIView commitAnimations];

26、在自己的应用程序中发送邮件:

NSString *emailString = 

@"mailto:?to=USER@EMAIL.COM&subject=SUBJECT&

body=BODY OF EMAIL";

[[UIApplication sharedApplication]

openURL:[NSURL

URLWithString:emailString]];

27、调用Safari:

 [[UIApplication sharedApplication]

openURL:[NSURL

URLWithString:

@"http://www.apple.com"]];

28、调用Phone:

 [[UIApplication sharedApplication]

openURL:[NSURL

URLWithString:@“tel:1234567890”]];

29、调用SMS

 [[UIApplication sharedApplication]

openURL:[NSURL

URLWithString:@“sms:96924065”]];

posted @ 2011-12-19 16:40  Earloye  阅读(372)  评论(0编辑  收藏  举报