一、删除main.storyboard,并在info.plist中删除main storyboard file base name
二、在-(BOOL)application:didFinishLauchingWithOptions:中添加以下代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//创建UIWindow对象
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//设置UIWindow的背景色
self.window.backgroundColor = [UIColor whiteColor];
//将该UIWindow对象设为主窗口并显示出来
[self.window makeKeyAndVisible];
//创建一个UIViewController对象
UIViewController *controller = [[UIViewController alloc] init];
self.window.rootViewController = controller;
//创建一个UIView对象
UIView *rootView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//设置controller显示rootView
controller.view = rootView;
//创建一个按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(120, 100, 80, 40);
[button setTitle:@"确定" forState:UIControlStateNormal];
[button setTitle:@"取消" forState:UIControlStateHighlighted];
[rootView addSubview:button];
//创建一个UILabel对象
self.show = [[UILabel alloc] initWithFrame:CGRectMake(60, 40, 180, 30)];
[self.show setText:@"初始文本"];
[self.show setBackgroundColor:[UIColor redColor]];
[rootView addSubview:self.show];
//为圆角按钮的触碰事件绑定事件处理方法
[button addTarget:self action:@selector(clickHandler:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:rootView];
// Override point for customization after application launch.
return YES;
}
- (IBAction)clickHandler:(id)sender
{
self.show.text = @"hahah";
}注:1、由于学习iOS的时候已经是iOS7,所以外国的教程中都推荐用IB来写界面。不过为了更好地理解界面的底层实现,还是要学习以下纯代码编写。
2、因为之前有cocos2d-x的学习经验,所以设置对象,添加子视图的操作还是比较熟悉的。
参考资料:《疯狂ios讲义》
这篇博客介绍了如何在iOS应用中不使用main.storyboard,而是通过代码创建UI界面。作者首先删除了main.storyboard文件,并在info.plist中移除了主故事板引用。接着在应用程序启动方法中添加代码来设置界面元素并添加子视图。作者提到,由于有cocos2d-x的背景,这部分工作相对较熟悉。
&spm=1001.2101.3001.5002&articleId=49103613&d=1&t=3&u=023ae4a6fefc46148d00058a353f5415)
3万+

被折叠的 条评论
为什么被折叠?



