新建的project以及ViewController 文件   跟之前的文章一样

直接贴代码(多码伤眼,伤身,伤脑,勿入)

AllUIKitAppDelegate.h

#import <UIKit/UIKit.h>
#import "mySwitchView.h"

@interface AllUIKitAppDelegate : UIResponder <UIApplicationDelegate>


@property (strong, nonatomic) UIWindow *window;
@property  (nonatomic,retain) mySwitchView *mySwitchViewController;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (NSURL *)applicationDocumentsDirectory;

@end

 

AllUIKitAppDelegate.m

#import "AllUIKitAppDelegate.h"

@implementation AllUIKitAppDelegate

@synthesize window = _window;
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
@synthesize mySwitchViewController=_mySwitchViewController;

- (void)dealloc
{
    [_window release];
    [_mySwitchViewController release];
    [__managedObjectContext release];
    [__managedObjectModel release];
    [__persistentStoreCoordinator release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.mySwitchViewController =[[mySwitchView alloc]init];
    self.window.rootViewController=self.mySwitchViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    /*
     Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
     Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     */
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    /*
     Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
     If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
     */
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    /*
     Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
     */
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    /*
     Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
     */
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Saves changes in the application's managed object context before the application terminates.
    [self saveContext];
}

- (void)saveContext
{
    NSError *error = nil;
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
    if (managedObjectContext != nil)
    {
        if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error])
        {
            /*
             Replace this implementation with code to handle the error appropriately.
             
             abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
             */
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        } 
    }
}

#pragma mark - Core Data stack

/**
 Returns the managed object context for the application.
 If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
 */
- (NSManagedObjectContext *)managedObjectContext
{
    if (__managedObjectContext != nil)
    {
        return __managedObjectContext;
    }
    
    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
    if (coordinator != nil)
    {
        __managedObjectContext = [[NSManagedObjectContext alloc] init];
        [__managedObjectContext setPersistentStoreCoordinator:coordinator];
    }
    return __managedObjectContext;
}

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created from the application's model.
 */
- (NSManagedObjectModel *)managedObjectModel
{
    if (__managedObjectModel != nil)
    {
        return __managedObjectModel;
    }
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"mySwitchView" withExtension:@"momd"];
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
    return __managedObjectModel;
}

