xcode 4.2 中 iPhone 的多级导航
我在 xcode 4.2 中创建了一个基于视图的项目,我想实现多级导航以导航到不同的视图。我的第一个视图类是 ViewController,我在其中显示一个表格。单击一行后,我进入下一个带有选项卡栏的类。在此我有 3 个选项卡。第一个选项卡使用类 BuyerViewController。它有 4 个按钮。问题是,当我单击按钮时,它不会推送下一堂课。
ViewController.m
- (void)viewDidLoad
{
self.title=@"Select County";
tView = [[UITableView alloc] init]; //tView is my table view object.
tView.frame=CGRectMake(0, 20, 320, 460);
[tView setDelegate:self];
[tView setDataSource:self];
[self.view addSubview:tView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [countyArray count];
}
- (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];
}
cell.textLabel.text = [countyArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//This works fine and pushes to my next class to show the tabs.
ShowOptionInTab *showTabbar = [[ShowOptionInTab alloc] initWithNibName:@"ShowOptionInTab" bundle:nil];
[self.navigationController pushViewController:showTabbar animated:YES];
[showTabbar release];
}
ShowOptionInTab.m
-(void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
BuyerViewController *buyerController = [[BuyerViewController alloc] init ];
SellerViewController *sellerController = [[SellerViewController alloc] init];
LenderViewController *lenderController = [[LenderViewController alloc] init];
buyerController.title=@"Buyer";
sellerController.title = @"Seller";
lenderController.title = @"Lender";
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.view.frame = CGRectMake(0, 0, 320, 460);
[tabbarController setViewControllers:[NSArray arrayWithObjects:buyerController, sellerController,lenderController, nil]];
[buyerController release];
[sellerController release];
[lenderController release];
[self.view addSubview:tabbarController.view];
}
BuyerViewController.m
-(void)viewWillAppear:(BOOL)animated
{
UIButton *quickEstimateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[quickEstimateButton addTarget:self action:@selector(getTagOfPressedButton:) forControlEvents:UIControlEventTouchUpInside];
[quickEstimateButton setTitle:@"QUICK ESTIMATE" forState:UIControlStateNormal];
quickEstimateButton.tag=1;
quickEstimateButton.frame = CGRectMake(10, 50, 150, 40.0);
[self.view addSubview:quickEstimateButton];
//more 3 buttons are added in same way
}
-(void) getTagOfPressedButton:(id)sender {
UIButton *getTagOfButton = (UIButton *)sender;
int buttonPressedTag = getTagOfButton.tag;
NSLog(@"TAG==========%d",buttonPressedTag);
if(buttonPressedTag==1)
{
NSLog(@"quick estimate pressed");
QuickEstimateViewController *q = [[QuickEstimateViewController alloc] init];
NSLog(@"nav====%@",self.navigationController); //This returns null.
[self.navigationController pushViewController:q animated:YES];
}
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *overviewViewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:overviewViewController];
navigation.navigationBar.tintColor = [UIColor blackColor];
[overviewViewController release];
[self.window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
}
为什么QuickEstimateViewController不显示?
I created a View-based project in xcode 4.2 and i want to achieve multi-level navigation to navigate to different views. My first view class is ViewController where i display a table. On Clicking a row i go to a next class which has a tabbar. in this i have 3 tabs. the 1st Tab is using the Class BuyerViewController. It has 4 buttons. The problem is that when i click on the button it does not push the next class.
ViewController.m
- (void)viewDidLoad
{
self.title=@"Select County";
tView = [[UITableView alloc] init]; //tView is my table view object.
tView.frame=CGRectMake(0, 20, 320, 460);
[tView setDelegate:self];
[tView setDataSource:self];
[self.view addSubview:tView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [countyArray count];
}
- (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];
}
cell.textLabel.text = [countyArray objectAtIndex:indexPath.row];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
//This works fine and pushes to my next class to show the tabs.
ShowOptionInTab *showTabbar = [[ShowOptionInTab alloc] initWithNibName:@"ShowOptionInTab" bundle:nil];
[self.navigationController pushViewController:showTabbar animated:YES];
[showTabbar release];
}
ShowOptionInTab.m
-(void)loadView {
UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
contentView.backgroundColor = [UIColor whiteColor];
self.view = contentView;
[contentView release];
BuyerViewController *buyerController = [[BuyerViewController alloc] init ];
SellerViewController *sellerController = [[SellerViewController alloc] init];
LenderViewController *lenderController = [[LenderViewController alloc] init];
buyerController.title=@"Buyer";
sellerController.title = @"Seller";
lenderController.title = @"Lender";
UITabBarController *tabbarController = [[UITabBarController alloc] init];
tabbarController.view.frame = CGRectMake(0, 0, 320, 460);
[tabbarController setViewControllers:[NSArray arrayWithObjects:buyerController, sellerController,lenderController, nil]];
[buyerController release];
[sellerController release];
[lenderController release];
[self.view addSubview:tabbarController.view];
}
BuyerViewController.m
-(void)viewWillAppear:(BOOL)animated
{
UIButton *quickEstimateButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[quickEstimateButton addTarget:self action:@selector(getTagOfPressedButton:) forControlEvents:UIControlEventTouchUpInside];
[quickEstimateButton setTitle:@"QUICK ESTIMATE" forState:UIControlStateNormal];
quickEstimateButton.tag=1;
quickEstimateButton.frame = CGRectMake(10, 50, 150, 40.0);
[self.view addSubview:quickEstimateButton];
//more 3 buttons are added in same way
}
-(void) getTagOfPressedButton:(id)sender {
UIButton *getTagOfButton = (UIButton *)sender;
int buttonPressedTag = getTagOfButton.tag;
NSLog(@"TAG==========%d",buttonPressedTag);
if(buttonPressedTag==1)
{
NSLog(@"quick estimate pressed");
QuickEstimateViewController *q = [[QuickEstimateViewController alloc] init];
NSLog(@"nav====%@",self.navigationController); //This returns null.
[self.navigationController pushViewController:q animated:YES];
}
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
ViewController *overviewViewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:overviewViewController];
navigation.navigationBar.tintColor = [UIColor blackColor];
[overviewViewController release];
[self.window addSubview:[navigation view]];
[self.window makeKeyAndVisible];
}
Why does the the QuickEstimateViewController does not show up?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您定义了 tabBarController 的新对象及其视图作为 ShowOptionInTab 中的子视图,然后还定义了一个新对象 BuyerViewController ,您尝试推送 QuickEstimateViewController 的 navigationController 将为零..我认为如果您的代码如下所示,它会起作用..
Because you define a new object of tabBarController and its view as a subview in ShowOptionInTab and then also define a new object BuyerViewController which its navigationController you try to push QuickEstimateViewController will be nil .. i think its will work if your code look like this ..