【问题标题】:How to add a common view to different xib?如何为不同的xib添加一个共同的视图?
【发布时间】:2011-08-26 12:36:16
【问题描述】:

我是 iPhone/iPad 编程的新手。我的问题是,如何将公共视图添加到两个或多个视图(或 xib 文件)。比如说,我计划添加一个通用菜单以显示在不同的 xib 上。目前我将相同的元素(按钮)添加到所有 xib。有什么方便的方法吗?

【问题讨论】:

    标签: iphone ipad


    【解决方案1】:

    在 MainWindow.xib 文件中获取您的共同视图,并在 AppDelegate 文件中获取其 IBOutlet 和属性..

    正确连接您的IBOutlet,何时在其他视图控制器中使用它,您应该创建您的 appDelegate 实例,然后将您的公共视图添加到您的视图控制器并设置其框架。

    例如...

    [appDelegate.indicatorView setFrame:CGRectMake(107, 213, 106, 81)];
    [self.view addSubview:appDelegate.indicatorView];
    

    appDelegate 是您的实例对象,通过创建视图属性,您可以访问它。 indicatorView 是您在MainWindow.xib 文件中的常用视图..

    【讨论】:

    • 您能详细解释一下吗?正如我在帖子中提到的,我在 iPhone/iPad 编程方面完全是新手。
    【解决方案2】:

    为什么你不使用标签栏控制器?

    用户标签栏控制器和隐藏标签栏并添加您的自定义菜单项(按钮)。请参阅下面的代码并尝试一下。

    - (void) hideTabBar
    {
         for(UIView *view in tabController.view.subviews)
         {
             if([view isKindOfClass:[UITabBar class]])
             {
                view.hidden = YES;
                break;
             }
         }
    }
    
    - (void) addCustomTabs
    {
         totalTabs = 5;
         CGRect rect = CGRectMake(0.0, 0.0, 64.0, 49.0);
    
         tabScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 460.0-rect.size.height, 320.0, rect.size.height)];
         tabScroller.backgroundColor = [UIColor clearColor];
         tabScroller.showsHorizontalScrollIndicator = NO;
         [tabController.view addSubview:tabScroller];
    
         float x = 0.0;
         for (int i = 0; i < totalTabs; i++) {
              customTabs[i] = [UIButton buttonWithType:UIButtonTypeRoundedRect];
              customTabs[i].frame = CGRectMake(x, 0.0, rect.size.width, rect.size.height);
              customTabs[i].tag = i;
              [customTabs[i] addTarget:self action:@selector(selectTab:) forControlEvents:UIControlEventTouchUpInside];
              [customTabs[i] setTitle:[NSString stringWithFormat:@"%d",i+1] forState:UIControlStateNormal];
              [tabScroller addSubview:customTabs[i]];
              x += rect.size.width;
          }
    
          tabScroller.contentSize = CGSizeMake(x, rect.size.height);
    }
    
    - (void) selectTab:(id)sender
    {
          int tabID = [sender tag];
    
          for (int i = 0; i < totalTabs; i++) {
             customTabs[i].selected = NO;
          }
    
          customTabs[tabID].selected = YES;
          tabController.selectedIndex = tabID;
    
    }
    

    【讨论】:

    • 实际上我希望菜单出现在顶部。我可以使用标签栏来实现吗?
    猜你喜欢
    • 1970-01-01
    • 2011-02-10
    • 2018-09-24
    • 2018-07-18
    • 1970-01-01
    • 2012-12-31
    • 2015-06-20
    • 2021-07-26
    • 1970-01-01
    相关资源
    最近更新 更多