1

I'm trying to push a view from a table cell to another view, I know that there's a lot of tutorials out there, I've tried a lot of them, been trying for 2 days and still can't get it working, here's my code..

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

and main.m

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}
2
  • You'll need to give us more detail on what your problem is. What's happening? Are you getting an error? Commented May 25, 2012 at 2:55
  • It just doesn't be able to go to another view when I clicked on the table cells. Commented May 25, 2012 at 3:05

2 Answers 2

3

Actually, your problem is simple, you reference self.navigationController, yet the view controller has no navigation controller set up in the AppDelegate! You're sending a message to nil, which produces nil (ergo, nothing happens). Try this in the 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:
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would be much obliged if someone could format this for me, seeing as I typed this out on an iPhone.
0

Agree with CodaFi, you need to create a UINavigationController and remember it to push and pop any UIViewController if you want to keep the push/pop stack. Do not use self.navigationController.

1 Comment

why do u mean by not use self.navigationController?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.