sub-UIView将主UIView推入DetailView

时间:2012-02-14 02:45:07

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

my problem

所以我有一个分段控件,可以在两个包含UITableView的子UIView之间切换。但每当我单击表中的一行时,它都无法将主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");
}

1 个答案:

答案 0 :(得分:0)

您是否正在实施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: