iOS 入门 界面UI 界面跳转

作为一个Android开发的小小猿人,推开了IOS开发的大门。开发成本是高昂的。不过公司的mac,开发证书什么的都可以免费供给。

所以我也了解了解世面,开始ios的基本ui事件的监听,页面的挑战的开发。

想写下来的内容包括:创建UIView,UIButton,并且在不同的UIViewController间的前跳,后跳。

以前知道的是,安卓下面的界面跳转,按钮添加是通过布局文件添加,然后findviewbyid函数来获取实例,

ios下面的开发虽然可以使用interface builder,但是我还是不习惯那种所谓的mvc的开发。完全不是代码控制的感觉。。。

看看网上人的说法,果断抛弃xib,stroyxxx什么的。。纯代码的构建界面。

首先是AppDelegate文件,(程序各种初始化以后就开始调用AppDelegate.m文件,相当于程序入口):

 1 #import <UIKit/UIKit.h>
 2 #import "MyViewController.h"
 3 
 4 @interface AppDelegate : UIResponder <UIApplicationDelegate>
 5 {
 6     
 7 }
 8 
 9 @property (strong, nonatomic) UIWindow *window;
10 @property (strong, nonatomic) MyViewController *viewController;
11 
12 @end

AppDelegate.m文件:

 1 //
 2 //  AppDelegate.m
 3 //  PhotoUpLoad
 4 //
 5 //  Created by ci123 on 14-4-4.
 6 //  Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
 7 //
 8 
 9 #import "AppDelegate.h"
10 
11 @implementation AppDelegate
12 
13 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
14 {
15     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
16     // Override point for customization after application launch.
17     self.viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
18     self.window.rootViewController = self.viewController;
19     [self.window makeKeyAndVisible];
20     return YES;
21 }
22 
23 - (void)applicationWillResignActive:(UIApplication *)application
24 {
25     // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
26     // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
27 }
28 
29 - (void)applicationDidEnterBackground:(UIApplication *)application
30 {
31     // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
32     // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
33 }
34 
35 - (void)applicationWillEnterForeground:(UIApplication *)application
36 {
37     // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
38 }
39 
40 - (void)applicationDidBecomeActive:(UIApplication *)application
41 {
42     // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
43 }
44 
45 - (void)applicationWillTerminate:(UIApplication *)application
46 {
47     // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
48 }
49 
50 @end

上面的代码所做的工作是:

当应用启动完毕后,调用函数,

创建了一个window,然后将自己定义的MyViewController作为window的rootViewController。

这样我们就可以看到MyViewController的内容啦~

然后是MyViewController,继承自ViewController(感觉这货跟安卓上的activity差不多。)

见代码:

MyViewController.h文件:

1 #import <UIKit/UIKit.h>
2 #import "PhotoViewController.h"
3 
4 @interface MyViewController : UIViewController
5 
6 @end

MyViewController.m文件:

 1 #import "MyViewController.h"
 2 
 3 @interface MyViewController ()
 4 
 5 @end
 6 
 7 @implementation MyViewController
 8 
 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
10 {
11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
12     if (self) {
13         // Custom initialization
14     }
15     return self;
16 }
17 
18 -(void) onPressClicked
19 {
20     PhotoViewController *viewController = [[PhotoViewController alloc] initWithNibName:nil bundle:nil];
21 
22     [self presentViewController:viewController animated:YES completion:^{
23         NSLog(@"Goto Next ViewController.");
24     }];
25 }
26 
27 - (void)viewDidLoad
28 {
29     [super viewDidLoad];
30 
31     // Do any additional setup after loading the view from its nib.
32     CGRect frame = CGRectMake(110, 200, 100, 20);
33     UIButton *pressButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
34     pressButton.backgroundColor = [UIColor clearColor];
35     [pressButton setTitle:@"Press Me  :-)" forState:UIControlStateNormal];
36     pressButton.frame = frame;
37     [pressButton addTarget:self action:@selector(onPressClicked) forControlEvents:UIControlEventTouchUpInside];
38     
39     UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(90, 120, 160, 20)];
40     lable.backgroundColor = [UIColor clearColor];
41     lable.text = @"UILable:Hello IOS!";
42     
43     UIView *t_view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen]bounds]];
44     t_view.backgroundColor = [UIColor whiteColor];
45     [self.view addSubview:t_view];
46     [self.view addSubview:pressButton];
47     [self.view addSubview:lable];
48 }
49 
50 - (void)didReceiveMemoryWarning
51 {
52     [super didReceiveMemoryWarning];
53     // Dispose of any resources that can be recreated.
54 }
55 
56 @end

