XCode 4.2开发笔记图文(3)---多视图开发的初步了解

今天看了一下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];

}

 

昨天参照书中的例子,回到家后开始实现示例中的代码.

发现XCode4.2中没有Window Base Application这一选项.使用Empty Application没有主界面的Storyboard文件.

于是打开google,发现Jeroen Trappers有一篇文章有详细的解决方法.

地址:http://www.trappers.tk/site/2011/06/16/mainwindow-xib/

 

我的看法是(不一定准确):AppDelegate是个代理类,起类似与window编程下的WNDPROC函数.

与其配合的是UIApplication.

在没有XIB文件的情况下,是通过程序的方式加载UIApplication的一个实例完成框架的建立的.

为了从XIB文件中加载,我们就要自己创建一个XIB文件,这是首先的问题.

如何将XIB文件和AppDelegate的类关联是下面的问题.

 

我们知道,AppDelegate需要一个UIApplition实例,这个事例保存所有XIB上元素的拷贝.所以File's Owner的类名我们要改为UIApplicaion

同时UIApplicaion有一个插座IBOutlet刚好是我们AppDelegate可以对接的.类型UIApplicationDelegate

于是我们首先拖放一个Object,修改类名为我们的xAppDelegate,这样就可以将这个Object和UIApplication中的delegate链接了.

delegate

The delegate of the application object.

@property(nonatomic, assign) id<UIApplicationDelegate> delegate

Discussion

The delegate must adopt the UIApplicationDelegate formal protocol. UIApplication assigns and does not retain the delegate.

Availability
  • Available in iOS 2.0 and later.
Declared In
UIApplication.h

 

源文件中有UIWindow的变量 window ,我们需要在上面添加的Object(类名xAppDelegate)里面创建一个Window,然后把变量加上IBOutlet后进行链接.

这样就完成了XIB中AppDelegate和它下面Window的两个对象从XIB到代码之间的链接.

 

可是此时程序的入口并没有改变,文章中推荐的方式是在工程配置中的Main Interface修改为一个XIB文件名,其实就是在程序中加载XIB文件作为入口.

同样的功能,也许我们在代码中也可以实现.类似与initWithNibName的方法,暂时我没有实现.

 

还有就是注释掉了一个初始化的函数:- (BOOL) application:didFinishLaunchingWithOptions: 这个方法中的操作是跟我们从XIB加载相冲突的.

 

通过以上几个步骤,我们就可以从Empty Application中自定义的加载一个XIB文件了.

学习到这里,让我对整个XIB的工作方式和UIApplication的工作原理又有了更深一步的了解.

对于以后的多视图学习非常的有帮助.

 

以上操作的详细步骤如下,顺便复习一下:

1.创建Empty application,此时项目中只有一个AppDelegate的类

Start with Empty Application template

2.新建文件,选择User Interface下的Empty 命名为MainWindow

Add Empty Interface Builder document

3.打开新建的MainWindow.xib文件

The empty design surface of MainWindow.xib

 

4.将File's Owner的类名修改为UIApplication

Change class of File

5. 在Library中拖放一个Object到图下的位置

Add Object to the document

6.将该object的类名修改为文件中AppDelegate的类名(同时还可以给object命名下Label)

Change class of the object to xAppDelegate

7.拖放一个Window到左边

Add a window to the document

8. 在xAppDelegate的h文件中,给window的属性加上IBOutlet

@interface DemoAppDelegate :
      UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) IBOutlet UIWindow *window;
@end

9.将File's Owner 和 拖放的Object按照以下方式连接

Link the application delegate

 

Link the window outlet of the app delegate

 

10.在项目属性中,把Main Interface修改成你的xib文件名MainWindow

Set the Main Interface to MainWindow

11. 在xAppDelegate.m文件中,将

- (BOOL) application:didFinishLaunchingWithOptions:

 

这个方法全部注释掉

 

以上在Lion 10.7.2 XCode4.2中测试通过.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值