iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
2017-06-17 21:19
609 查看
如今的iOS开发,已经比以前有了很大的简便,尤其是界面设计方面。主要是因为使用了nib文件和storyboard。但是作为程序员,我们需要挑战一下自己,如果没有nib文件和storyboard,我们怎么来构建一个应用。该Demo我放在了 https://github.com/chenyufeng1991/JumpAndNavigationCode
中的03文件夹下 。
(1)新建一个ios项目,直接删除Main.storyboard文件,然后在下面选项中删除Main...
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/05/4ed2c6e5ee2a883c02171f7de32f9b6a)
.
(2)在AppDelegate.m中实现如下:
[objc] view
plain copy
print?
#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中实现如下:
[objc] view
plain copy
print?
#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中实现如下:
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(strong,nonatomic)UIWindow *window;
@end
(5)SecondViewController.m中实现如下:
[objc] view
plain copy
print?
#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)运行程序,查看效果:
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/05/62a2c7b9c1823b014daefc61a3f77ed4)
。
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/05/d4db0d79c07a24734c563cc30ff9f750)
。
综上,我们既要能在nib和storyboard的帮助下快速构建一个应用,也需要在必要时能用代码来解决问题,让我们继续快乐的开发吧!(其实个人还是非常推荐使用xib的,使用代码实现各种UI看似很吊,但是效率也会很低下,如果是涉及Autolayout的话,那就更为麻烦了。)
github主页:https://github.com/chenyufeng1991 。欢迎大家访问!
from:http://blog.csdn.net/chenyufeng1991/article/details/49506039
中的03文件夹下 。
(1)新建一个ios项目,直接删除Main.storyboard文件,然后在下面选项中删除Main...
.
(2)在AppDelegate.m中实现如下:
[objc] view
plain copy
print?
#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中实现如下:
[objc] view
plain copy
print?
#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中实现如下:
[objc] view
plain copy
print?
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property(strong,nonatomic)UIWindow *window;
@end
(5)SecondViewController.m中实现如下:
[objc] view
plain copy
print?
#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 。欢迎大家访问!
from:http://blog.csdn.net/chenyufeng1991/article/details/49506039
相关文章推荐
- iOS纯代码实现界面建立、跳转、导航栏(无storyboard、无nib)(Objective-C)
- iOS界面跳转与返回代码实现(Objective-C)
- iOS 用代码实现跳转到设置界面
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- iOS用代码实现导航栏NavigationBar与跳转(Objective-C)
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 大钟的ios开发之旅(4)————简单谈谈ios程序界面实现的三种方式(代码创建,xib和storyboard)
- Objective-C弹出模态学习之纯代码跳转到xib界面以及storyboard界面
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- 【iOS界面处理】使用storyboard实现页面跳转,简单的数据传递
- IOS学习笔记32—使用Storyboard实现复杂界面
- iOS开发有用代码(有判断邮箱格式是否正确的代码、图片压缩、亲测可用的图片上传代码和实现点击图片进行跳转的代码等。)
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- ios开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局
- IOS---通过控件实现界面跳转
- Storyboard、Nib文件和代码来实现UI的利与弊
- Storyboard、Nib文件和代码来实现UI的利与弊
- iOS利用代理实现界面跳转
- iOS开发UI篇—使用纯代码自定义UItableviewcell实现一个简单的微博界面布局