2012-05-25 2 views
1

Я пытаюсь вывести представление из ячейки таблицы в другое представление, я знаю, что там много учебников, я пробовал много из них, пробовал в течение 2 дней и все еще могу «т заставить его работать, вот мой код ..pushViewController в Xcode

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, если вы хотите сохранить стек PUSH/поп. Не используйте self.navigationController.

+0

Почему вы подразумеваете, что не используете self.navigationController? –

Смежные вопросы