iOS开发——程序的基本结构

来源:互联网 发布:ubuntu搭建私有云 编辑:程序博客网 时间:2024/06/10 01:28
<span style="color: rgb(75, 209, 87); font-size: 18px; font-family: Menlo;"> </span><span style="color: rgb(75, 209, 87); font-family: 'Heiti SC Light'; font-size: 18px;">单例:单例设计模式,单例是与程序生命周期相同的对象,该对象仅能创建一次并且无法被持有或释放,可以在程序任何位置访问。</span>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(75, 209, 87);"><span style="font-family: Menlo;">AppDelegate</span>:不是单例,但是程序中仅能存在一个程序委托,由<span style="font-family: Menlo;">UIApplicationMain()</span>函数创建的,禁止手动创建。</p>
AppDelegate.m
// 程序加载完成时:程序基础用户界面初始化、程序依赖的用户数据加载等- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    // 打印当前方法调用    // _cmd:SEL类型,@selector,当前方法的选择器    NSLog(@"%@", NSStringFromSelector(_cmd));        // UIWindow:程序设备的窗口对象,程序需要显示任何的界面元素需要一个窗口来管理,窗口默认由AppDelegate初始化,一般情况无需手动创建。    // 初始化窗口:使用设备全屏大小    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.        // UIWindowLevel:表示窗口等级,窗口等级越高的窗口优先显示,同级窗口中后显示的优先显示。    NSLog(@"window level = %.2f", self.window.windowLevel);    // 一般级别    NSLog(@"normal = %.2f", UIWindowLevelNormal);    // 状态栏级别    NSLog(@"status bar = %.2f", UIWindowLevelStatusBar);    // 警告级别    NSLog(@"alert = %.2f", UIWindowLevelAlert);        // self.window.windowLevel = UIWindowLevelStatusBar;        // 配置窗口背景色    self.window.backgroundColor = [UIColor whiteColor];    // 将窗口作为程序的主窗口,并显示出来。    [self.window makeKeyAndVisible];        /* Hello World! */        /*     CGRect:矩形,描述了视图的位置及大小,使用函数CGRectMake()生成;     CGPoint:点,描述了视图的位置,使用函数CGPointMake()生成;     CGSize:大小,描述了视图的宽和高,使用函数CGSizeMake()生成。    */         // UILabel:标签视图,主要用于文本显示,不支持用户交互。    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 100, 320, 44)];    // 文本    _label.text = @"Hello World!";    // 字体    _label.font = [UIFont italicSystemFontOfSize:30];    // 字体颜色    _label.textColor = [UIColor blackColor];    // 对齐方    _label.textAlignment = NSTextAlignmentCenter;    // 背景色    _label.backgroundColor = [UIColor clearColor];    // 添加到父视图并显示,子视图一旦添加到父视图之上,父视图负责持有子视图,retainCount + 1。    [self.window addSubview:_label];        // UITextField:文本输入框,支持用户交互,用于用户自定义文本输入。    _textField = [[UITextField alloc] init];    // 配置视图中心点坐标    _textField.center = CGPointMake(160, 180);    // 配置视图边框大小    _textField.bounds = CGRectMake(0, 0, 200, 25);    // 配置边框类型    _textField.borderStyle = UITextBorderStyleRoundedRect;    // 配置占位符    _textField.placeholder = @"请输入文本...";    // 配置字体    _textField.font = [UIFont systemFontOfSize:15];    [self.window addSubview:_textField];         // UIButton:按钮控件,支持用户交互,指定点击方式触发自定义逻辑。    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];    button.center = CGPointMake(160, 240);    button.bounds = CGRectMake(0, 0, 100, 30);    // 配置按钮标题文本    [button setTitle:@"确定"            forState:UIControlStateNormal];        // 配置按钮关联事件    // target:配置按钮关联的对象,该对象负责执行按钮响应方法;    // action:target即将调用的方法,当方法仅接受一个参数时,方法在触发的时候将获取到当前按钮对象;    // controlEvents:按钮点击方式    [button addTarget:self               action:@selector(buttonPressed:)     forControlEvents:UIControlEventTouchUpInside];        [self.window addSubview:button];            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.        NSLog(@"%@", NSStringFromSelector(_cmd));}// 程序进入后台:- (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.        NSLog(@"%@", NSStringFromSelector(_cmd));}// 程序将要进入前台:- (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.        NSLog(@"%@", NSStringFromSelector(_cmd));}// 程序激活:刷新数据、读取数据、恢复界面、恢复网络链接等- (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.        NSLog(@"%@", NSStringFromSelector(_cmd));}// 程序异常结束- (void)applicationWillTerminate:(UIApplication *)application{    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.        NSLog(@"%@", NSStringFromSelector(_cmd));}@end

0 0
原创粉丝点击