生活随笔
收集整理的這篇文章主要介紹了
的界面跳转
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在界面的跳轉(zhuǎn)有兩種方法,一種方法是先刪除原來的界面,然后在插入新的界面:如下代碼
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];
}
使用這種方式無法實(shí)現(xiàn)界面跳轉(zhuǎn)時(shí)的動(dòng)畫效果。
另一中方式為將跳轉(zhuǎn)的界面的Controller放入到UINavigationController中,使用push或pop實(shí)現(xiàn)跳轉(zhuǎn):使用這種方式可用實(shí)現(xiàn)動(dòng)畫效果
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 創(chuàng)建一個(gè)基于Navigation-based Application的iphone工程,為什么要?jiǎng)?chuàng)建基于Navigation-based Application的工程呢,因?yàn)檫@樣系統(tǒng)就會(huì)自動(dòng)將Navigation視圖加到我們的窗口視圖中,這樣我們就不用自己手動(dòng)去加,并且可以用
[ self . navigationController pushViewController :otherview animated : YES ]去跳轉(zhuǎn)頁面。當(dāng)設(shè)置每個(gè)頁面的標(biāo)題后會(huì)在頁面左上角自動(dòng)生成后退導(dǎo)航按鈕,多方便呀,當(dāng)然需要在建立后將xib文件里面的tableview去掉加入view。
2 新建一個(gè)頁面,我這里是OtherView,讓系統(tǒng)自動(dòng)生成.h,.xib文件。
3 第一個(gè)頁面上面添加一個(gè)文本筐和一個(gè)按鈕,點(diǎn)擊按鈕后調(diào)轉(zhuǎn)到第二個(gè)頁面并將文本筐里面的數(shù)據(jù)傳入第二個(gè)頁面。第二個(gè)頁面用一個(gè)label來顯示傳過來的數(shù)據(jù)。
4 代碼:
?首先是自動(dòng)生成的協(xié)議里面的代碼,注意看navigationController :
?
MynavAppDelegate.h代碼:
?
[html] view plaincopyprint?
#import?< UIKit /UIKit.h> ?? ?? @interface?MynavAppDelegate?:?NSObject?< UIApplicationDelegate > ?{?? ?? }?? ?? @property?(nonatomic,?retain)?IBOutlet?UIWindow?*window;?? ?? @property?(nonatomic,?retain)?IBOutlet?UINavigationController?*navigationController;?? ?? @end??
#import <UIKit/UIKit.h>@interface MynavAppDelegate : NSObject <UIApplicationDelegate> {}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;@end
?
MynavAppDelegate.m代碼:
[cpp] view plaincopyprint?
#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??
#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
第一個(gè)頁面的代碼:
RootViewController.h
[cpp] view plaincopyprint?
#import?<UIKit/UIKit.h> ??#import?<Foundation/Foundation.h> ???? @interface?RootViewController?:?UIViewController?{?? ????IBOutlet?UITextField?*?message;?? }?? ?? @property(nonatomic,retain)?UITextField?*?message;?? ?? -(IBAction)?send;?? ?? ?? @end??
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>@interface RootViewController : UIViewController {IBOutlet UITextField * message;//需要傳出的數(shù)據(jù)
}@property(nonatomic,retain) UITextField * message;-(IBAction) send;//按鈕點(diǎn)擊方法@end
RootViewController.m
[cpp] view plaincopyprint?
#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?=?@"第一個(gè)頁面" ;?? ????[super?viewDidLoad];?? }?? ?? ?? -?(void )didReceiveMemoryWarning?? {?? ?????? ????[super?didReceiveMemoryWarning];?? ?????? ?????? }?? ?? -?(void )viewDidUnload?? {?? ????[super?viewDidUnload];?? ?? ?????? ?????? }?? ?? -?(void )dealloc?? {?? ????[message?release];?? ????[super?dealloc];?? }?? ?? @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 = @"第一個(gè)頁面";[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
第二個(gè)頁面代碼:
OtherView.h
[cpp] view plaincopyprint?
#import?<UIKit/UIKit.h> ???? ?? @interface?OtherView?:?UIViewController?{?? ????IBOutlet?UILabel?*?mylabel;?? ????NSString?*?mystring;?? }?? ?? @property(nonatomic,retain)?UILabel?*?mylabel;?? @property(nonatomic,retain)?NSString?*?mystring;?? ?? ?? @end??
#import <UIKit/UIKit.h>@interface OtherView : UIViewController {IBOutlet UILabel * mylabel;//用來顯示傳入的數(shù)據(jù)NSString * mystring;//數(shù)據(jù)傳入用到的屬性
}@property(nonatomic,retain) UILabel * mylabel;
@property(nonatomic,retain) NSString * mystring;@end
OtherView.m
[cpp] view plaincopyprint?
#import?"OtherView.h" ???? ?? @implementation?OtherView?? ?? @synthesize?mylabel,mystring;?? ?? ?? ?? -?(id)initWithNibName:(NSString?*)nibNameOrNil?bundle:(NSBundle?*)nibBundleOrNil?? {?? ????self?=?[super?initWithNibName:nibNameOrNil?bundle:nibBundleOrNil];?? ????if ?(self)?{?? ?????????? ????}?? ????return ?self;?? }?? ?? -?(void )dealloc?? {?? ????[mylabel?release];?? ????[mystring?release];?? ????[super?dealloc];?? }?? ?? -?(void )didReceiveMemoryWarning?? {?? ?????? ????[super?didReceiveMemoryWarning];?? ?????? ?????? }?? ?? #pragma?mark?-?View?lifecycle ???? -?(void )viewDidLoad?? {?? ????? ????[super?viewDidLoad];?? ?????self.title?=?@"第二個(gè)頁面" ;?? ????self.mylabel.text=mystring;?? ????? ?????? }?? ?? -?(void )viewDidUnload?? {?? ????[super?viewDidUnload];?? ?????? ?????? }?? ?? -?(BOOL )shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation?? {?? ?????? ????return ?(interfaceOrientation?==?UIInterfaceOrientationPortrait);?? }?? ?? @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 = @"第二個(gè)頁面";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 orientationsreturn (interfaceOrientation == UIInterfaceOrientationPortrait);
}@end
?
總結(jié)
以上是生活随笔 為你收集整理的的界面跳转 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。