zoukankan      html  css  js  c++  java
  • 导航控制器 控制视图间的切换

    委托下是一个UINavigationController导航控制器。三个视图的先后切换,分别为UITableViewController、UITableViewController、UIViewController。

     

    导航控制器的

    pushViewController:animated:

    Pushes a view controller onto the receiver’s stack and updates the display.

    - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated

    是push视图控制器

     

     

    1、创建一个空模板,在委托中建立一个导航控制器。

    在AppDelegate.h中添加:

    @property (strong, nonatomic) UINavigationController *myNaviController;

     

    AppDelegate.m文件修改如下:

    #import "AppDelegate.h"

    #import "RootViewController.h"

     

    @synthesize myNaviController;

     

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

    {

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

        // Override point for customization after application launch.

        

        RootViewController *rootView = [[RootViewController alloc] initWithStyle:UITableViewStyleGrouped];

        

        self.myNaviController = [[UINavigationController alloc] initWithRootViewController:rootView];

        

        [self.window addSubview:self.myNaviController.view];

        [self.window setRootViewController:self.myNaviController];

        

        self.window.backgroundColor = [UIColorwhiteColor];

        [self.window makeKeyAndVisible];

        returnYES;

    }

     

     

    2、创建一个子类,为UITableViewController的子类,名为RootViewController,不需要同时创建.xib。此为程序一开始看到的画面

    RootViewController.h中添加如下:

    @property (nonatomic, strong) NSMutableArray *mutableArrayForRootView;

     

    RootViewController.m中修改如下:

    #import "RootViewController.h"

    #import "SongViewController.h"

     

    @synthesize mutableArrayForRootView;

     

    - (void)viewDidLoad

    {

        [super viewDidLoad];

        

        [self setTitle:@"哈哈"];

        

        self.mutableArrayForRootView = [[NSMutableArray allocinit];

        

        SongViewController *mySongViewController = [[SongViewController alloc] initWithStyle:UITableViewStylePlain];

        mySongViewController.title = @"11111";

        

        [self.mutableArrayForRootView addObject:mySongViewController];

        

    }

     

    #pragma mark - Table view data source

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {        

        return 1;

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        NSInteger numberOfSection = [mutableArrayForRootView count];

        

        return numberOfSection;

    }

     

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *CellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        }

        

        // Configure the cell...

        

        UITableViewController *tableViewController = [self.mutableArrayForRootView objectAtIndex:indexPath.row];

        

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        

        cell.textLabel.text = tableViewController.title;

        return cell;

    }

     

    #pragma mark - Table view delegate

     

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {    

        UITableViewController *tableViewController = [self.mutableArrayForRootView objectAtIndex:indexPath.row];

        [self.navigationController pushViewController:tableViewController animated:YES];

        

    }

    画面如下:

     

    可以按照类似的方法,在table中添加多个cell。

     

     

     

    3、 创建一个子类,为UITableViewController的子类,名为SongViewController,不需要同时创建.xib。此视图为在root视图中,点击了table中的一个cell之后,看到的视图,

    SongViewController.h文件添加如下:

    @property (strong, nonatomic) NSArray *arrayOfSongViewController;

     

    SongViewController.m文件修改如下:

    #import "SongViewController.h"

    #import "DetailViewControllerOfSong.h"

     

    @implementation SongViewController

     

    @synthesize arrayOfSongViewController;

     

    - (void)viewDidLoad

    {

        [superviewDidLoad];

        

        self.arrayOfSongViewController = [[NSArrayalloc]initWithObjects:@"aaa",@"bbbb",@"ccc",nil];

    }

     

    #pragma mark - Table view data source

     

    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

    {

        // Return the number of sections.

        return 1;

    }

     

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

    {

        // Return the number of rows in the section.

        return [self.arrayOfSongViewControllercount];

    }

     

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    {

        static NSString *CellIdentifier = @"SongViewCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

        }

     

        cell.textLabel.text = [self.arrayOfSongViewController objectAtIndex:indexPath.row];

        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

        return cell;

    }

     

    #pragma mark - Table view delegate

     

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

    {

        DetailViewControllerOfSong *detailView = [[DetailViewControllerOfSong alloc]initWithNibName:@"DetailViewControllerOfSong" bundle:nil];

        detailView.chooseMessage = [self.arrayOfSongViewController objectAtIndex:indexPath.row];

        

        detailView.title = [self.arrayOfSongViewController objectAtIndex:indexPath.row];

        

        [self.navigationController pushViewController:detailView animated:YES];

    }

     

    画面如下:

     

     

    4、创建一个UIViewController子类,名称为DetailViewControllerOfSong,可以同时创建.xib。

    DetailViewControllerOfSong.h文件修改如下:

    #import <UIKit/UIKit.h>

     

    @interface DetailViewControllerOfSong : UIViewController

     

    @property (nonatomic, strongNSString *chooseMessage;

     

    @end

     

    DetailViewControllerOfSong.m文件修改如下:

    - (void)viewDidLoad

    {

        [superviewDidLoad];

        // Do any additional setup after loading the view from its nib.

        

        UIAlertView *alertView = [[UIAlertViewalloc] initWithTitle:@"message" message:self.chooseMessage delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil,nil];

        

        [alertView show];

    }

    有人说:爱上一座城,是因为城里住着某个人,能够与所爱的人在一起,连光阴都是美的。即便粗茶淡饭,修篱种田,只要有你陪伴就好。那么,找一个青山绿水的地方,寻一处幽静的茅舍,或是云水禅心的庭院,那里有晴朗的阳光和静谧的悠然,还有你明媚的笑脸。掬一捧花香在平淡的日子,握着一路相随的暖意,让爱的馨香在柴米油盐中升腾;在一杯茶的温情里,体味生活的诗意;在一碗粥的清淡中,感受生活的浪漫,每天清晨你和阳光都在,便是我的幸福。——春暖花开 《择一城终老,遇一人白首》
  • 相关阅读:
    树形数据深度排序处理示例(递归法).sql
    12种JavaScript MVC框架之比较
    逐级汇总示例(用户定义函数法).sql
    名次查询的处理示例.sql
    实现删除指定结点及所有子节点的处理触发器.sql
    memcpy和memmove的区别
    据说是月薪2W的笔试题
    C++重点知识
    Java初学者需掌握的30个概念
    (转)微软面试题
  • 原文地址:https://www.cnblogs.com/-Eric-Liu/p/5563956.html
Copyright © 2011-2022 走看看