如何将2个UIView合并为1个视图控制器?

时间:2011-06-25 21:12:22

标签: iphone uitableview uiviewcontroller uisegmentedcontrol addsubview

我有一个包含3个视图的ViewController:Rootview,显示带有UISegmentedControl,tableView和calendarView的工具栏。

我有root用户和tableView的XIB,但是calendarView没有XIB。

我需要以某种方式组合代码来加载日历视图以适应此ViewController。以前,我使用calendarView作为自己的viewController。

calendarView的代码:

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
        calendar =  [[TKCalendarMonthView alloc] init];
        calendar.delegate = self;
        calendar.dataSource = self;
    }
    return self;
}

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView 
{
    UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:self action:@selector(dismissCalendarView)];
    self.navigationItem.leftBarButtonItem = actionButton;
    [actionButton release];

    int statusBarHeight = 0;
    CGRect applicationFrame = (CGRect)[[UIScreen mainScreen] applicationFrame];
    self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, applicationFrame.size.width, 300.0)] autorelease];
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.view.backgroundColor = [UIColor clearColor];
    self.title = @"Select Workout";
    calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);
    NSLog(@"%f height", applicationFrame.size.height);
    [self.view addSubview:calendar];
    [calendar reload];
}

如果我将该代码直接放入这个新的viewController中,它不会尊重UISegmentedControl并且只是在启动时显示。

以下是UISegmentedConrol的代码:

- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{    
    switch (index) 
    {
        case 0: 
        {
            [self.view addSubview: tableView1];
            tableView1.hidden = NO;
            calendar.hidden = YES;
            [calendar removeFromSuperview];
            break;
        }
        case 1: 
        {
            [self.view addSubview: calendar];
            tableView1.hidden = YES;
            calendar.hidden = NO;
            [tableView1 removeFromSuperview];
            break;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

使用两种不同的init-Methods是否符合您的需求? initWithNibName:@"nib1"或同样initWithNibName:@"nib2" ?? 否则,您需要指定您想要实现的目标