在界面的跳转有两种方法,一种方法是先删除原来的界面,然后在插入新的界面:如下代码
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代码:
#import <UIKit/UIKit.h> @interface MynavAppDelegate : NSObject <UIApplicationDelegate> { } @property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UINavigationController *navigationController; @end
MynavAppDelegate.m代码:
#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
#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface RootViewController : UIViewController { IBOutlet UITextField * message;//需要传出的数据 } @property(nonatomic,retain) UITextField * message; -(IBAction) send;//按钮点击方法 @end
RootViewController.m
#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
#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
#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