• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Objective-C,ios,iphone开发基础:多个视图(view)之间的切换,以及视图之间传值。 ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

所有的视图都继承自 UIViewController,都使用 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;来初始化视图,所以一般初始化的数据或者变量都是在这个方法中。

首先建一个single view 工程,然后再添加一个view。

结构如图:

单击单开 cidpViewController.xib 文件,在视图上面拖放一个UIbutton 和一个 UILable,并且在cidpViewController.h文件中声明一个变量和一个方法,然后将UIbutton 和对应的方法连线, UILable和对应的变量连线,(这里要注意的是,必须将UIbutton的Touch up inside 和对应的方法连接 )。

SecondViewController.xib同上:

关键代码如下:

#pragma mark 支持试图切换的方法
#pragma mark -
-(IBAction)btnPreClick:(id)sender{
    //实例化一个SecondViewController 对象
    SecondViewController* second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    //控制试图加载的样式,就是一些效果,有四种可以选择
    second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //让实例化的试图对象加载在当前试图之上,
    [self presentModalViewController:second animated:YES];
    //页面传值。
    second.lblNext.text = lblPre.text;
    //释放开辟的空间,前提是将 arc关闭。
    [second release];
    
}


#pragma mark 试图消失的方法:
#pragma mark -
-(IBAction)btnBackClick:(id)sender{
    //让当前试图消失
    [self dismissModalViewControllerAnimated:YES];
}

整个代码如下:

//
//  cidpViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"
@interface cidpViewController : UIViewController{
    
    IBOutlet UILabel* lblPre;
}
@property (nonatomic,retain) UILabel* lblPre;
-(IBAction)btnPreClick:(id)sender;
@end

//
//  cidpViewController.m

#import "cidpViewController.h"

@implementation cidpViewController
@synthesize lblPre;
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark 支持试图切换的方法
#pragma mark -
-(IBAction)btnPreClick:(id)sender{
    //实例化一个SecondViewController 对象
    SecondViewController* second = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    //控制试图加载的样式,就是一些效果,有四种可以选择
    second.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    //让实例化的试图对象加载在当前试图之上,
    [self presentModalViewController:second animated:YES];
    //页面传值。
    second.lblNext.text = lblPre.text;
    //释放开辟的空间,前提是将 arc关闭。
    [second release];
    
}
@end

//
//  SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController{
    IBOutlet UILabel* lblNext;
    
}
@property (nonatomic ,retain) UILabel* lblNext;
-(IBAction)btnBackClick:(id)sender;
@end

//
//  SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController
@synthesize lblNext;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (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.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#pragma mark 试图消失的方法:
#pragma mark -
-(IBAction)btnBackClick:(id)sender{
    //让当前试图消失
    [self dismissModalViewControllerAnimated:YES];
}

@end

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap