ios学习第1章:实现控制器和视图-基础控件
来源:互联网 发布:ubuntu安装搜狗 编辑:程序博客网 时间:2024/06/05 16:52
UiAlert
typedefNS_ENUM(NSInteger,UIAlertViewStyle) {
UIAlertViewStyleDefault=0,
UIAlertViewStyleSecureTextInput,
UIAlertViewStylePlainTextInput,
UIAlertViewStyleLoginAndPasswordInput
};
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Credit Card Number"
message:@"Please enter your credit card number:" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
[alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
/* Display a numerical keypad for this text field */
UITextField *textField = [alertView textFieldAtIndex:0]; textField.keyboardType = UIKeyboardTypeNumberPad;
-(void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:[self yesButtonTitle]]){NSLog(@"User pressed the Yes button.");
}
else if([buttonTitle isEqualToString:[self noButtonTitle]]){
NSLog(@"User pressed the No button."); }
}
UISwitch
self.mainSwitch = [[UISwitch alloc] initWithFrame: CGRectMake(100, 100, 0, 0)];
self.mainSwitch.tintColor = [UIColor redColor];/* Adjust the on-mode tint color */
/* Also change the knob's tint color */
/*
*/self.mainSwitch.onImage = [UIImage imageNamed:@"On"]; self.mainSwitch.offImage = [UIImage imageNamed:@"Off"];self.mainSwitch.onTintColor = [UIColor brownColor];
self.mainSwitch.thumbTintColor = [UIColor greenColor];
[self.view addSubview:self.mainSwitch];
[self.mainSwitch addTarget:selfaction:@selector(switchIsChanged:)
forControlEvents:UIControlEventValueChanged];
//事件
-(void)switchIsChanged:(UISwitch*)paramSender{
NSLog(@"Sender is = %@", paramSender);
if([paramSender isOn]){
NSLog(@"The switch is turned on.");
}else{
NSLog(@"The switch is turned off.");
}}
UIPickerView
elf.myPicker = [[UIPickerView alloc] init]; self.myPicker.dataSource = self; self.myPicker.center = self.view.center; [self.view addSubview:self.myPicker];
//返回列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
//返回行数
-(NSInteger)pickerView:(UIPickerView*)pickerViewnumberOfRowsInComponent:(NSInteger)component;
//每项标题
self.myPicker.delegate=self;
-(NSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)row
forComponent:(NSInteger)component{
if([pickerView isEqual:self.myPicker]){
return[NSString stringWithFormat:@"Row %ld", (long)row+1];
}
returnnil;}
//ios6开始 突出显⽰示当前选择
self.myPicker.showsSelectionIndicator = YES;
selectedRowInComponent:
//修改选择项内容
reloadComponent:
UIDatePicker
typedefNS_ENUM(NSInteger,UIDatePickerMode) {
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
};
(void)datePickerDateChanged:(UIDatePicker*)paramDatePicker{
if([paramDatePicker isEqual:self.myDatePicker]){NSLog(@"Selected date = %@", paramDatePicker.date);
}}
[self.myDatePicker addTarget:selfaction:@selector(datePickerDateChanged:)
forControlEvents:UIControlEventValueChanged];
self.myDatePicker.datePickerMode = UIDatePickerModeDate;
NSTimeInterval oneYearTime = 365 * 24 * 60 * 60; NSDate *todayDate = [NSDate date];
NSDate *oneYearFromToday = [todayDate dateByAddingTimeInterval:oneYearTime];
NSDate *twoYearsFromToday = [todayDate dateByAddingTimeInterval:2 * oneYearTime];
self.myDatePicker.minimumDate = oneYearFromToday;
self.myDatePicker.maximumDate = twoYearsFromToday;
NSDate *currentDate = self.myDatePicker.date;
如果您想使⽤用⽇日期选择器作为⼀一个倒数计时器,你必须设置你的⽇日期选择器模式UIDatePickerModeCountDownTimer并使⽤用countDownDuration属性
⽇日期选取器来指定默认的倒计时时间。举例来说,如果你想提出⼀一个倒计时选择器给⽤用户,并设置默认的倒计时时间到两分钟,这样写代码:
self.myDatePicker.datePickerMode=UIDatePickerModeCountDownTimer;
[self.view addSubview:self.myDatePicker];
NSTimeInterval twoMinutes = 2 * 60;
[self.myDatePicker setCountDownDuration:twoMinutes];
UISlider
self.slider.minimumValue = 0.0f; self.slider.maximumValue = 1,0.0f; self.slider.value = self.slider.maximumValue / 2.0;
[self.slider setThumbImage:[UIImage imageNamed:@"ThumbNormal.png"] forState:UIControlStateNormal];
[self.slider setThumbImage:[UIImage imageNamed:@"ThumbHighlighted.png"] forState:UIControlStateHighlighted];
[self.slider addTarget:selfaction:@selector(sliderValueChanged:)
forControlEvents:UIControlEventValueChanged];
/* Set the tint color of the minimum value */
self.slider.minimumTrackTintColor=[UIColor redColor];/* Set the tint color of the thumb */
self.slider.maximumTrackTintColor=[UIColor greenColor];/* Set the tint color of the maximum value */
self.slider.thumbTintColor = [UIColor blackColor];
UISegmentedControl
NSArray *segments = @[ @"iPhone",
[UIImage imageNamed:@"iPad"],
@"iPod",
@"iMac",];
self.mySegmentedControl = [[UISegmentedControl alloc] initWithItems:segments];
[self.mySegmentedControl addTarget:selfaction:@selector(segmentChanged:)
forControlEvents:UIControlEventValueChanged];
-(void)segmentChanged:(UISegmentedControl*)paramSender{if([paramSender isEqual:self.mySegmentedControl]){
NSInteger selectedSegmentIndex = [paramSender selectedSegmentIndex]; NSString *selectedSegmentText =
[paramSender titleForSegmentAtIndex:selectedSegmentIndex];
NSLog(@"Segment %ld with %@ text is selected",(long)selectedSegmentIndex,selectedSegmentText);
}}
UIViewController
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController"
bundle:nil];
self.window.rootViewController = self.viewController;
UIActivityViewController
UINavigationController
self.navigationController = [[UINavigationController alloc]
initWithRootViewController:viewController]; self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]]; self.window.rootViewController = self.navigationController;
[self.navigationController pushViewController:secondController
animated:YES];
self.title=@"Second Controller";
[self.navigationController popViewControllerAnimated:YES];
NSArray*currentControllers=self.navigationController.viewControllers;
/* Create a mutable array out of this array */
NSMutableArray *newControllers = [NSMutableArray arrayWithArray:currentControllers];
/* Remove the last object from the array */
[newControllers removeLastObject];
/* Assign this array to the Navigation Controller */
self.navigationController.viewControllers = newControllers;
self.navigationItem.titleView = imageView;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:self
action:@selector(performAdd:)];
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithCustomView:simpleSwitch];
UITabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init]; [tabBarController setViewControllers:@[firstViewController, secondViewController
]]; self.window.rootViewController = tabBarController;
-(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self!=nil) {
self.title=@"First";
self.tabBarItem.image=[UIImage imageNamed:@"FirstTab"];
}
returnself;
}
INavigationController *firstNavigationController = [[UINavigationController alloc]
initWithRootViewController:firstViewController];
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:nil
bundle:NULL];
UINavigationController *secondNavigationController = [[UINavigationController alloc]
initWithRootViewController:secondViewController];
UITabBarController *tabBarController = [[UITabBarController alloc] init];
[tabBarController setViewControllers: @[firstNavigationController, secondNavigationController]];
self.window.rootViewController = tabBarController;
UIImageView
typedefNS_ENUM(NSInteger,UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit,
UIViewContentModeScaleAspectFill,
UIViewContentModeRedraw,
UIViewContentModeCenter,
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
self.myImageView.contentMode = UIViewContentModeScaleAspectFit;
UIScrollView
self.myScrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds]; [self.myScrollView addSubview:self.myImageView]; self.myScrollView.contentSize = self.myImageView.bounds.size; [self.view addSubview:self.myScrollView];
@interfaceViewController() <UIScrollViewDelegate>
-(void)scrollViewDidScroll:(UIScrollView*)scrollView{/* Gets called when user scrolls or drags */self.myScrollView.alpha=0.50f;
}
-(void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView{/* Gets called only after scrolling */self.myScrollView.alpha=1.0f;
}
-(void)scrollViewDidEndDragging:(UIScrollView*)scrollViewwillDecelerate:(BOOL)decelerate{
/* Make sure the alpha is reset even if the user is dragging */
self.myScrollView.alpha = 1.0f; }
UIWebView
NSString *htmlString = @"<br/>iOS 7 Programming <strong>Cookbook</strong>"; [self.myWebView loadHTMLString:htmlString
baseURL:nil];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.myWebView loadRequest:request];
@interfaceViewController() <UIWebViewDelegate>
-(void)webViewDidStartLoad:(UIWebView*)webView{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:YES];
}
-(void)webViewDidFinishLoad:(UIWebView*)webView{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
}
-(void)webView:(UIWebView*)webViewdidFailLoadWithError:(NSError*)error{
[[UIApplication sharedApplication]setNetworkActivityIndicatorVisible:NO];
}
构建和显⽰示⻛风格的⽂文本
-(NSAttributedString*)attributedText{
NSString *string = @"iOS SDK";
NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithString:string];
NSDictionary *attributesForFirstWord = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor redColor], NSBackgroundColorAttributeName : [UIColor blackColor] };
NSShadow *shadow = [[NSShadow alloc] init]; shadow.shadowColor = [UIColor darkGrayColor]; shadow.shadowOffset = CGSizeMake(4.0f, 4.0f);
NSDictionary *attributesForSecondWord = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:60.0f],
NSForegroundColorAttributeName : [UIColor whiteColor], NSBackgroundColorAttributeName : [UIColor redColor], NSShadowAttributeName : shadow };
/* Find the string "iOS" in the whole string and sets its attribute */
[result setAttributes:attributesForFirstWord range:[string rangeOfString:@"iOS"]];
/* Do the same thing for the string "SDK" */
[result setAttributes:attributesForSecondWord range:[string rangeOfString:@"SDK"]];
return[[NSAttributedString alloc]initWithAttributedString:result];}
-(void)viewDidLoad{[super viewDidLoad];
self.label = [[UILabel alloc] init]; self.label.backgroundColor = [UIColor clearColor]; self.label.attributedText = [self attributedText]; [self.label sizeToFit]; self.label.center = self.view.center; [self.view addSubview:self.label];
}
UISplitViewController
UIPageViewController
UIPopoverController
- ios学习第1章:实现控制器和视图-基础控件
- ios学习第1章:实现控制器和视图-基础变化
- iOS基础控件-UIViewController,视图控制器
- iOS基础控件-UITabBarController - 2 之UITabBarItem 详解,结合视图控制器和导航控制器的用法
- 【IOS学习】IOS开发之视图和视图控制器
- 窗口,视图,视图控制器和UIKit基础-IOS开发
- iOS-UI-基础控件核心API-视图控制器(ViewControl)
- 实现控制器和视图
- iOS视图控制器<1>
- iOS学习笔记-003.视图 & 视图控制器
- IOS开发之视图和视图控制器
- iOS视图控制器编程指南 --- 实现一个容器视图控制器
- UITextField控件 文本框和视图控制器
- IOS使用视图控制器和视图(四)使用UISlider 实现指定范围值的选择
- IOS学习日志(三)------视图控制器
- iOS开发学习笔记③ - 视图控制器
- ios学习笔记(六)视图控制器
- iOS 学习日志 : 获取视图所在控制器
- 易方达科汇转换为泰达宏利红利先锋可行性分析
- memcache slab
- AC自动机总结
- Asp.Net Ajax简单测试动态显示时间
- Asp.net 在刷新或提交页面后保持滚动条的位置
- ios学习第1章:实现控制器和视图-基础控件
- struts2--(3)--一些小知识--默认action和include标签
- Java抽象类与接口
- 为女神的生日准备惊喜,我开始学习html5游戏开发 -第四天
- Vim编译出现的问题
- 笨小五工作笔记之-游戏储存CCUserDefault
- 黑马程序员_命名和敲代码规范
- 游戏服务器边学边做(00000000) - 前言
- lower_bound upper bound