IOS开发笔记(六)对iOS多视图开发的初步了解

今天看了一下iOS开发中多个视图的应用.

iOS下包含了几种不同种类的视图类型和controller:比如Tab Bar ,Navigation Bar ,Tool Bar等.也可以自定义自己的视图的controller

程序中主窗口的视图控制器我们成为root controller,由它负责不同视图的切换等功能.

由root controller负责的视图都有自己的controller和delegate,比如一个tab bar,当用户在tab bar上点击的时候,是由tab bar的controller负责处理,而当用户在内容界面点击的时候,是由内容视图的controller负责处理的.

书中的例子很简单,点击tab bar中的按扭,在两个背景颜色(一蓝一黄)的视图中切换,两个视图中各有一个button

书中的例子建立的步骤如下:

1.建立一个Window base application ,没有view controller,只有一个window

2.添加视图文件

2.1 新建文件,选择cocoa touch下的UIViewController subclass 不使用xib文件(书中这里是xcode4.2以前的版本) 保存为SwitchViewController.h 和 SwitchViewController.m.这就是root controller

2.2 按照步骤2.1,创建蓝,黄背景视图的类文件 BlueViewController & YellowViewController

3.添加xib文件

3.1 选择cocoa touch下user interface下的View XIB的文件

3.2 新建两个视图的xib文件,BlueView.xib & YellowView.xib

4.在AppDelegate中添加一个IBOutlet,

@property (nonatomic, retain) IBOutlet SwitchViewController   *switchViewController;

5.为了将主视图SwitchViewController和Window关联,需要使用addSubview,添加了以下代码:

#import "View_SwitcherAppDelegate.h"
#import "SwitchViewController.h"
@implementation View_SwitcherAppDelegate
@synthesize window;
@synthesize switchViewController;
- (void) application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    // Override point for customization after application launch
   [self.window addSubview:switchViewController.view];
    [self.window makeKeyAndVisible];
    return YES; //注:书中代码这样写的,返回YES 但是函数类型是void.经查SDK,发现函数类型是BOOL
}
- (void)dealloc {
    [window release];
   [switchViewController release];
    [super dealloc];
}

6. 为了切换两个view,我们在SwitchViewController里添加两个View的指针,不定义IBOutlet,同时定义方法switchViews

#import <UIKit/UIKit.h>
@class YellowViewController;
@class BlueViewController;
@interface SwitchViewController : UIViewController {
}
@property (retain, nonatomic) YellowViewController *yellowViewController;
@property (retain, nonatomic) BlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
@end

 

7.代码架构好了,开始在IB中操作

7.1 在Library中拖动一个View Controller到Window上面

7.2 该View是UIViewController,在Identity Inspector中修改类名为UIViewController

7.3 新建Toolbar的View,拖放一个View到7.1添加的View上面,替换原有的View(注:为何是替换呢?)

7.4 拖动一个Toolbar到7.3新建的View中,选中Toolbar后,点击Button,链接到SwitchViewController的方法switchView中

 

8. 在IB中将AppDelegate中的IBOutlet switchViewController与类SwitchViewController链接

9. 修改SwitchViewController.m

主要就是从Nib加载ViewController,不用的释放Controller

刚开始只加载BlueView,因为YellowView可能用户不会选择,等到切换的时候才加载(Lazy Loading)

#import "SwitchViewController.h"
#import "YellowViewController.h"
#import "BlueViewController.h"
@implementation SwitchViewController
@synthesize yellowViewController;
@synthesize blueViewController;
- (void)viewDidLoad
{
    BlueViewController *blueController = [[BlueViewController alloc]
              initWithNibName:@"BlueView" bundle:nil];
    self.blueViewController = blueController;
   [self.view insertSubview:blueController.view atIndex:0];
    [blueController release];
    [super viewDidLoad];
}
- (IBAction)switchViews:(id)sender
{
    if (self.yellowViewController.view.superview == nil)
   {
        if (self.yellowViewController == nil)
        {
            YellowViewController *yellowController =
           [[YellowViewController alloc] initWithNibName:@"YellowView"
                                                   bundle:nil];
            self.yellowViewController = yellowController;

            [yellowController release];
        }
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview:yellowViewController.view atIndex:0];
   }
   else
   {
      if (self.blueViewController == nil)
      {
          BlueViewController *blueController =
          [[BlueViewController alloc] initWithNibName:@"BlueView"
                                               bundle:nil];
          self.blueViewController = blueController;
          [blueController release];
      }
      [yellowViewController.view removeFromSuperview];
      [self.view insertSubview:blueViewController.view atIndex:0];
   }
}

Releases the view if it doesn't have a superview

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc, that aren't in use
    if (self.blueViewController.view.superview == nil)
        self.blueViewController = nil;
    else
       self.yellowViewController = nil;
}

 

- (void)dealloc {
    [yellowViewController release];
    [blueViewController release];
    [super dealloc];
}
@end

 

10.完善两个内容视图,添加button,弹出不同的内容提示.

#import <UIKit/UIKit.h>
@interface BlueViewController : UIViewController {
}
- (IBAction)blueButtonPressed;
@end

在实现中加入UIAlertView即可.(略去)

打开BlueView.nib,在Indentity Inspector中选择class name为BlueViewController,表明从nib从BlueViewController

然后修改View的背景色,更改View的位置在属性页的

Simulated User Interface Elements选项下

 

11.加入视图切换动画

- (IBAction)switchViews:(id)sender
{
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration:1.25];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

.....

 

if(..)

{

        [UIView setAnimationTransition:
         UIViewAnimationTransitionFlipFromRight
                               forView:self.view cache:YES];
        [blueViewController viewWillAppear:YES];
        [yellowViewController viewWillDisappear:YES];

        … …

}

[UIView commitAnimations];

}

--------------------- 我可以操纵冰冷的代码,但我操纵不了我的人生...... [url=http://www.puya360.com]西安普雅软件工作室[/url]
原文地址:https://www.cnblogs.com/ternastone/p/2243048.html