objective-c - Xcode 中的 pushViewController

标签 objective-c ios xcode xcode4.2

我正在尝试将 View 从表格单元格推送到另一个 View ,我知道那里有很多教程,我已经尝试了很多,已经尝试了 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

Root View Controller .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]));
    }
}

最佳答案

其实你的问题很简单,引用self.navigationController ,但 View Controller 没有在 AppDelegate 中设置导航 Controller !您正在向 nil 发送一条消息,该消息产生 nil (因此,没有任何 react )。在 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:
}

关于objective-c - Xcode 中的 pushViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10747836/

相关文章:

iphone - 核心数据建模基础知识

xcode - 在 Xcode 7.0 中获取框架相关警告

ios - 使用 Storyboard从 XIB 加载 UIView - View 的属性为零

ios - 即使应用程序被终止并在 ios 中显示本地通知,如何进行服务器调用。

objective-c - 如果时间为零,如何使 NSDateFormatter 不包括时间?

objective-c - 使用 NSPredicate 访问 self.items

ios - Xcode动态实时更新折线图

android - 我们可以使用 Protractor 在带有 appium 的原生 Android 或 IOS 应用程序中进行自动化测试吗

iphone - 我是否需要初始化作为 plist 导入一部分的 iOS 空嵌套数组?

iphone - UILabel 居中对齐问题