代码改变世界

IOS 中的页面跳转(navigaitonController)+带自动返回

2015-09-07 22:26  一树一菩提  阅读(839)  评论(0编辑  收藏  举报

页面1                                                                      跳至                                  页面2

        

 

代码如下:

 

1.AppDelegate.h

 

 

#import <UIKit/UIKit.h>

 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

 

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic)UINavigationController *navigationController;

 

@end

 

2. AppDelegate.m

 

#import "AppDelegate.h"

#import "FirstViewController.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    UIWindow *window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];

    FirstViewController *viewController = [[FirstViewController alloc]initWithNibName:@"FirstViewController" bundle:nil];

        _navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];

        [self.window addSubview:_navigationController.view];

        self.window.rootViewController = _navigationController;

//    self.window.rootViewController = viewController;

    [self.window makeKeyAndVisible];

    return YES;    return YES;

}

 

3. FirstViewController.h

 

#import <UIKit/UIKit.h>

 

 

@interface FirstViewController : UIViewController{

    

}

 

- (IBAction)pressToSecond:(id)sender;

 

@end

 

4.FirstViewController.m

 

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "AppDelegate.h"

 

@interface FirstViewController ()

 

@end

 

@implementation FirstViewController

 

- (void)viewDidLoad {

    [super viewDidLoad];

//    [self.next addTarget:self action:@selector(pressToSecond:) forControlEvents:UIControlEventTouchUpInside];

}

 

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

 

- (IBAction)pressToSecond:(id)sender {

    SecondViewController *secondView = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];

    

//        页面跳转:

//        方法1:(传不了值,不知道为什么???)

        AppDelegate *myDelegate = [[UIApplication sharedApplication]delegate];

        [myDelegate.navigationController pushViewController:secondView animated:YES];

    

    

}

@end

这样就可以有上面的效果了,但是如果secondViewController里设置此controller的navigationController的

navigationBar就不会自动显示返回按钮了,需要在secondViewController里设置一下。

-(void)viewDidAppear:(BOOL)animated{

    [super viewDidAppear:animated];

    AppDelegate *myDelegate = [[UIApplication sharedApplication]delegate];

    //    myDelegate.navigationController.navigationBarHidden = YES;

    self.navigationController.navigationBarHidden = YES;

}