通过编程实现UINavigationController 包含 UITableViewController

本文将指导您从基础知识开始,逐步掌握使用Objective-C编写iOS应用的全过程,包括环境搭建、基本语法、界面设计、数据交互等核心技能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


//应用程序代理
@interface SimpleTableViewAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window; //程序主窗体
UINavigationController *navigationController; //在接口中定义导航控制器
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navigationController;

@end


//引入接口
#import "SimpleTableViewAppDelegate.h"
#import "RootViewController.h" //继承UITableViewController的子类接口


@implementation SimpleTableViewAppDelegate

@synthesize window;
@synthesize navigationController;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

/*
通过一般样式初始化UITableViewController子类
*/
RootViewController *rootViewController = [[RootViewController alloc] initWithStyle:UITableViewStylePlain];

//接收一个数组,数据从当前系统获得所有时区作为数组的元素
NSArray *timeZones = [NSTimeZone knownTimeZoneNames];

//根视图控制器UITableViewController中时区名的数组数据等于排序后的时区数组数据
rootViewController.timeZoneNames = [timeZones sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];


//------------知识点-------------
//编程实例化一个导航控制器,并没有继承某个类或者遵循某个协议,关键是initWithRootViewController:rootViewController方法,他是把rootViewController实例作为导航控制器的根视图。
UINavigationController *aNavigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

//赋值到当前程序代理接口中定义的导航控制器类的实体
self.navigationController = aNavigationController;
[aNavigationController release];
[rootViewController release];

//把导航控制器的视图增加到当前的window对象中。
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}

- (void)dealloc {
[navigationController release];
[window release];
[super dealloc];
}

@end


//表视图控制器子类
@interface RootViewController : UITableViewController {
NSArray *timeZoneNames; //存储所有时区名字
}

@property (nonatomic, retain) NSArray *timeZoneNames;

@end




#import "RootViewController.h"
#import "SimpleTableViewAppDelegate.h"


@implementation RootViewController

@synthesize timeZoneNames;


- (void)viewDidLoad {
//设定当前控制器的标题,以备导航控制器显示
//通过第一个参数作为键取当前程序沙盒中找Localizable.strings文件中的内容,第二个参数是备注这个字段的意思,不显示到界面中的。
self.title = NSLocalizedString(@"Time Zones", @"Time Zones title");
}

//接口继承自UITableViewController,需要实现这个方法获得一共有多少部分。
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// 这里只有一个部分.
return 1;
}

//接口继承自UITableViewController,需要实现这个方法获得每一部分有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of time zone names.
return [timeZoneNames count];
}

//回调方法,表格视图控制器的每一行的显示都是通过这个方法进行处理的
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *MyIdentifier = @"MyIdentifier";

// Try to retrieve from the table view a now-unused cell with the given identifier.
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];

// If no cell is available, create a new one using the given identifier.
if (cell == nil) {
// Use the default cell style.
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier] autorelease];
}

// 从时区数组中获得当前行号对应的时区数据作为这行的文本显示
NSString *timeZoneName = [timeZoneNames objectAtIndex:indexPath.row];
cell.textLabel.text = timeZoneName;

return cell;
}

/*
To conform to Human Interface Guildelines, since selecting a row would have no effect (such as navigation), make sure that rows cannot be selected.
当对表格行进行选择时,禁止做选择动作。
*/
- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
return nil;
}


- (void)dealloc {
[timeZoneNames release];
[super dealloc];
}

@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值