【问题标题】:sub-UIView to push main UIView into DetailViewsub-UIView 将主 UIView 推送到 DetailView
【发布时间】:2012-02-14 02:45:07
【问题描述】:

所以我有一个分段控件,可以在两个子 UIViews 之间切换,这两个子 UIViews 都包含 UITableView。但是,每当我单击表中的某一行时,它都无法将主 UIView 推送到 DetailView 中。

简单地说,我试图让子 UIView(包含 TableView)将(深灰色)主 UIView 推送到详细视图中

我错过了什么?

MenuViewController.H

@class MenuDrinksViewController;
@class MenuFoodViewController;
@interface MenuViewController : UIViewController
{
    MenuDrinksViewController *menuDrinksViewController;
    MenuFoodViewController *menuFoodViewController;

    UISegmentedControl *segmentedControl;
}

@property (nonatomic, retain) UISegmentedControl *segmentedControl;

@end

MenuViewController.m

#import "MenuDrinksViewController.h"
#import "MenuFoodViewController.h"


@implementation MenuViewController
@synthesize segmentedControl;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Menu", @"Menu");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

    }
    return self;
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    menuDrinksViewController = [[MenuDrinksViewController alloc] init];
    menuFoodViewController = [[MenuFoodViewController alloc] init];

    AppearanceClass *appearanceClass = [[AppearanceClass alloc] init];


    // segmented controller
    segmentedControl = [[UISegmentedControl alloc] initWithItems:[[NSArray alloc] initWithObjects:@"Food", @"Drinks", nil]];
    [appearanceClass setSegmentedControl:segmentedControl];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:segmentedControl];



    [self.view addSubview:menuFoodViewController.view];

    menuDrinksViewController.view.frame = CGRectMake(0, 40, 320, 367);
    menuFoodViewController.view.frame = CGRectMake(0, 40, 320, 367);

    [self loadMenuFoodXML];
    [self loadMenuDrinksXML];

}

-(IBAction)segmentAction:(id)selector
{


    if (segmentedControl.selectedSegmentIndex == 0)
    {
        [self.view addSubview:menuFoodViewController.view];
        [menuDrinksViewController.view removeFromSuperview];


    } else if (segmentedControl.selectedSegmentIndex == 1)
    {

        [self.view addSubview:menuDrinksViewController.view];
        [menuFoodViewController.view removeFromSuperview];
    }

}

MenuFoodViewController.h

@interface MenuFoodViewController : UIViewController
<UITableViewDelegate>
{
    NSMutableArray *dMenu;

    UITableView *TableView;

}

@property (nonatomic, retain) NSMutableArray *fMenu;

@end

MenuFoodViewController.m

#import "AppDelegate.h"

#import "AppearanceClass.h"

#import "MenuFoodViewController.h"

#import "MenuFood.h"
#import "MenuDrinks.h"
#import "Item.h"

#import "MenuFoodCustomTableViewCell.h"

#import "MenuFoodDetailViewController.h"

@implementation MenuFoodViewController
@synthesize fMenu;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Menu", @"Menu");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];

    }
    return self;
}



#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    AppearanceClass *appearanceClass = [[AppearanceClass alloc] init];

    self.view.backgroundColor = [UIColor clearColor];

    // TableView
    TableView.frame = CGRectMake(0, 0, 320, 327);

    [self.view addSubview:TableView];

}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

     MenuFoodDetailViewController *menuFoodDetailViewController = [[MenuFoodDetailViewController alloc] initWithNibName:@"MenuFoodDetailViewController" bundle:nil];

    MenuFood *aMenuFood = [fMenu objectAtIndex:indexPath.section];

        Item *aItem = [aMenuFood.item objectAtIndex:indexPath.row];
        menuFoodDetailViewController.item = aItem;
        menuFoodDetailViewController.navigationItem.title = @"food"; 


    [self.navigationController pushViewController:menuFoodDetailViewController animated:YES];

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    NSLog(@"This is being pressed");
}

【问题讨论】:

  • 我不明白你的问题。其他人可以一样

标签: iphone objective-c ios cocoa-touch uiview


【解决方案1】:

你在实现 tableView:didSelectRowAtIndexPath 吗?您可以在其中创建详细视图,然后使用导航控制器推送它。

例如:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{   
    // get the selected cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];

    // navigate to detail
    DetailedTableViewController *detailedView = [[DetailedTableViewController alloc] init];
    [[self navigationController] pushViewController:detailedView animated:YES];
}

如果您不想推送到导航控制器而是想换出子视图的内容,请查看:

transitionFromView:toView:duration:options:completion:使用给定参数在指定视图之间创建过渡动画。

+ (void)transitionFromView:(UIView *)fromView toView:(UIView *)toView duration:

【讨论】:

  • 是的,我已经实现了 didSelectRowAtIndexPath: 但它根本没有推送视图,甚至没有做任何事情。
  • 你能添加一个 NSLog 以确保它被调用吗?另外,self.navigationController 是 nil 吗?
  • 你是对的,self.navigationController = (null)。这是什么意思?
  • 你是如何在主窗口中添加 MenuViewController 的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
相关资源
最近更新 更多