• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C) ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

  如今的iOS开发,已经比以前有了很大的简便,尤其是界面设计方面。主要是因为使用了nib文件和storyboard。但是作为程序员,我们需要挑战一下自己,如果没有nib文件和storyboard,我们怎么来构建一个应用。该Demo我放在了  https://github.com/chenyufeng1991/JumpAndNavigationCode  中的03文件夹下 。

(1)新建一个ios项目,直接删除Main.storyboard文件,然后在下面选项中删除Main...

.


(2)在AppDelegate.m中实现如下:

[objc] view plain copy
 print?
  1. #import "AppDelegate.h"  
  2. #import "ViewController.h"  
  3. #import "SecondViewController.h"  
  4.   
  5. @interface AppDelegate ()  
  6.   
  7. @end  
  8.   
  9. @implementation AppDelegate  
  10.   
  11.   
  12. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
  13.   
  14.   self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  15.   
  16.   //这里加载第一个页面;  
  17.   UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];  
  18.     
  19.   self.window.backgroundColor = [UIColor whiteColor];  
  20.   self.window.rootViewController = navC;  
  21.   [self.window makeKeyAndVisible];  
  22.     
  23.   return YES;  
  24. }  
  25.   
  26. @end  

(3)在第一个界面中的ViewController.m中实现如下:

[objc] view plain copy
 print?
  1. #import "ViewController.h"  
  2. #import "SecondViewController.h"  
  3.   
  4. @interface ViewController ()  
  5.   
  6. //@property(strong,nonatomic) UIButton *button;  
  7.   
  8. @end  
  9.   
  10. @implementation ViewController  
  11.   
  12. - (void)viewDidLoad {  
  13.   [super viewDidLoad];  
  14.   
  15.   
  16.     
  17.     
  18. }  
  19.   
  20.   
  21.   
  22. - (void)viewDidAppear:(BOOL)animated{  
  23.   
  24.   [super viewDidAppear:animated];  
  25.     
  26.   UIButton *button = [[UIButton alloc] init];  
  27.     
  28.   button = [[UIButton alloc] initWithFrame:CGRectMake(501005020)];  
  29.   [button setTitle:@"跳转" forState:UIControlStateNormal];  
  30.   [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];  
  31.     
  32.   [self.view addSubview:button];  
  33.     
  34.     
  35.   [button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside];  
  36.     
  37.   [self.navigationItem setTitle:@"首页"];  
  38. }  
  39.   
  40.   
  41. - (void)jump:(id)sender{  
  42.   
  43.   //如果没有导航栏,就进行这种跳转;  
  44. //  [self presentViewController:[[SecondViewController alloc] init] animated:true completion:^{  
  45. //      
  46. //  }];  
  47.   
  48.   [self.navigationController pushViewController:[[SecondViewController alloc] init] animated:true];  
  49.     
  50.     
  51. }  
  52.   
  53.   
  54. @end  

(4)在第二个界面SecondViewController.h中实现如下:

[objc] view plain copy
 print?
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface SecondViewController : UIViewController  
  4.   
  5. @property(strong,nonatomic)UIWindow *window;  
  6.   
  7. @end  

(5)SecondViewController.m中实现如下:

[objc] view plain copy
 print?
  1. #import "SecondViewController.h"  
  2.   
  3. @interface SecondViewController ()  
  4.   
  5. @end  
  6.   
  7. @implementation SecondViewController  
  8.   
  9. - (void)viewDidLoad {  
  10.     [super viewDidLoad];  
  11.   
  12.   self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];  
  13.     
  14.   UINavigationController *navi = [[UINavigationController alloc] init];  
  15.     
  16.   [navi addChildViewController:self];  
  17.     
  18.   [self.window makeKeyAndVisible];  
  19.     
  20.   //设置导航标题,这个时候的返回按钮的title就是上一级的navigationItem的title文字  
  21.   [self.navigationItem setTitle:@"子页"];  
  22.     
  23.     
  24. }  
  25.   
  26. @end  

(6)运行程序,查看效果:



     综上,我们既要能在nib和storyboard的帮助下快速构建一个应用,也需要在必要时能用代码来解决问题,让我们继续快乐的开发吧!(其实个人还是非常推荐使用xib的,使用代码实现各种UI看似很吊,但是效率也会很低下,如果是涉及Autolayout的话,那就更为麻烦了。)



github主页:https://github.com/chenyufeng1991  。欢迎大家访问!



from:http://blog.csdn.net/chenyufeng1991/article/details/49506039



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
如何在Objective-C的环境下实现defer发布时间:2022-07-12
下一篇:
Objective-C外观模式--简单介绍和使用发布时间:2022-07-12
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap