如何将 2 个 UIView 组合成 1 个视图控制器?

发布于 2024-11-16 22:57:48 字数 2532 浏览 3 评论 0 原文

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

我有 rootView 和 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;
        }
    }
}

I have a ViewController with 3 views: Rootview that shows toolbar with UISegmentedControl, tableView, and calendarView.

I have XIB for the rootView, and tableView, but the calendarView doesn't have a XIB.

I need to somehow combine the code to load the calendar view to fit with with this ViewController. Before, I was using the calendarView as its own viewController.

The code for 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];
}

If I put that code directly into this new viewController, it does not respect the UISegmentedControl and just shows up at launch.

Here is the code for the 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;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

万劫不复 2024-11-23 22:57:48

使用两种不同的 init-Methods 是否满足您的需求? initWithNibName:@"nib1" 或者同样initWithNibName:@"nib2"
否则,您将需要详细说明您想要实现的目标

Does using two different init-Methods fit your needs? initWithNibName:@"nib1" or likewise initWithNibName:@"nib2"??
Otherwise you will need to specify what you want to achieve a little more

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文