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;
      [alertView show];

代理方法

-(void)alertView:(UIAlertView*)alertViewclickedButtonAtIndex:(NSInteger)buttonIndex{

page50image7592page50image7752
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;
//具体选择项 返回0开始的索引

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];
page70image14864
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 

//建⽴立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 


0 0