gpt4 book ai didi

objective-c - Xcode 中的 pushViewController

转载 作者:行者123 更新时间:2023-12-01 19:20:11 37 4
gpt4 key购买 nike

我正在尝试将 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com