的界面跳转

在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面:如下代码

if (self.rootViewController.view.superview == nil) {

[singleDollController.view removeFromSuperview];

[self.view insertSubview:rootViewController.view atIndex:0];

}

else {

[rootViewController.view removeFromSuperview];

[self.view insertSubview:singleDollController.view atIndex:0];

}

使用这种方式无法实现界面跳转时的动画效果。

另一中方式为将跳转的界面的Controller放入到UINavigationController中,使用push或pop实现跳转:使用这种方式可用实现动画效果

navController = [[UINavigationController alloc]init];

[navController setNavigationBarHidden:YES];

[window addSubview:navController.view];

rootView = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];

[navController pushViewController:rootView animated:NO];

 

///

self.singleDollView = view;

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.5];

[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft forView:self.navController.view cache:NO];

[self.navController pushViewController:self.singleDollView animated:NO];

[UIView commitAnimations];

 

1 创建一个基于Navigation-based Application的iphone工程,为什么要创建基于Navigation-based Application的工程呢,因为这样系统就会自动将Navigation视图加到我们的窗口视图中,这样我们就不用自己手动去加,并且可以用

[self.navigationControllerpushViewController:otherviewanimated:YES]去跳转页面。当设置每个页面的标题后会在页面左上角自动生成后退导航按钮,多方便呀,当然需要在建立后将xib文件里面的tableview去掉加入view。

2 新建一个页面,我这里是OtherView,让系统自动生成.h,.xib文件。

3 第一个页面上面添加一个文本筐和一个按钮,点击按钮后调转到第二个页面并将文本筐里面的数据传入第二个页面。第二个页面用一个label来显示传过来的数据。

4 代码:

 首先是自动生成的协议里面的代码,注意看navigationController

 

MynavAppDelegate.h代码:

 

  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface MynavAppDelegate : NSObject <UIApplicationDelegate> {  
  4.   
  5. }  
  6.   
  7. @property (nonatomic, retain) IBOutlet UIWindow *window;  
  8.   
  9. @property (nonatomic, retain) IBOutlet UINavigationController *navigationController;  
  10.   
  11. @end  
#import <UIKit/UIKit.h>

@interface MynavAppDelegate : NSObject <UIApplicationDelegate> {

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end


 

MynavAppDelegate.m代码:

  1. #import "MynavAppDelegate.h"   
  2.   
  3. @implementation MynavAppDelegate  
  4.   
  5.   
  6. @synthesize window=_window;  
  7.   
  8. @synthesize navigationController=_navigationController;  
  9.   
  10. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  11. {  
  12.     self.window.rootViewController = self.navigationController;  
  13.     [self.window makeKeyAndVisible];  
  14.     return YES;  
  15. }  
  16.   
  17. - (void)applicationWillResignActive:(UIApplication *)application  
  18. {  
  19.      
  20. }  
  21.   
  22. - (void)applicationDidEnterBackground:(UIApplication *)application  
  23. {  
  24.      
  25. }  
  26.   
  27. - (void)applicationWillEnterForeground:(UIApplication *)application  
  28. {  
  29.       
  30. }  
  31.   
  32. - (void)applicationDidBecomeActive:(UIApplication *)application  
  33. {  
  34.      
  35. }  
  36.   
  37. - (void)applicationWillTerminate:(UIApplication *)application  
  38. {  
  39.       
  40. }  
  41.   
  42. - (void)dealloc  
  43. {  
  44.     [_window release];  
  45.     [_navigationController release];  
  46.     [super dealloc];  
  47. }  
  48.   
  49. @end  
#import "MynavAppDelegate.h"

@implementation MynavAppDelegate


@synthesize window=_window;

@synthesize navigationController=_navigationController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
   
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
   
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
   
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    
}

- (void)dealloc
{
    [_window release];
    [_navigationController release];
    [super dealloc];
}

@end


第一个页面的代码:

RootViewController.h

  1. #import <UIKit/UIKit.h>   
  2. #import <Foundation/Foundation.h>   
  3.   
  4. @interface RootViewController : UIViewController {  
  5.     IBOutlet UITextField * message;//需要传出的数据   
  6. }  
  7.   
  8. @property(nonatomic,retain) UITextField * message;  
  9.   
  10. -(IBAction) send;//按钮点击方法   
  11.   
  12.   
  13. @end  
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface RootViewController : UIViewController {
    IBOutlet UITextField * message;//需要传出的数据
}

@property(nonatomic,retain) UITextField * message;

-(IBAction) send;//按钮点击方法


@end


