2012-05-25 41 views
1

我试图从一个表格单元格推到另一个视图,我知道有很多的教程,我已经尝试了很多,一直在尝试2天,仍然可以“弄不明白的工作,这是我的代码..Xcode的pushViewController

AppDelegate.h

#import <UIKit/UIKit.h> 


@class RootViewController; 
@interface AppDelegate : UIResponder <UIApplicationDelegate> 

@property (strong, nonatomic) UIWindow *window; 

@property (strong, nonatomic) RootViewController *rootViewController; 
@end 

AppDelegate.m

#import "AppDelegate.h" 

#import "RootViewController.h" 
@implementation AppDelegate 

@synthesize window = _window; 
@synthesize rootViewController; 
- (void)dealloc 
{ 
    [_window release]; 
    [super dealloc]; 
} 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    self.rootViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; 
    self.window.rootViewController = self.rootViewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 



@end 

RootViewController.h

#import <UIKit/UIKit.h> 

@interface RootViewController : UITableViewController{ 

    NSMutableArray* array; 

} 
@property(nonatomic, retain)NSMutableArray* array; 
@end 

RootViewController.m

#import "RootViewController.h" 
#import "SubLevelViewController.h" 
@implementation RootViewController 
@synthesize array; 
- (void)viewDidLoad { 
    [super viewDidLoad]; 

    array = [[NSMutableArray alloc] init]; 

    [array addObject:@"One"]; 
    [array addObject:@"Two"]; 
    [array addObject:@"Three"]; 


    // Uncomment the following line to display an Edit button in the navigation bar for this view controller. 
    self.navigationItem.rightBarButtonItem = self.editButtonItem; 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    // Release anything that can be recreated in viewDidLoad or on demand. 
    // e.g. self.myOutlet = nil; 
} 


#pragma mark Table view methods 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { 
    return 1; 
} 


// Customize the number of rows in the table view. 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 
    return [array count]; 
} 


// Customize the appearance of table view cells. 
- (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] autorelease]; 
    } 

    NSString *cellValue = [array objectAtIndex:indexPath.row]; 

    cell.textLabel.text = cellValue; 

    // Configure the cell. 

    return cell; 
} 

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

    SubLevelViewController *sub = [[SubLevelViewController alloc] initWithNibName:@"SubLevelViewController" bundle:nil]; 

    sub.title = @"My First View"; 

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

} 

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


@end 

和main.m文件

#import <UIKit/UIKit.h> 

#import "AppDelegate.h" 

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
    } 
} 
+0

你需要给我们更多关于你的问题的细节。发生了什么?你有错误吗? – glenstorey

+0

当我点击表格单元格时,它无法进入另一个视图。 – user486174

回答

3

其实,你的问题很简单,您引用self.navigationController,但视图控制器没有导航控制器在AppDelegate中成立!你发送一条消息给nil,它产生nil(ergo,没有任何反应)。在的appDelegate试试这个:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    // Override point for customization after application launch. 
    self.rootViewController = [[[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil] autorelease]; 
    UINavigationController * controller = [[[UINavigationController alloc]initWithRootViewController:self.rootViewController]autorelease]; 
    self.window.rootViewController = controller; 
    [self.window makeKeyAndVisible]; 
    return YES: 
} 
+0

如果有人能为我设计这种格式,我会非常感激,因为我在iPhone上输入了这些内容。 – CodaFi

+0

非常感谢!有用! – user486174

+1

@CodaFi完成..... – Krishnabhadra

0

同意CodaFi,你需要创建一个UINavigationController,并记住它推动和弹出任何的UIViewController如果你想保持推/弹出堆栈。不要使用self.navigationController。

+0

为什么你的意思是不使用self.navigationController? –