上面的代码有UILable显示字体,有UIButton,并且添加了监听函数,执行跳转界面。

还设置了一个UIView作为底层的白色底,这样看起来更舒服一点。

然后就是怎么把这些控件添加到viewController的view上面了。可以看45-47行的代码:)

从代码中,可以看到如何给按钮加上监听函数,如何创建界面,执行跳转。

看看我们下一个跳转界面都有什么~

上代码:

PhotoViewController.h文件:

1 #import <UIKit/UIKit.h>
2 
3 @interface PhotoViewController : UIViewController
4 
5 @end

PhotoViewController.m文件:

 1 //
 2 //  PhotoViewController.m
 3 //  PhotoUpLoad
 4 //
 5 //  Created by vokie on 14-4-4.
 6 //  Copyright (c) 2014年 vokie. All rights reserved.
 7 //
 8 
 9 #import "PhotoViewController.h"
10 
11 @interface PhotoViewController ()
12 
13 @end
14 
15 @implementation PhotoViewController
16 
17 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
18 {
19     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
20     if (self) {
21         // Custom initialization
22     }
23     return self;
24 }
25 
26 - (void)onBackClicked
27 {
28     [self dismissViewControllerAnimated:YES completion:^{
29         NSLog(@"Goto back!~!!");
30     }];
31 }
32 
33 - (void)viewDidLoad
34 {
35     [super viewDidLoad];
36     // Do any additional setup after loading the view from its nib.
37     UIImage *defaultPhoto = [UIImage imageNamed:@"main_bg.jpg"];
38     UIImageView *imageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
39     [imageView setImage:defaultPhoto];
40     [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
41     imageView.contentMode = UIViewContentModeScaleAspectFill;
42     imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
43     imageView.clipsToBounds = YES;
44     
45     CGRect frame = CGRectMake(0, 450, 130, 40);
46     UIButton *backButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
47     backButton.backgroundColor = [UIColor grayColor];
48     [backButton setTitle:@"Press Me  :-(" forState:UIControlStateNormal];
49     backButton.frame = frame;
50     [backButton addTarget:self action:@selector(onBackClicked) forControlEvents:UIControlEventTouchUpInside];
51     
52     UIButton *takePhotoButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
53     takePhotoButton.backgroundColor = [UIColor redColor];
54     [takePhotoButton setTitle:@"拍照" forState:UIControlStateNormal];
55     takePhotoButton.frame = CGRectMake(130, 450, 80, 40);
56     
57     UIButton *fileChoose = [UIButton buttonWithType:UIButtonTypeRoundedRect];
58     fileChoose.backgroundColor = [UIColor greenColor];
59     [fileChoose setTitle:@"文件选择" forState:UIControlStateNormal];
60     fileChoose.frame = CGRectMake(210, 450, 120, 40);
61     
62     [self.view addSubview:imageView];
63     [self.view addSubview:backButton];
64     [self.view addSubview:takePhotoButton];
65     [self.view addSubview:fileChoose];
66 }
67 
68 - (void)didReceiveMemoryWarning
69 {
70     [super didReceiveMemoryWarning];
71     // Dispose of any resources that can be recreated.
72 }
73 
74 @end

第二个界面跟第一个界面差不多,只是多了一个背景图片层,图片的位置怎么放呢,我捣鼓了一下,直接在我项目下面创建了一个Resources的文件夹,

然后把我需要的图片放进去,然后在xcode中右击项目,add files。。

然后就可以直接通过图片名称来引用了。jpg,png都可以加载。

然后把然后就是怎么向前一个界面跳转呢?

dissmiss。。。

既然是入门,继续上项目的结构图:

好~上几张图片。

--------------------next--------------

原文地址:https://www.cnblogs.com/vokie/p/3645507.html