如今的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 。欢迎大家访问!
from:http://blog.csdn.net/chenyufeng1991/article/details/49506039
|
请发表评论