標籤:des style blog color os io ar for 2014
功能:點擊清單項目,用列表字串作為參數建立一個新視圖,新視圖預設可以有一個BACK按鈕回到上一個視圖
//// main.m// Hello//// Created by lishujun on 14-8-28.// Copyright (c) 2014年 lishujun. All rights reserved.//#import <UIKit/UIKit.h>// ------------- 接受參數的視圖控制器-------------@interface TestViewController : UIViewController@end@implementation TestViewController-(TestViewController *)initWithArg:(NSString *)arg{ NSLog(@"%@", arg); return self;}-(void) loadView{ //建立視圖對象 UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor lightGrayColor]; self.view = contentView;}@end// --------------視圖控制器對象--------------@interface HelloWorldViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{ UITableView *tableView; NSArray *array;}@end@implementation HelloWorldViewController-(HelloWorldViewController *) init{ array = [[NSArray alloc]initWithObjects:@"oops", @"hello", @"world", nil]; return self;}-(void) loadView{ //建立視圖對象 UIView *contentView = [[UIView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]]; contentView.backgroundColor = [UIColor lightGrayColor]; self.view = contentView; tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]]; [self.view addSubview:tableView]; tableView.dataSource = self; tableView.delegate = self;}// --------DataSource介面必須實現的方法,用於填充表格------------(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{ return 1; //設定列表分區個數}-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ return [array count]; //設定列表長度}-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ // 產生列表單元(儘可能複用已有單元) static NSString *simpleTableIdentifer = @"SimpleTableIdentifer"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifer]; if (cell == nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifer]; } cell.textLabel.text = array[indexPath.row]; return cell;}// ----------實現Delegate介面的方法,用於操作----------------(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ //行選中事件 NSString *rowValue = array[indexPath.row]; [tableView deselectRowAtIndexPath:indexPath animated:YES]; //取消選中狀態 [self.navigationController pushViewController:[[TestViewController alloc]initWithArg:rowValue] animated:YES];}@end// ----------------委派物件--------------------@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate>{ IBOutlet UIWindow *window;}@property (nonatomic, retain) UIWindow *window;@property (nonatomic, retain) UINavigationController *nav;@end@implementation HelloWorldAppDelegate@synthesize window;@synthesize nav;-(void) applicationDidFinishLaunching:(UIApplication *)application{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen]bounds]]; HelloWorldViewController *viewController = [[HelloWorldViewController alloc]init]; self.nav = [[UINavigationController alloc]initWithRootViewController: viewController]; self.window.rootViewController = self.nav; [self.window makeKeyAndVisible];}@end// ---------------程式入口---------------------int main(int argc, char * argv[]){ @autoreleasepool { return UIApplicationMain(argc, argv, nil, @"HelloWorldAppDelegate"); }}
iOS:NAV+TABLE結合