iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
来源:互联网 发布:农村淘宝服务站加盟费 编辑:程序博客网 时间:2024/05/14 00:47
如今的iOS开发,已经比以前有了很大的简便,尤其是界面设计方面。主要是因为使用了nib文件和storyboard。但是作为程序员,我们需要挑战一下自己,如果没有nib文件和storyboard,我们怎么来构建一个应用。该Demo我放在了 https://github.com/chenyufeng1991/JumpAndNavigationCode 中的03文件夹下 。
(1)新建一个iOS项目,直接删除Main.storyboard文件,然后在下面选项中删除Main...
.
(2)在AppDelegate.m中实现如下:
#import "AppDelegate.h"#import "ViewController.h"#import "SecondViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; //这里加载第一个页面; UINavigationController *navC = [[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]]; self.window.backgroundColor = [UIColor whiteColor]; self.window.rootViewController = navC; [self.window makeKeyAndVisible]; return YES;}@end
(3)在第一个界面中的ViewController.m中实现如下:
#import "ViewController.h"#import "SecondViewController.h"@interface ViewController ()//@property(strong,nonatomic) UIButton *button;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; }- (void)viewDidAppear:(BOOL)animated{ [super viewDidAppear:animated]; UIButton *button = [[UIButton alloc] init]; button = [[UIButton alloc] initWithFrame:CGRectMake(50, 100, 50, 20)]; [button setTitle:@"跳转" forState:UIControlStateNormal]; [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [self.view addSubview:button]; [button addTarget:self action:@selector(jump:) forControlEvents:UIControlEventTouchUpInside]; [self.navigationItem setTitle:@"首页"];}- (void)jump:(id)sender{ //如果没有导航栏,就进行这种跳转;// [self presentViewController:[[SecondViewController alloc] init] animated:true completion:^{// // }]; [self.navigationController pushViewController:[[SecondViewController alloc] init] animated:true]; }@end
(4)在第二个界面SecondViewController.h中实现如下:
#import <UIKit/UIKit.h>@interface SecondViewController : UIViewController@property(strong,nonatomic)UIWindow *window;@end
(5)SecondViewController.m中实现如下:
#import "SecondViewController.h"@interface SecondViewController ()@end@implementation SecondViewController- (void)viewDidLoad { [super viewDidLoad]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UINavigationController *navi = [[UINavigationController alloc] init]; [navi addChildViewController:self]; [self.window makeKeyAndVisible]; //设置导航标题,这个时候的返回按钮的title就是上一级的navigationItem的title文字 [self.navigationItem setTitle:@"子页"]; }@end
(6)运行程序,查看效果:
。
。
综上,我们既要能在nib和storyboard的帮助下快速构建一个应用,也需要在必要时能用代码来解决问题,让我们继续快乐的开发吧!(其实个人还是非常推荐使用xib的,使用代码实现各种UI看似很吊,但是效率也会很低下,如果是涉及Autolayout的话,那就更为麻烦了。)
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
1 0
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- iOS界面跳转与返回代码实现(Objective-C)
- IOS swift3.1 创建Swift空工程(无storyBoard)纯代码方式编写APP
- iOS用代码实现导航栏NavigationBar与跳转(Objective-C)
- iOS学习之——纯代码跳转到xib界面以及storyboard界面
- IOS 页面跳转方法(采用Storyboard或者纯代码)
- 纯代码跳转到xib界面以及storyboard界面
- 纯代码跳转到xib界面以及storyboard界面
- 纯代码跳转到Xib界面和Storyboard界面
- IOS storyboard 无Segue连接,手动跳转,并传递变量
- Storyboard实现界面跳转
- iOS UICollectionView 纯代码,无xib
- IOS 纯代码实现界面
- 纯代码无图片Tips实现过程
- IOS storyboard文件界面跳转
- iOS storyBoard 和 纯代码 实现 九宫格 切图
- iOS 纯代码跳转
- EA Sports reveal price range and transfer market changes
- TCP/IP协议
- Number Sequence
- csrf laravel
- Android中可以做的两件坏事---破解锁屏密码和获取Wifi密码
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- mysql日期函数互转
- 第八周项目4-字符串加密
- 第九周上机实践—项目2(2)—实现两个用压缩形式存储的对称矩阵的加法和乘法
- 在MapReduce中利用MultipleOutputs输出多个文件
- 第九周 项目二(2) 对称矩阵压缩存储的实现与应用
- 听我说说我的博客: 月访问量过万的个人IT博客的技术史
- netbeans打开UTF文件,提示无法使用
- Notepad 自动补全的插件 安装 (包括html,JavaScript)