博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UI代码练习-视图的层次关系
阅读量:6250 次
发布时间:2019-06-22

本文共 8899 字,大约阅读时间需要 29 分钟。

hot3.png

////  AppDelegate.h//  视图的层次关系////  Created by on 14-12-17.//  Copyright (c) 2014年 apple. All rights reserved.//#import 
#import
@interface AppDelegate : UIResponder
{ UIView *view1; UIView *view2; UIView *view3;}@property (strong, nonatomic) UIWindow *window;@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;- (void)saveContext;- (NSURL *)applicationDocumentsDirectory;@end

////  AppDelegate.m//  视图的层次关系////  Created by on 14-12-17.//  Copyright (c) 2014年 apple. All rights reserved.//#import "AppDelegate.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        view1 = [[UIView alloc] initWithFrame:CGRectMake(60, 100, 200, 100)];    view1.backgroundColor = [UIColor redColor];    [self.window addSubview:view1];        view2 = [[UIView alloc] initWithFrame:CGRectMake(60, 170, 200, 100)];    view2.backgroundColor = [UIColor yellowColor];    [self.window addSubview:view2];    //    view1和view2都是加在UIWindow上的,所以他们的super view都是UIWindow//    NSLog(@"view1 super view: %@", [view1 superview]);//    NSLog(@"view2 super view: %@", [view2 superview]);        view3 = [[UIView alloc] initWithFrame:CGRectMake(60,100, 200, 50)];    view3.backgroundColor = [UIColor blueColor];    [self.window addSubview:view3];    //    这次是将view3加在了view1的上面,所以view3的super view 是view1//    NSLog(@"view1 super view: %@", [view1 superview]);//    NSLog(@"view3 super view: %@", [view3 superview]);        UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button1.frame = CGRectMake(90, 300, 140, 35);    [button1 setTitle:@"使view1至于顶层" forState:UIControlStateNormal];    [button1 addTarget:self action:@selector(changeView1) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button1];        UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button2.frame = CGRectMake(90, 340, 140, 35);    [button2 setTitle:@"使view2至于顶层" forState:UIControlStateNormal];    [button2 addTarget:self action:@selector(changeView2) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button2];        UIButton *button3 = [UIButton buttonWithType:UIButtonTypeRoundedRect];    button3.frame = CGRectMake(90, 380, 140, 35);    [button3 setTitle:@"使view3至于顶层" forState:UIControlStateNormal];    [button3 addTarget:self action:@selector(changeView3) forControlEvents:UIControlEventTouchUpInside];    [self.window addSubview:button3];        return YES;}- (void)changeView1 {    [self.window bringSubviewToFront:view1];}- (void)changeView2 {    [self.window bringSubviewToFront:view2];}- (void)changeView3 {    [self.window bringSubviewToFront:view3];}- (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 {    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.    // Saves changes in the application's managed object context before the application terminates.    [self saveContext];}#pragma mark - Core Data stack@synthesize managedObjectContext = _managedObjectContext;@synthesize managedObjectModel = _managedObjectModel;@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;- (NSURL *)applicationDocumentsDirectory {    // The directory the application uses to store the Core Data store file. This code uses a directory named "apple._______" in the application's documents directory.    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];}- (NSManagedObjectModel *)managedObjectModel {    // The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.    if (_managedObjectModel != nil) {        return _managedObjectModel;    }    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"_______" withExtension:@"momd"];    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];    return _managedObjectModel;}- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {    // The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.    if (_persistentStoreCoordinator != nil) {        return _persistentStoreCoordinator;    }        // Create the coordinator and store        _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"_______.sqlite"];    NSError *error = nil;    NSString *failureReason = @"There was an error creating or loading the application's saved data.";    if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {        // Report any error we got.        NSMutableDictionary *dict = [NSMutableDictionary dictionary];        dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";        dict[NSLocalizedFailureReasonErrorKey] = failureReason;        dict[NSUnderlyingErrorKey] = error;        error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];        // Replace this 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();    }        return _persistentStoreCoordinator;}- (NSManagedObjectContext *)managedObjectContext {    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)    if (_managedObjectContext != nil) {        return _managedObjectContext;    }        NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];    if (!coordinator) {        return nil;    }    _managedObjectContext = [[NSManagedObjectContext alloc] init];    [_managedObjectContext setPersistentStoreCoordinator:coordinator];    return _managedObjectContext;}#pragma mark - Core Data Saving support- (void)saveContext {    NSManagedObjectContext *managedObjectContext = self.managedObjectContext;    if (managedObjectContext != nil) {        NSError *error = 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();        }    }}@end

转载于:https://my.oschina.net/are1OfBlog/blog/364563

你可能感兴趣的文章
java web编程 servlet读取配置文件参数
查看>>
ChartControl实现时间轴实现
查看>>
生成器函数
查看>>
Google(谷歌)中国工程研究院 工程师 方坤 对学生朋友的一些建议
查看>>
oracle 优化——索引与组合索引
查看>>
android基础—尺寸单位和屏幕适配
查看>>
小试 ScriptManager
查看>>
异常处理
查看>>
C/S模型之消息传输
查看>>
一道int与二进制加减题
查看>>
Java中输入判定的错误和纠正
查看>>
详解Nginx 13: Permission denied 解决方案
查看>>
InPlace Transition of a matrix
查看>>
Project Euler 26 Reciprocal cycles( 分数循环节 )
查看>>
做了几道简单的基础题,慢慢熟悉循环
查看>>
元素的多种延时等待(&页面的超时处理)
查看>>
ios 后台发送邮件之SKPSMTPMessage的使用
查看>>
JavaScript学习
查看>>
3014C语言_运算符
查看>>
202702算法_二分法查找
查看>>