1

我正在尝试将视图从表格单元格推送到另一个视图,我知道那里有很多教程,我已经尝试了很多,已经尝试了 2 天但仍然无法正常工作,这是我的代码..

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

根视图控制器.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]));
    }
}
4

2 回答 2

3

实际上,您的问题很简单,您参考self.navigationController,但视图控制器在 AppDelegate 中没有设置导航控制器!您正在向 nil 发送一条消息,该消息产生 nil (因此,没有任何反应)。在 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:
}
于 2012-05-25T03:08:57.053 回答
0

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

于 2012-05-25T03:17:09.300 回答