/**
 Returns the persistent store coordinator for the application.
 If the coordinator doesn't already exist, it is created and the application's store added to it.
 */
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
    if (__persistentStoreCoordinator != nil)
    {
        return __persistentStoreCoordinator;
    }
    
    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"mySwitchView.sqlite"];
    
    NSError *error = nil;
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
    {
        /*
         Replace this implementation with code to handle the error appropriately.
         
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
         
         Typical reasons for an error here include:
         * The persistent store is not accessible;
         * The schema for the persistent store is incompatible with current managed object model.
         Check the error message to determine what the actual problem was.
         
         
         If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
         
         If you encounter schema incompatibility errors during development, you can reduce their frequency by:
         * Simply deleting the existing store:
         [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
         
         * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
         [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
         
         Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
         
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    
    
    return __persistentStoreCoordinator;
}

#pragma mark - Application's Documents directory

/**
 Returns the URL to the application's Documents directory.
 */
- (NSURL *)applicationDocumentsDirectory
{
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

@end

  

mySwitchView.h(ViewController)

#import <Foundation/Foundation.h>

@interface mySwitchView : UIViewController<UIActionSheetDelegate,UIAlertViewDelegate >

@property (nonatomic,retain) IBOutlet UILabel *nameLabel;
@property (nonatomic,retain) IBOutlet UILabel *passWordLable;
@property (nonatomic,retain) IBOutlet UITextField *nameField;
@property (nonatomic,retain) IBOutlet UITextField *passWordField;
@property   (nonatomic,retain) IBOutlet UISlider *mySlider;
@property (nonatomic,retain)    IBOutlet UILabel *sliderValueLabel;
@property   (nonatomic,retain) IBOutlet UISwitch *leftSwitch;
@property   (nonatomic,retain) IBOutlet UISwitch *rightSwitch;
@property   (nonatomic,retain) IBOutlet UIButton *doSomeThingButton;
@property   (nonatomic,retain)  IBOutlet UISegmentedControl *segmentedController;

-(IBAction)mySliderPressed:(id)sender;

-(IBAction)switchChanged:(id)sender;

-(IBAction)segmentedControllerPressed:(id)sender;

-(IBAction)doSomeThingButtonAction:(id)sender;
@end

  

mySwitchView.m

#import "mySwitchView.h"

@implementation mySwitchView

@synthesize nameLabel=_nameLabel;
@synthesize passWordLable=_passWordLable;
@synthesize nameField=_nameField;
@synthesize passWordField=_passWordField;
@synthesize mySlider=_mySlider;
@synthesize sliderValueLabel=_sliderValueLabel;
@synthesize leftSwitch=_leftSwitch;
@synthesize rightSwitch=_rightSwitch;
@synthesize doSomeThingButton=_doSomeThingButton;
@synthesize segmentedController=_segmentedController;

-(void)dealloc
{
    [_nameLabel release];
    [_passWordLable release];
    [_nameField release];
    [_passWordField release];
    [_mySlider release];
    [_sliderValueLabel release];
    [_leftSwitch release];
    [_rightSwitch release];
    [_doSomeThingButton release];
    [_segmentedController release];
    [super dealloc];
}



-(void)loadView
{
    //creat a new  main view;
    UIView *view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
    [view setBackgroundColor:[UIColor whiteColor]];
    self.view=view;
    [view release];
    view=nil;
    
    //creat nameLabel
    CGRect nameLabelFrame=CGRectMake(0, 60,100,25);
    self.nameLabel=[[UILabel alloc]initWithFrame:nameLabelFrame];
    [self.nameLabel setText:@"Name:"];
    [self.nameLabel setTextColor:[UIColor redColor]];
    [self.nameLabel setTextAlignment:UITextAlignmentRight];
    [self.view addSubview:self.nameLabel];
    [self.nameLabel release];
    self.nameLabel =nil;
    
    //creat passWord
    CGRect passWordFrame=CGRectMake(0, 100, 100, 25);
    self.passWordLable =[[UILabel alloc]initWithFrame:passWordFrame];
    [self.passWordLable setText:@"PassWord:"];
    [self.passWordLable setTextColor:[UIColor redColor]];
    [self.passWordLable setTextAlignment:UITextAlignmentRight];
    [self.view addSubview:self.passWordLable];
    [self.passWordLable release];
    self.passWordLable =nil;
    
    //creat nameField
    CGRect nameFieldFrame=CGRectMake(110, 60, 200, 25);
    self.nameField=[[UITextField alloc]initWithFrame:nameFieldFrame];
    [self.nameField setPlaceholder:@"User Name"];
    [self.nameField setReturnKeyType:UIReturnKeyNext];
    [self.nameField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.nameField setTextAlignment:UITextAlignmentLeft];
    [self.view addSubview:self.nameField];
    [self.nameField release];
   
    
    //creat passWordField  and set the keyboard only numberpad.and set placeholder:only number!
    CGRect passWordFieldFrame=CGRectMake(110, 100, 200, 25);
    self.passWordField=[[UITextField alloc]initWithFrame:passWordFieldFrame];
    [self.passWordField setBorderStyle:UITextBorderStyleRoundedRect];
    [self.passWordField setPlaceholder:@"PassWord(only number)"];
    [self.passWordField setKeyboardType:UIKeyboardTypeNumberPad];
    [self.passWordField setTextAlignment:UITextAlignmentLeft];
    [self.passWordField setSecureTextEntry:YES];
    [self.view addSubview:self.passWordField];
    [self.passWordField release];
   
    
    //creat the mySlider label;
    CGRect mySliderFrame=CGRectMake(80, 150, 200, 10);
    self.mySlider=[[UISlider alloc]initWithFrame:mySliderFrame];
    [self.mySlider setMaximumValue:(int)100];
    [self.mySlider setMinimumValue:(int)0];
    [self.mySlider setValue:(int)1 animated:YES];
    [self.mySlider addTarget:self action:@selector(mySliderPressed:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.mySlider];
    [self.mySlider release];
    
    
    //creat the mySliderValueLabel
    CGRect mySliderValueLabelFrame= CGRectMake(40, 150, 35, 15);
    self.sliderValueLabel=[[UILabel alloc]initWithFrame:mySliderValueLabelFrame];  
    self.sliderValueLabel.text=[NSString stringWithFormat:@"%d", self.mySlider.minimumValue];
    [self.sliderValueLabel setTextAlignment:UITextAlignmentLeft];
    [self.view addSubview:self.sliderValueLabel];
    [self.sliderValueLabel release];
    
    //creat the leftSwitch and rightSwitch
    CGRect leftSwitchFrame=CGRectMake(50, 190, 30, 10);
    self.leftSwitch=[[UISwitch alloc]initWithFrame:leftSwitchFrame];
    [self.leftSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventAllEvents];
    [self.view addSubview:self.leftSwitch];
    [self.leftSwitch release];
    
    
    CGRect rightSwitchFrame=CGRectMake(180, 190, 30, 10);
    self.rightSwitch=[[UISwitch alloc] initWithFrame:rightSwitchFrame];
    [self.rightSwitch addTarget:self action:@selector(switchChanged:) forControlEvents:UIControlEventAllEvents];
    [self.view addSubview:self.rightSwitch];
    [self.rightSwitch release];
    
    
    
    
    //creat a segmentedController   have three choice button;
    CGRect segmentedControllerFrame=CGRectMake(30, 250, 260, 40);
    NSArray *segmentedControllerArray=[NSArray arrayWithObjects:@"First",@"Second",@"Third", nil];
    self.segmentedController=[[UISegmentedControl alloc]initWithItems:segmentedControllerArray];
    [self.segmentedController setSegmentedControlStyle:UISegmentedControlStylePlain];
    self.segmentedController.frame=segmentedControllerFrame;
    [self.segmentedController addTarget:self action:@selector(segmentedControllerPressed:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.segmentedController];
    [self.segmentedController release];
    self.segmentedController=nil;
    
    


    //creat the doSomeThingButton  and addTarget:self when you TouchDown. it will be  action:@selector(doSomeThingButtonAction:)
    CGRect doSomeThingButtonFrame=CGRectMake(120, 320,80 , 40);
    self.doSomeThingButton  =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    self.doSomeThingButton.frame=doSomeThingButtonFrame;
    [self.doSomeThingButton setShowsTouchWhenHighlighted:YES];
    [self.doSomeThingButton setTitle:@"Button" forState:UIControlStateNormal];
    [self.doSomeThingButton addTarget:self action:@selector(doSomeThingButtonAction:) forControlEvents:UIControlEventTouchDown];
    [self.doSomeThingButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [self.view addSubview:self.doSomeThingButton];
    [self.doSomeThingButton release];
        
    
}


//set nameField and passWordField became the firstResponder to the keyboard. if you touch other place.the keyboard will be hiding;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.nameField resignFirstResponder];
    [self.passWordField resignFirstResponder];
}


//When you dragging the Slider ,There have a Label discription the Slider Current Vlaue;

-(IBAction)mySliderPressed:(id)sender
{
    [self.sliderValueLabel setText: [NSString stringWithFormat:@"%d",(int)self.mySlider.value]];
}


// It make the different Switch will be self synchronization :Ex  leftSwitch.isOn  ,the rightSwitch.isOn  and if one of these changed,each others also synchronization changed

-(IBAction)switchChanged:(id)sender
{
    UISwitch *chooseSwitch=(UISwitch*)sender;
    BOOL setting=chooseSwitch.isOn;
    
    [self.leftSwitch setOn:setting animated:YES];
    [self.rightSwitch setOn:setting animated:YES];
    
}



//when segementedController index changed

-(IBAction)segmentedControllerPressed:(id)sender
{

    if ([sender selectedSegmentIndex]%2 !=0 ) {
        self.doSomeThingButton.hidden=YES;
    }
    else
    {  self.doSomeThingButton.hidden=NO; }
}

//when you touch the doSomeThingButton ,It will show An actionSheet on the view

-(IBAction)doSomeThingButtonAction:(id)sender
{
    UIActionSheet *actionSheet=[[UIActionSheet alloc]initWithTitle:@"action sheet" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"First",@"Second", nil];
    [actionSheet showInView:self.view];
    [actionSheet release];
    actionSheet =nil;
    
}

//  when you touch the actionSheet button,it will be show an AlertView on the View 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"You touch the %d button",buttonIndex);
    NSString *theButtonIndex=[NSString stringWithFormat:@"%d",buttonIndex];
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Alret View" message:theButtonIndex delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [alert show];
    [alert release];
    alert=nil;
    
}



@end

  

求拍砖。。。。。。。。。。。

posted on 2012-11-25 09:42  xxppxiaowei  阅读(230)  评论(0编辑  收藏  举报