RootViewController.m

  1. #import "RootViewController.h"   
  2. #import "OtherView.h"   
  3.   
  4. @implementation RootViewController  
  5.   
  6. @synthesize message;  
  7.   
  8.   
  9. -(IBAction) send{  
  10.     OtherView  *otherview = [[OtherView alloc] initWithNibName:@"OtherView" bundle:nil];  
  11.       
  12.     otherview.mystring= message.text;  
  13.       
  14.     [self.navigationController pushViewController:otherview animated:YES];  
  15.       
  16.     [otherview release];  
  17. }  
  18.   
  19.   
  20. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {  
  21.       
  22.     UITouch *touch = [[event allTouches] anyObject];  
  23.       
  24.     if (touch.tapCount >= 1) {  
  25.           
  26.         [message resignFirstResponder];  
  27.           
  28.     }  
  29. }  
  30.   
  31. - (void)viewDidLoad  
  32. {  
  33.     self.title = @"第一个页面";  
  34.     [super viewDidLoad];  
  35. }  
  36.   
  37.   
  38. - (void)didReceiveMemoryWarning  
  39. {  
  40.     // Releases the view if it doesn't have a superview.   
  41.     [super didReceiveMemoryWarning];  
  42.       
  43.     // Relinquish ownership any cached data, images, etc that aren't in use.   
  44. }  
  45.   
  46. - (void)viewDidUnload  
  47. {  
  48.     [super viewDidUnload];  
  49.   
  50.     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.   
  51.     // For example: self.myOutlet = nil;   
  52. }  
  53.   
  54. - (void)dealloc  
  55. {  
  56.     [message release];  
  57.     [super dealloc];  
  58. }  
  59.   
  60. @end  
#import "RootViewController.h"
#import "OtherView.h"

@implementation RootViewController

@synthesize message;


-(IBAction) send{
    OtherView  *otherview = [[OtherView alloc] initWithNibName:@"OtherView" bundle:nil];
    
    otherview.mystring= message.text;
    
    [self.navigationController pushViewController:otherview animated:YES];
    
    [otherview release];
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	UITouch *touch = [[event allTouches] anyObject];
	
	if (touch.tapCount >= 1) {
		
		[message resignFirstResponder];
		
	}
}

- (void)viewDidLoad
{
    self.title = @"第一个页面";
    [super viewDidLoad];
}


- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}

- (void)dealloc
{
    [message release];
    [super dealloc];
}

@end


第二个页面代码:

OtherView.h

  1. #import <UIKit/UIKit.h>   
  2.   
  3.   
  4. @interface OtherView : UIViewController {  
  5.     IBOutlet UILabel * mylabel;//用来显示传入的数据   
  6.     NSString * mystring;//数据传入用到的属性   
  7. }  
  8.   
  9. @property(nonatomic,retain) UILabel * mylabel;  
  10. @property(nonatomic,retain) NSString * mystring;  
  11.   
  12.   
  13. @end  
#import <UIKit/UIKit.h>


@interface OtherView : UIViewController {
    IBOutlet UILabel * mylabel;//用来显示传入的数据
    NSString * mystring;//数据传入用到的属性
}

@property(nonatomic,retain) UILabel * mylabel;
@property(nonatomic,retain) NSString * mystring;


@end

OtherView.m

  1. #import "OtherView.h"   
  2.   
  3.   
  4. @implementation OtherView  
  5.   
  6. @synthesize mylabel,mystring;  
  7.   
  8.   
  9.   
  10. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  11. {  
  12.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  13.     if (self) {  
  14.         // Custom initialization   
  15.     }  
  16.     return self;  
  17. }  
  18.   
  19. - (void)dealloc  
  20. {  
  21.     [mylabel release];  
  22.     [mystring release];  
  23.     [super dealloc];  
  24. }  
  25.   
  26. - (void)didReceiveMemoryWarning  
  27. {  
  28.     // Releases the view if it doesn't have a superview.   
  29.     [super didReceiveMemoryWarning];  
  30.       
  31.     // Release any cached data, images, etc that aren't in use.   
  32. }  
  33.   
  34. #pragma mark - View lifecycle   
  35.   
  36. - (void)viewDidLoad  
  37. {  
  38.      
  39.     [super viewDidLoad];  
  40.      self.title = @"第二个页面";  
  41.     self.mylabel.text=mystring;  
  42.    // mylabel.text = mystring;   
  43.     // Do any additional setup after loading the view from its nib.   
  44. }  
  45.   
  46. - (void)viewDidUnload  
  47. {  
  48.     [super viewDidUnload];  
  49.     // Release any retained subviews of the main view.   
  50.     // e.g. self.myOutlet = nil;   
  51. }  
  52.   
  53. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation  
  54. {  
  55.     // Return YES for supported orientations   
  56.     return (interfaceOrientation == UIInterfaceOrientationPortrait);  
  57. }  
  58.   
  59. @end  
#import "OtherView.h"


@implementation OtherView

@synthesize mylabel,mystring;



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

- (void)dealloc
{
    [mylabel release];
    [mystring release];
    [super dealloc];
}

- (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];
     self.title = @"第二个页面";
    self.mylabel.text=mystring;
   // mylabel.text = mystring;
    // 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);
}

@end





 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值