你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS Dev (14) NavigationController基礎示例

iOS Dev (14) NavigationController基礎示例

編輯:IOS開發綜合

iOS Dev (14) NavigationController基礎示例

  •  
  • AppDelegate

    .h

    #import 
    
    @class ViewController
    
    @interface AppDelegate: UIResponder 
    
    @property  UIWindow *window;
    @property  ViewController *vc;
    
    @end
    

    .m

    #import AppDelegate.h
    #import ViewController.h
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.vc = [[ViewController alloc] initWithNibName:nil bundle:nil];
        self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:self.vc]
        [self.window makeKeyAndVisible];
        return YES;
    }
    
    @end
    

    主視圖

    ViewController.h

    #import 
    
    @interface ViewController: UIViewController
    
    @end
    

    ViewController.m,注意看 pushMethod 和 presentMethod。注意後者與前者完全不同哦,前者是 self.navigationController 相關的方法,後者是 self 自己的(也就是 UIViewController 的)。注意後者有一個 completion 參數後,如果啥也不想寫就用 ^{} 來代替。

    #import ViewController.h
    #import LLViewController.h
    #import MMViewController.h
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        UIButton *pushButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
        [pushButton setTitle:@Push forState:UIControlStateNormal];
        [pushButton setBackgroundColor:[UIColor redColor]];
        [pushButton addTarget:self action:@selector(pushMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubView:pushButton];
    
        UIButton *presentButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
        [presentButton setTitle:@present forState:UIControlStateNormal];
        [presentButton setBackgroundColor:[UIColor redColor]];
        [presentButton addTarget:self action:@selector(presentMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubView:presentButton];
    }
    
    - (void)didReceiveMemoryWarning
    {
    }
    
    - (void)pushMethod:(UIButton *)button
    {
        [self.navigationController pushViewController:[[LLViewController alloc] init] animated:YES];
    }
    
    - (void)presentMethod:(UIButton *)button
    {
        [self presentViewController:[[MMViewController alloc] init] animated:YES completion:^{}]
    }
    
    @end
    

    另外兩個視圖

    上面演示的是從主視圖切換到另外兩個視圖,一個是LLViewController,一個是MMViewController。前者是從右向左移入的,後者是從下向上移入的。

    LLViewController.h

    #import 
    
    @interface LLViewController: UIViewController
    @end
    

    LLViewController.m,注意看 popMethod。

    #import LLViewController.h
    
    @interface LLViewController()
    @end
    
    @implementation LLViewController
    
    - (void)didViewLoad
    {
        [super didViewLoad];
    
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 200, 100, 100)];
        [button setTitle:@Pop forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(popMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubView:button];
    }
    
    - (void)didReceiveMemoryWarning
    {
    }
    
    - (void)popMethod
    {
        [self.nagivationController popViewControllerAnimated:YES];
    }
    
    @end
    

    MMViewController.h

    #import 
    
    @interface MMViewController: UIViewController
    @end
    

    MMViewController.m 主要看 dismissMethod。注意這個方法與 presentMethod 是類似的,而與 pop 是完全不同的。

    #import MMViewController.h
    
    @interface MMViewController()
    @end
    
    @implementation MMViewController
    
    - (void)didViewLoad
    {
        [super didViewLoad];
    
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 400, 100, 100)];
        [button setTitle:@Dismiss forState:UIControlStateNormal];
        [button setBackgroundColor:[UIColor redColor]];
        [button addTarget:self action:@selector(dismissMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubView:button];
    }
    
    - (void)didReceiveMemoryWarning
    {
    }
    
    - (void)dismissMethod
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }
    
    @end
    

    轉載請注明來自:http://blog.csdn.net/prevention

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved