IOS开发基础教程学习笔记1(第6章)多视图应用程序

本文介绍了一个iOS应用中视图控制器之间的切换方法,并演示了如何通过UIView动画实现平滑过渡。此外,还展示了如何在两个不同的视图控制器中添加按钮并响应UIAlertView。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工程目录结构如下图所示(ios5 ARC + Xcode4.4):

1。添加BlueViewController

2。添加YellowViewController

3。ViewController.xib中添加一个按钮 ,按钮标题为:change views,

ViewController.h中添加代码

#import <UIKit/UIKit.h>

@class BlueViewController;
@class YellowViewController;

@interface ViewController : UIViewController
{
    BlueViewController *blueViewController;
    YellowViewController *yellowViewController;
}

@property (strong, nonatomic) BlueViewController *blueViewController;
@property (strong, nonatomic) YellowViewController *yellowViewController;

- (IBAction)showBlueView:(UIButton *)sender;

@end

4。在ViewController.m中添加代码

//
//  ViewController.m
//  test
//
//  Created by  on 12-9-13.
//  Copyright (c) 2012年 All rights reserved.
//

#import "ViewController.h"
#import "BlueViewController.h"
#import "YellowViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize blueViewController;
@synthesize yellowViewController;

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    //BlueViewController *blueController = [[BlueViewController alloc] initWithNibName: @"BlueViewController" bundle: nil];
    //self.blueViewController = blueController;
    //[self.view insertSubview: blueController.view atIndex: 0];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)showBlueView:(UIButton *)sender
{
    [UIView beginAnimations: @"view flip" context: nil];
    [UIView setAnimationDuration: 1.25];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    
    if (self.yellowViewController.view.superview == nil)
    {
        if (self.yellowViewController == nil) {
            YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName: @"YellowViewController" bundle: nil];
            self.yellowViewController = yellowController;
        }
        
        [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView: self.view cache: YES];
        [yellowViewController viewWillAppear: YES];
        [blueViewController viewWillDisappear: YES];
        
        [blueViewController.view removeFromSuperview];
        [self.view insertSubview: yellowViewController.view atIndex: 0];
        
        [blueViewController viewDidDisappear: YES];
        [yellowViewController viewDidAppear: YES];
    }
    else
    {
        if (self.blueViewController == nil)
        {
            BlueViewController *blueController = [[BlueViewController alloc] initWithNibName: @"BlueViewController" bundle: nil];
            self.blueViewController = blueController;
        }
        
        [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView: self.view cache: YES];
        [blueViewController viewWillAppear: YES];
        [yellowViewController viewWillDisappear: YES];
        
        [yellowViewController.view removeFromSuperview];
        [self.view insertSubview: blueViewController.view atIndex: 0];
        
        [yellowViewController viewDidDisappear: YES];
        [blueViewController viewDidAppear: YES];
    }
    
    [UIView commitAnimations];
}

@end

5。BlueViewController.xib中添加一个按钮,响应UIAlertView。

BlueViewController.h中添加按钮函数声明

#import <UIKit/UIKit.h>

@interface BlueViewController : UIViewController

- (IBAction)blueButtonPressed:(UIButton *)sender;

@end

6。BlueViewController.m中添加函数实现

- (IBAction)blueButtonPressed:(UIButton *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"蓝色按钮标题" message: @"你点击了蓝色视图上的按钮" delegate: nil cancelButtonTitle: @"yep, I did." otherButtonTitles: nil, nil];
    [alert show];
}

7。YellowViewController.xib中添加一个按钮,响应UIAlertView。

YellowViewController.h中添加按钮函数声明

#import <UIKit/UIKit.h>

@interface YellowViewController : UIViewController

- (IBAction)yellowButtonPressed:(UIButton *)sender;

@end

8。YellowViewController.m中添加按钮函数实现

- (IBAction)yellowButtonPressed:(UIButton *)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"yellow title" message: @"message,.abc,asdf" delegate: nil cancelButtonTitle: @"ok" otherButtonTitles: nil, nil];
    [alert show];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值