本帖最后由 竹林风 于 2018-12-6 14:20 编辑
文章导航
创建一个简单的应用程序后会自动生成下面不同的5个文件
AppDelegate.h
[Objective-C] 纯文本查看 复制代码 #import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
代码说明
- AppDelegate调用UIResponder来处理iOS事件。
- 完成UIApplication的命令,提供关键应用程序事件,如启动完毕,终止,等等。
- 在设备的屏幕上用UIWindow对象来管理和协调各种视角,它就像其它加载视图的基本视图一样。通常一个应用程序只有一个窗口。
AppDelegate.m [Objective-C] 纯文本查看 复制代码 #import "AppDelegate.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
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 invalidate graphics rendering callbacks. 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 active 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:.
}
代码说明
- 此处定义UIApplication.上面的方法都是应用程序UI调用和不包含用户定义的方法。
- UIWindow对象被分配用来保存应用程序分配对象
- 调用makeKeyAndVisible能使窗口可见
ViewController.h
[Objective-C] 纯文本查看 复制代码 #import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
代码说明
- ViewController类继承UIViewController,为iOS应用程序提供基本视图管理模型。
ViewController.m
[Objective-C] 纯文本查看 复制代码 #import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
代码说明
- 在这里两种方法实现UIViewController类的基类中的定义
- 初始视图加载后调用viewDidKoad中的安装程序
- 在内存警告的情况下会调用didReceviveMemoryWarning
Main.storyboard
这个默认就是程序的第一个视图界面 就是ViewController,在这里可以直接拖拽相关的控件进行视图布局。也可以在info.list 里面设置不用这个来启动主页面。用纯代码的方式启动应用程序。如下图。
下一篇 Object-C atomic与nonatomic,assign,copy与retain的定义和区别
|