不适用nib,用代码写最小的ios程序

来源:互联网 发布:淘宝刷手怎么注册小号 编辑:程序博客网 时间:2024/05/22 02:11
1 创建ios工程,single view的
2 删除除了main.m以外的.m .h文件,删除故事板(同时修改info.plist)、nib文件
3 修改main.m文件,直接return 0
4 编译一下,应该可以编译通过
现在是一个裸工程,没有功能,但是能够编译通过。
以下代码写入main.m文件,编译运行
#import <UIKit/UIKit.h>@interface ViewController: UIViewController@end@implementation ViewController- (void)viewDidLoad{[super viewDidLoad];UILabel *lab = [[UILabel alloc]initWithFrame: [UIScreen mainScreen].bounds];lab.backgroundColor = [UIColor greenColor];lab.textColor = [UIColor whiteColor];lab.textAlignment = NSTextAlignmentCenter;lab.font = [UIFont boldSystemFontOfSize: 16];lab.text = @"hello world";[self.view addSubview: lab];}- (void)didReciveMemoryWarning {}@end@interface AppDelegate: UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@end@implementation AppDelegate- (BOOL)application: (UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)didFinishLaunchingWithOptions{ViewController *vc;vc = [[ViewController alloc]init];_window = [[UIWindow alloc]initWithFrame: [UIScreen mainScreen].bounds];_window.rootViewController = vc;[_window addSubview: vc.view];[_window makeKeyAndVisible];return YES;}- (void)applicationWillEnterForeground: (UIApplication *)application {}- (void)applicationDidEnterBackground: (UIApplication *)application {}- (void)applicationWillBecomeActive: (UIApplication *)application {}- (void)applicationResignActive: (UIApplication *)application {}- (void)applicationTerminate: (UIApplication *)application {}@endint main(const int argc, char *argv[]){@autoreleasepool {return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));}}
运行截图
空白的函数申明都是可以移除的,实际代码很少,ios程序的架构可以看的更清楚。



0 0
原创粉丝点击